0% found this document useful (0 votes)
14 views13 pages

Sample Template

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views13 pages

Sample Template

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Name: AKSHARA E

Registration No: 25BPS1013 Date: 14-08-2025


Code: BMAT101P Slot: L5+L6
Title: Matlab: Plotting of curves
Problem Statement 1: Finding the maxima and minima of x^4 + y^4-2x^2+4xy-2y^2

Code Input:

clc

clear all

syms x y real % DECLARES INDEPENDANT VARIABLES (real valued fn )

% The solve() command can solve a system of equations using two variables,

% Where solutions represented as row vectors

% Say f(x,y) and g(x,y)

f = x^4 + y^4 -2*x^2 + 4*x*y -2*y^2;

fx= diff(f,x); fy = diff(f,y); % Finds partial derivatives

[cx,cy] = solve(fx,fy); % Critical points are stored as cx and cy

cx = double(cx);cy = double(cy); % Converts the variables to numeric data type


with double precision

ncp = length(cx);

fprintf('\nThere are %d critical points.\n',ncp);


% If there are multiple values of x and y , then cx and cy shall be stored

% as arrays with all x components stored in cx and y components stored in

% cy

fxx = diff(fx,x);

fyy = diff(fy,y);

fxy = diff(fx,y); % 2nd partial derivatives

D = fxx*fyy - fxy^2;

% For plotting the surface with the critical points , we need a proper interval

% We take [a2-e,a1+e] as the interval where a2 and a1 are the minimum and

% maximum critical points in x and y and e is a small number

a1 = max(cx); a2= min(cx);

b1 = max(cy);b2 =min(cy); % Finding the minimum and max values of x and y to be


included

ex = 0.5;ey=0.5;

s = fsurf(f,[a2-ex,a1+ex,b2-ey,b2+ey],'g','EdgeColor','none');

s.FaceAlpha = .65; % Plotting of the surface with the cp(s)

box on

hold on

% Generating the for loop for 2nd derivative test

for k =1:ncp % Defining start and stop values

T1 = subs(subs(D,x,cx(k)),y,cy(k));T1 = double(T1); % Discriminant at


various cp(s)

% Subs fn substitutes the desired values in place of a variable of a

% multivariable fn

T2 = subs(subs(fxx,x,cx(k)),y,cy(k));T2 = double(T2); % Finding various


values of fxx

T3 = subs(subs(f,x,cx(k)),y,cy(k));T3 = double(T3); % Finding various


values of f at the critical points

if (T1==0) % D = 0

fprintf('\nThe point(%d,%d) needs further investigation.',cx(k),cy(k))

elseif(T1<0) % D is less than 0

fprintf('\nThe point(%d,%d) is a saddle point.',cx(k),cy(k))

fprintf('\nf has no extreme value at (%d,%d)',cx(k),cy(k))

plot3(cx(k),cy(k),T3,'b','markersize',30);

else %D>0

if (T2<0) %fxx at that cp is <0

fprintf('\nThe point(%d,%d) is a local maximum.',cx(k),cy(k))

fprintf('\nThe maximum value of f at(%d,%d) is %d.',cx(k),cy(k),T3)

plot3(cx(k),cy(k),T3,'r+','markersize',30);

else %fxx at a cp is >0

fprintf('\nThe point(%d,%d) is a local minimum.',cx(k),cy(k))

fprintf('\nThe minimum value of f at(%d,%d) is %d.',cx(k),cy(k),T3)

plot3(cx(k),cy(k),T3,'m*','markersize',30);

end

end

end
Code Output:
Problem Statement 2: Finding the maxima and minima of: x^3y^2(1-x-y)

Code Input:

clc

clear all

syms x y real

f = x^3*y^2*(1-x-y);

fx= diff(f,x); fy = diff(f,y);

[cx,cy] = solve(fx,fy);

cx = double(cx);cy = double(cy);

ncp = length(cx);

fprintf('\nThere are %d critical points.\n',ncp);

fxx = diff(fx,x);

fyy = diff(fy,y);

fxy = diff(fx,y);

D = fxx*fyy - fxy^2;

a1 = max(cx); a2= min(cx);

