0% found this document useful (0 votes)
28 views

Matlab (Da)

Uploaded by

yukthiabhinav
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)
28 views

Matlab (Da)

Uploaded by

yukthiabhinav
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/ 14

NAME- M.

YUKTHI ABHINAV
REG. NO- 24BLC1118
SLOT: F2+TF2

(1) MAXIMA AND MINIMA OF FUNCTIONS OF TWO


VARIABLES.
CODE-
% Local maxima and minima for two variables
clc
clear all
close all
format compact
%%
syms x y real
f =input('Enter the function f(x,y): ');
fx = diff(f,x)
fy = diff(f,y)
[ax ay] = solve(fx,fy)
fxx = diff(fx,x)
D = fxx*diff(fy,y) - diff(fx,y)^2%ln-m^2

%% Collecting critical points

r=1;
for k=1:1:size(ax)
if ((imag(ax(k))==0)&&(imag(ay(k))==0))
ptx(r)=ax(k);
pty(r)=ay(k);
r=r+1;
end
end

%% Visulalizing the function

a1=max(double(ax))
a2=min(double(ax))
b1=max(double(ay))
b2=min(double(ay))
ezsurf(f,[a2-.5,a1+.5,b2-.5,b1+.5])
colormap('summer');
shading interp
hold on

%% Finding the maximum and minimum values of the function and their
visulaization

for r1=1:1:(r-1)
T1=subs(subs(D,x,ptx(r1)),y,pty(r1))
T2=subs(subs(fxx,x,ptx(r1)),y,pty(r1))
if (double(T1) == 0)
sprintf('The point (x,y) is (%d,%d) and need further investigation',
double(ptx(r1)),double(pty(r1)))
elseif (double(T1) < 0)
T3=subs(subs(f,x,ptx(r1)),y,pty(r1))
sprintf('The point (x,y) is (%d,%d) a saddle point',
double(ptx(r1)),double(pty(r1)))
plot3(double(ptx(r1)),double(pty(r1)),double(T3),'b.','markersize',30);
else
if (double(T2) < 0)
sprintf('The maximum point(x,y) is (%d, %d)',
double(ptx(r1)),double(pty(r1)))
T3=subs(subs(f,x,ptx(r1)),y,pty(r1))
sprintf('The value of the function is %d', double(T3))
plot3(double(ptx(r1)),double(pty(r1)),double(T3),'r+','markersize',30);
else
sprintf('The minimum point(x,y) is (%d, %d)',
double(ptx(r1)),double(pty(r1)))
T3=subs(subs(f,x,ptx(r1)),y,pty(r1))
sprintf('The value of the function is %d', double(T3))
plot3(double(ptx(r1)),double(pty(r1)),double(T3),'m*','markersize',30);
end
end
end

EXAMPLE1-
Input:
Enter the function: = 2x3+xy^2+5x2+y2
Output:
Enter the function f(x,y): 2*x^3+x*y^2+5*x^2+y^2
a1 =
0
a2 =
-1.6667
b1 =
2
b2 =
-2
ans =
'The minimum point(x,y) is (0, 0)' T3 =
0
ans =
'The value of the function is 0' T3 =
3
ans =
'The point (x,y) is (-1,-2) a saddle point' T3 =
3
ans =
'The point (x,y) is (-1,2) a saddle point' ans =
'The maximum point(x,y) is (-1.666667e+00, 0)' T3 =
125/27
ans =
'The value of the function is 4.629630e+00'

