Matlab 02

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

MATLAB ASSIGNMENT 02

NAME - NEERAV BABEL


BRANCH - CSE* SECTIOON - B

USN NO. - 23BTRCT088

1) if then find

syms x y f=x^3+3*x*y^2-15*x^2-15*y^2+72*x

f =

diff(f,x)

ans =

diff(f,y)

ans =

diff(f,x,2)

ans =

diff(f,y,2)

ans =

diff(f,x,y)

ans =

2) Obtain the maximum and minimum values of


clc clear
ALL syms x
y
f=input('Enter the function f(x,y):');
p= diff(f,x); q=diff(f,y);
[ax,ay]=solve(p,q);
ax=double(ax);ay=double(ay);
r= diff(p,x); s=diff(p,y);

t =diff(q,y); D=r*t-s^2;
for i=1:size(ax)
T1=subs(D,{x,y},{ax(i),ay(i)});
T2=subs(r,{x,y},{ax(i),ay(i)}); T3=subs(f,{x,y},{ax(i),ay(i)}); if
(double(T1) == 0) sprintf('At (%f,%f) further investigation is
required', ax(i),ay(i)) elseif (double(T1) < 0)
sprintf('The point (%f,%f) is a saddle point', ax(i),ay(i)) else
if (double(T2) < 0) sprintf('The maximum
value of the function is f(%f,%f)=
%f',ax(i),ay(i),double(T3)) else
sprintf('The minimum value of the function is
f(%f,%f)=%f',ax(i),ay(i),double(T3)) end end
end

ans =
'The point (0.000000,0.000000) is a saddle point' ans
=
'The maximum value of the function is f(-1.000000,0.000000)=1.000000'
ans =
'The maximum value of the function is f(1.000000,0.000000)=1.000000'
ans =
'The minimum value of the function is f(0.000000,-1.000000)=-1.000000'
ans =
'The minimum value of the function is f(0.000000,1.000000)=-1.000000'
ans =
'The point (-1.000000,-1.000000) is a saddle point' ans
=
'The point (1.000000,-1.000000) is a saddle point' ans
=
'The point (-1.000000,1.000000) is a saddle point' ans
= 'The point (1.000000,1.000000) is a saddle point'
2

3) Obtain the maximum and minimum values of


clc clear
ALL syms
x y
f=input('Enter the function f(x,y):');
p= diff(f,x);
q=diff(f,y);
[ax,ay]=solve(p,q);
ax=double(ax);ay=double(ay);
r= diff(p,x);
s=diff(p,y);
t =diff(q,y);
D=r*t-s^2; for
i=1:size(ax)
T1=subs(D,{x,y},{ax(i),ay(i)});
T2=subs(r,{x,y},{ax(i),ay(i)}); T3=subs(f,{x,y},
{ax(i),ay(i)});
if (double(T1) == 0)
sprintf('At (%f,%f) further investigation is required', ax(i),ay(i))
elseif (double(T1) < 0)
sprintf('The point (%f,%f) is a saddle point', ax(i),ay(i))
else
if (double(T2) < 0)
sprintf('The maximum value of the function is
f(%f,%f)=%f',ax(i),ay(i),double(T3))
else
sprintf('The minimum value of the function is
f(%f,%f)=%f',ax(i),ay(i),double(T3))
end
end
end

ans =
'The maximum value of the function is f(0.000000,0.000000)=1.000000'
ans =
'The minimum value of the function is f(-0.707107,-0.707107)=0.500000'
ans =
'The minimum value of the function is f(0.707107,-0.707107)=0.500000'
ans =
'The minimum value of the function is f(-0.707107,0.707107)=0.500000'
ans =
'The minimum value of the function is f(0.707107,0.707107)=0.500000'
ans =
'The point (-0.707107,0.000000) is a saddle point'
ans =
'The point (0.707107,0.000000) is a saddle point'
ans =
'The point (0.000000,-0.707107) is a saddle point'
ans =
'The point (0.000000,0.707107) is a saddle point'

1
4)Find the Gradient of the function

syms x y
f=input( 'Enter the function f(x,y):'); grad=gradient(f,
[x,y])

grad =

f1=diff(f,x);
f2=diff(f,y);
P = inline(vectorize(f1), 'x', 'y');
Q = inline(vectorize(f2), 'x','y');
x = linspace(-2, 2, 10);
y = x;
[X,Y] = meshgrid(x,y);
U = P(X,Y); V =
Q(X,Y);
quiver(X,Y,U,V,1)
axis on
xlabel('x')
ylabel('y')
hold on
ezcontour(f,[-2 2])

5) Find the divergence of a vector field

1
x=-4:0.5:4;
y=x;
[X Y]=meshgrid (x,y);
Div=divergence(X,Y,X.*Y, X.^2);
figure
pcolor(X,Y,Div);
shading interp
hold on;
quiver(X,Y, X.*Y, X.^2,'Y');
hold off;
colormap copper
title('Vector field of F(x,y)=[xy,x^2]');

6) Visualize the curl of a vector function

1
syms x y z real f =
[y*z, 3*x*z, z];

% Calculate the curl of the vector function


curl_f = curl(f, [x, y, z]);

% Create a grid of points in the x-y-z space


[x, y, z] = meshgrid(-5:0.5:5, -5:0.5:5, -5:0.5:5);

% Evaluate the curl at each point in the grid


curl_x = eval(curl_f(1));
curl_y = eval(curl_f(2));
curl_z = eval(curl_f(3));

% Plot the curl vector field using quiver3


figure;
quiver3(x, y, z, curl_x, curl_y, curl_z);
xlabel('X-axis'); ylabel('Y-
axis'); zlabel('Z-axis');
title('Curl of the Vector Field f = yz i + 3zx j + z k');
grid on;

____________THANK YOU____________

You might also like