b1 = max(cy);b2 =min(cy
ex = 0.5;ey=0.5;

s = fsurf(f,[a2-ex,a1+ex,b2-ey,b2+ey],'r','EdgeColor','none');

s.FaceAlpha = .65;

box on

hold on

% Generating the for loop for 2nd derivative test

for k =1:ncp

T1 = subs(subs(D,x,cx(k)),y,cy(k));T1 = double(T1);

T2 = subs(subs(fxx,x,cx(k)),y,cy(k));T2 = double(T2);

T3 = subs(subs(f,x,cx(k)),y,cy(k));T3 = double(T3

if (T1==0)

fprintf('\nThe point(%d,%d) needs further investigation.',cx(k),cy(k))

elseif(T1<0)

fprintf('\nThe point(%d,%d) is a saddle point.',cx(k),cy(k))

fprintf('\nf has no extreme value at (%d,%d)',cx(k),cy(k))

plot3(cx(k),cy(k),T3,'b','markersize',30);

else

if (T2<0)

fprintf('\nThe point(%d,%d) is a local maximum.',cx(k),cy(k))

fprintf('\nThe maximum value of f at(%d,%d) is %d.',cx(k),cy(k),T3)

plot3(cx(k),cy(k),T3,'r+','markersize',30);

else

fprintf('\nThe point(%d,%d) is a local minimum.',cx(k),cy(k))

fprintf('\nThe minimum value of f at(%d,%d) is %d.',cx(k),cy(k),T3)

plot3(cx(k),cy(k),T3,'m*','markersize',30);
end

end

end

Code Output:
LANGRANGE ‘S MULTIPLIER METHOD – MODULE 4
Problem Statement 3:
Find the maxima and minima of the function: f = x*2y*3z*4
subject to the constraint x+y+z=5

Code Input:

%% Constrained Maxima and minima of functions - Lagrange's method:

clc

clear all

syms x y z lam real % Variables of lagrange's equation defined

f= x^2*y^3*z^4;

g= x+y+z-5; % Constraint definition

F=f-lam*g;

Fd=jacobian(F,[x y z lam]); % Getting values of Fx,Fy,Fz,Flam

[ax,ay,az, alam]=solve(Fd,[x y z lam]); % Getting various values of x y z lam

ax=double(ax); ay=double(ay); az=double(az); %Conversion to numeric type


T = subs(f,{x,y,z},{ax,ay,az}); % Function value at critical points

T=double(T);

for i = 1:length(T), % T is an array

fprintf('\nCritical point of f is (%1.3f,%1.3f,%1.3f).',ax(i),ay(i),az(i))

fprintf('\nf(%1.3f,%1.3f,%1.3f) = %1.3f',ax(i),ay(i),az(i),T(i));

end

fprintf('\nMinimum value of f is %1.3f.',min(T))

fprintf('\nMinimum value of f is %1.3f.',max(T))

Code Output:

Problem Statement 4: A rectangular box, open at the top, is to have a volume of 32


c.c. Find the dimensions of the box, that requires the least material for its
construction.

Code Input :
%% Word problems

clc

clear all

syms x y z lam real

f= x*y+2*y*z+2*x*z;

g= x*y*z-32;

F=f-lam*g;

Fd=jacobian(F,[x y z lam]);

% restrictions on the independent variables

eq1=x>=0;

eq2=y>=0;

eq3=z>=0;

eqns=[eq1,eq2,eq3,Fd];

[ax,ay,az, alam]=solve(eqns,[x y z lam]);

ax=double(ax); ay=double(ay); az=double(az);

T = subs(f,{x,y,z},{ax,ay,az});

T=double(T);

for i = 1:length(T),

fprintf('\nExtreme value occurs at (%1.3f,%1.3f,%1.3f).',ax(i),ay(i),az(i))

fprintf('\nf(%1.3f,%1.3f,%1.3f) = %1.3f',ax(i),ay(i),az(i),T(i));

end

fprintf('\nMinimum value of the surface area such that cost is minimum is


%1.3f.',min(T))

Code Output:
Problem Statement 5:

Find the maximum and minimum distances from the origin to the curve
3x2+4xy+6y2=140.

Code Input :

clc

clear all

syms x y z lam real

f= (x^2+y^2+z^2)^(0.5);

g= 3*x^2+4*x*y+6*y^2-140;

F=f-lam*g;

Fd=jacobian(F,[x y z lam]);

[ax,ay,az, alam]=solve(Fd,[x y z lam]);

ax=double(ax); ay=double(ay); az=double(az);

T = subs(f,{x,y,z},{ax,ay,az});

T=double(T);

for i = 1:length(T),
fprintf('\nExtreme value occurs at (%1.3f,%1.3f,%1.3f).',ax(i),ay(i),az(i))

fprintf('\nf(%1.3f,%1.3f,%1.3f) = %1.3f',ax(i),ay(i),az(i),T(i));

end

fprintf('\nMinimum value of distance is %1.3f.',min(T))

fprintf('\nMaximum value of distance is %1.3f.',max(T))

Code Output:

You might also like