EXAMPLE2-
Input:
Enter the function: f(x,y)=2(x2-y2)-x4+y4
Output:
Enter the function f(x,y): 2*(x^2-y^2)-x^4+y^4
a1 =
1
a2 =
-1
b1 =
1
b2 =
-1
T3 = 0
ans =
'The point (x,y) is (0,0) a saddle point' ans =
'The maximum point(x,y) is (-1, 0)' T3 =
1
ans =
'The value of the function is 1'
ans =
'The maximum point(x,y) is (1, 0)' T3 =
1
ans =
'The value of the function is 1' ans =
'The minimum point(x,y) is (0, -1)' T3 =
-1
ans =
'The value of the function is -1' ans =
'The minimum point(x,y) is (0, 1)' T3 =
-1
ans =
'The value of the function is -1' T3 =
0
ans =
'The point (x,y) is (-1,-1) a saddle point' T3 =
0
ans =
'The point (x,y) is (1,-1) a saddle point' T3 =
0
ans =
'The point (x,y) is (-1,1) a saddle point' T3 =
0
ans =
'The point (x,y) is (1,1) a saddle point'
(2)EVALUATING VOLUME UNDER THE SURFACES
CODE for example 1-
clc
clear all
syms x y z
vol=8*int(int(sqrt(1-x^2-y^2),y,0,sqrt(1-x^2)),x,0,1)
viewSolid(z,0+0*x*y,sqrt(1-x^2-y^2),y,0+0*x,sqrt(1-x^2),x,0,1);
axis equal; grid on;

EXAMPLE1- Set up a double integral to find the volume of a sphere


of unit radius.
Solution:
Let the sphere be x 2+ y 2+ z 2=1 . We know that due to the symmetry the
volume of the sphere is 8 times its volume in the first octant. Thus
we setup a double integral to find the volume below the surface of
the sphere in the first octant only and write the total volume as:

1 √1− x2
V =8∫ ∫ √1−x 2− y 2 dy dx .
0 0

Output:
vol =

(4*pi)/3

0.8

0.6
z

0.4

0.2

0
0
1
0.5
0.5
1 0
y
x

EXAMPLE2-Find the volume of the solid that lies under the


paraboloid z=x 2 + y 2 and above the region D in the xy -plane bounded
by the lines y=2 x and the parabola y=x 2
Solution:
The double integral for this problem can be setup as:
4 √y
∫ ∫ ( x2 + y 2 ) dx dy
0 y/ 2

CODE for example 2-


clc
clear all
syms x y z
vol = int(int(x^2+y^2, x,y/2,sqrt(y)), y, 0, 4)
viewSolidone(z,0+0*x*y,x^2+y^2,x,y/2,sqrt(y),y,0,4);
grid on;
Output:
vol =

216/35

20

15

10
z

5 0
1
0 2
0 3
0.5
1
1.5 4
2 x
y

Converting Cartesian to polar coordinates

(3)EVALUATING TRIPLE INTEGRAL

EXAMPLE1-

Evaluate the iterated integral

Matlab code
syms x y z
sol = int(int(int(6*x*z,y,0,x+z),x,0,z),z,0,1)
EXAMPLE2-

Evaluate the triple integral ∭ 6 xy dV , where E lies


E

under the plane z =1+x+y and above the region in


the xy-pane bounded by the curves y= √ x , y=0 and
x=1.

Sol

Matlab code
syms x y z
sol = int(int(int(6*x*y,z,0,1+x+y),y,0,sqrt(x)),x,0,1)
viewSolid(z,0+0*x*y,1+x+y,y,0+0*x,sqrt(x),x,0,1);
axis equal; grid on;

(4)EVALUATING GRADIENT, CURL AND DIVERGENCE

EXAMPLE1-

Find the gradient vector field of (𝑥, 𝑦) = 𝑥2𝑦 − 𝑦3 . Plot the


gradient vector field together with a contour map of f. How
are they related?
MATLAB Code:
clc clear all
syms x y
f=input( 'enter the function f(x,y):'); F=gradient(f)
P = inline(vectorize(F(1)), 'x', 'y');
Q = inline(vectorize(F(2)), '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])
Output:
Command Window:

enter the function f(x,y): x^2*y-y^3

Inference:
The gradient vectors are orthogonal to the contours.

EXAMPLE2-
Find (a) the curl and (b) the divergence of the vector field.
Matlab code
clc clear all
syms x y z real
F=input( 'enter the vector as i, j and k order in vector form:')
curl_F = curl(F, [x y z])
div_F = divergence(F, [x y z])
Output:

enter the vector as i,j and k order in vector form: [x^2*y*z


x*y^2*z x*y*z^2]
F=
[x^2*y*z, x*y^2*z, x*y*z^2] curl_F =

x*z^2 - x*y^2 y*x^2 - y*z^2


- z*x^2 + z*y^2 div_F =
6*x*y*z

You might also like