Untitled

 avatar
unknown
plain_text
2 years ago
14 kB
13
Indexable
Draw a circle with centre (1, 3)
MATLAB Code:
clc
clear all;
close all;
t = linspace(0, 2*pi, 101);
x = 1 +2*cos(t);
y = 3 +2*sin(t);
plot(x,y,'r.')
axis equal
xlabel('x-axis')
ylabel('y-axis')
title('Circle')


Example 2:
Draw the graph by using without hold on function
MATLAB Code:
y = linspace(-10,10,1000)
plot(y,cos(y),'b.',y,cos(2*y),'g.')
xlabel('x axis')
ylabel('y axis')
legend('cos(x)','cos(2x)','location','northeast')


Example 3:
Draw the surface by using plot3
MATLAB Code:
t=linspace(0,2*pi,500);
x=cos(t);
y=sin(t);
z=sin(5*t);
comet3(x,y,z)
plot3(x,y,z,'g*','markersize',7)
xlabel('x-axis')
ylabel('y-axis')
zlabel('z-axis')
title('3D Curve')


Example 4:
Draw the four curves sinx, cosx, e-x
, sin3x in one window
MATLAB Code:
clc
clear all
x=0:.1:2*pi;
subplot(2,2,1);
plot(x,sin(x),'b*');
title('sin(x)')
subplot(2,2,2);
plot(x,cos(x),'r-o');
title('cos(x)')
subplot(2,2,3)
plot(x,exp(-x),'g.');
title('exp(-x)')
subplot(2,2,4);
plot(x,sin(3*x),'m-o');
title('sin(3x)')


Example 5:
Draw the surface by using ezsurf and ezplot
MATLAB Code:
syms x y
f = 2*(x^2+y^2)
colormapcool


Example 6:
Draw the ezplot for the function x^2+2*x-6
MATLAB Code:
syms x
y = x^2+2*x-6
ezplot(y)


Example 7:
MATLAB Code:
x=-1:.05:1;
y=-1:.05:1;
[x,y]=meshgrid(x,y);
z=x.*y.^2-x.^3
surf(x,y,z);
colormap spring
shading interp


Example 8:
Find

 ( )
 ( ) ( )

MATLAB Code:
syms x
f= x^2+cos(2*x)+4*sin(x)+exp(x)
diff(f,x) % differentiate f w.r.to x
diff(f,x,2)

Example 9:
∫ ()
MATLAB Code:
syms x
f= 3*x-x^2;
int(f,x,0,3)




Week 2:- Plotting of Curves and Surfaces


Example 1:
To Plot the Circle
Matlab Code
clc
clear all
syms r a b
r= input(‘Enter the radius of the circle’)
a= input(‘Enter the x coordinate of the center’)
b= input(‘Enter the y coordinates of the center’)
t = linspace(0, 2*pi, 100);
x = a+r*cos(t); y = b+r*sin(t);
axis equal plot(x, y)
xlabel(‘x-Coordinate’)
ylabel(‘x-Coordinate’)
title(‘(x − a) 2
+ (y − b) 2
= r 2


Example 2:
Multiple plots using Hold on Matlab Code
Matlab code
Clc
clear all
x = linspace(0, 1, 100);
plot(x, x.2 ,‘r’, ‘LineWidth’,2.0)
hold on
plot(x, cos(x), ‘g’, ‘LineWidth’,2.0)
hold on
plot(x, sin(x),‘b’, ‘LineWidth’,2.0)
hold on
plot(x, exp(x),‘c’, ‘LineWidth’,2.0)
legend(‘x 2 ’, ‘cos(x)’ , ‘sin(x)’ , ‘ex
’)


Example 3:
Multiple plots without command “hold on” Matlab Code
Matlab code
clc
clear all
x = linspace(0, 1, 200);
plot( x, sin(x), x, cos(x), x, x.3
, x, tan(x), ’LineWidth’,2.0)
legend(‘sin(x)’,‘cos(x)’,‘x3
 ’,‘tan(x)’)


Example 4:
Multiple plots using “subplot ”
Matlab code
clc
clear all
x=0:0.1:2*pi;
subplot(2,2,1)
plot(x,sin(x)); title(‘sin(x)’)
subplot(2,2,2)
plot(x,cos(x),’r-*’); title(’cos(x)’)
subplot(2,2,3)
plot(x,exp(−x),’go’); title(’e-x
’)
subplot(2,2,4);
plot(x,sin(3 ∗ x),’ms’) title(‘sin(3x)’)


Example 5:
Graph of the curve using “ezplot ”
Matlab code
clc
clear all
syms x
f=sin(2*x)+cos(3*x)
figure(1)
ezplot(f)
figure(2)
ezplot(f,[0,3])


Example 6:
Graph of a curve and its tangent line in the neighbourhood D of a point.
Matlab code
Clc
clear all
syms x
y=input(’enter the function f in terms of x:’)
x1 = input(’Enter x value at which tangent : ’);
D=[x1-2 x1+2]
ezplot(y,D)
hold on
yd = diff(y,x);
slope = subs(yd,x,x1);
y1 = subs(y,x,x1);
plot(x1,y1,’ko’)
Tgtline = slope*(x-x1)+y1




Week 3: Function and its derivatives

Example 1:
To Plot the function and its derivatives
Matlab code
clc
clear all
syms x real
f= input(‘Enter the function f(x):’);
fx= diff(f,x)
fxx= diff(fx,x)
D = [0, 5];
l=ezplot(f,D)
set(l,‘color’,‘b’);
hold on
h=ezplot(fx,D); s
et(h,‘color’,‘r’);
e=ezplot(fxx,D);
set(e,‘color’,‘g’);
legend(‘f’,‘ fx,‘fxx’)
legend(‘Location’,‘northeastoutside’)


Example 2:
To find the maxima and minima of the single variable function and visualize it
Matlab code
clc
clear all
syms x real
f= input(‘Enter the function f(x):’);
fx= diff(f,x); fxx= diff(fx,x);
c = solve(fx)
c=double(c);
for i = 1:length(c)
T1 = subs(fxx, x ,c(i) );
T1=double(T1);
T3= subs(f, x, c(i));
T3=double(T3);
if (T1==0)
sprintf(‘The inflection point is x = %d’,c(i))
else if (T1 < 0)
sprintf(‘The maximum point x is %d’, c(i))
sprintf(‘The maximum value of the function is %d’, T3)
else sprintf(‘The minimum point x is %d’, c(i))
sprintf(‘The minimum value of the function is %d’, T3)
end
end
cmin = min(c);
cmax = max(c); D = [cmin-2, cmax+2];
ezplot(f,D)
hold on
plot(c(i), T3, ‘g*’, ‘markersize’, 15);
end


Example 4:
To find the area of the regions enclosed by curves and visualize it.
Matlab code
clc
clear
syms x
y1=input(‘ENTER the upper curve as a function of x : ’);
y2=input(‘ENTER the lower curve as a function of x : ’);
t=solve(y1-y2);
t=double(t);
A=int(y1-y2,t(1),t(2))
D=[t(1)-0.2 t(2)+0.2];
ez1=ezplot(y1,D);
set(ez1,‘color’,‘r’)
hold on
ez2=ezplot(y2,D);
set(ez2,‘color’,‘g’)
xv = linspace(t(1),t(2));
y1v =subs(y1,x,xv);
y2v = subs(y2,x,xv);
x = [xv,xv]; y = [y1v,y2v];
fill(x,y,‘b’)



Week 4:- Volume of the solid of revolution

Example 1:
Find the volume of the solid generated by revolving the region bounded by y = √ x, 0 ≤ x ≤ 4
about the line y = 1.
Matlab code
clc
clearvars
syms x;
f = input('Enter the function: ');
fL = input('Enter the interval on which the function is defined: ');
yr = input('Enter the axis of rotation y = c (enter only c value): ');
iL = input('Enter the integration limits: ');
Volume = pi*int((f-yr)^2,iL(1),iL(2));
disp(['Volume is: ', num2str(double(Volume))])
fx = inline(vectorize(f));
xvals = linspace(fL(1),fL(2),201);
xvalsr = fliplr(xvals);
xivals = linspace(iL(1),iL(2),201);
xivalsr = fliplr(xivals);
xlim = [fL(1) fL(2)+0.5];
ylim = fx(xlim);
figure('Position',[100 200 560 420])
subplot(2,1,1)
hold on;
plot(xvals,fx(xvals),'-b','LineWidth',2);
fill([xvals xvalsr],[fx(xvals) ones(size(xvalsr))*yr],[0.8 0.8 0.8],'FaceAlpha',0.8)
plot([fL(1) fL(2)],[yr yr],'-r','LineWidth',2);
legend('Function Plot','Filled Region','Axis of Rotation','Location','Best');
title('Function y=f(x) and Region');
set(gca,'XLim',xlim)
xlabel('x−axis');
ylabel('y−axis');
subplot(2,1,2)
hold on;
plot(xivals,fx(xivals),'-b','LineWidth',2);
fill([xivals xivalsr],[fx(xivals) ones(size(xivalsr))*yr],[0.8 0.8 0.8],'FaceAlpha',0.8)
fill([xivals xivalsr],[ones(size(xivals))*yr -fx(xivalsr)+2*yr],[1 0.8 0.8],'FaceAlpha',0.8)
plot(xivals,-fx(xivals)+2*yr,'-m','LineWidth',2);
plot([iL(1) iL(2)],[yr yr],'-r','LineWidth',2);
title('Rotated Region in xy−Plane');
set(gca,'XLim',xlim)
xlabel('x-axis');
ylabel('y-axis');
[X,Y,Z] = cylinder(fx(xivals)-yr,100);
figure('Position',[700 200 560 420])
Z = iL(1) + Z.*(iL(2)-iL(1));
surf(Z,Y+yr,X,'EdgeColor','none','FaceColor','flat','FaceAlpha',0.6);
hold on;
plot([iL(1) iL(2)],[yr yr],'-r','LineWidth',2);
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
view(22,11);




Week 5:- Evaluating maxima and minima of functions of two variables

Example 1:
Obtain the maximum and minimum values of f(x,y) = 2(x2
-y
2
)- x
4
+y4
Matlab code
clc;
clear all;
close all;
syms x y
f(x,y) = 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;
figure
fsurf(f);
legstr = {'Function plot'};
for i=1:size(ax)
T1 = D(ax(i),ay(i));
T2 = r(ax(i),ay(i));
T3 = f(ax(i),ay(i));
if (double(T1)==0)
sprintf('At (%f,%f) further investigation is required',ax(i),ay(i))
legstr = [lrgstr,{'case of futher investigtation'}];
mkr = 'bv'; %marker
elseif (double(T1)<0)
sprintf('The point(%f,%f) is a saddle point',ax(i),ay(i))
legstr = [legstr,{'The case of further investigation'}];
mkr = 'ko';
else
if (double(T2)<0)
sprintf('The maximum value of the function is f(%f,%f) = %f', ax(i),ay(i),T3)
legstr = [legstr,{'Maximum value of the function'}]; %updating legend' mkr = 'g+';
else
sprintf('The minimum value of the function is f(%f,%f) = %f', ax(i),ay(i),T3)
legstr = [legstr,{'Minimum value of the function'}]; %updating legend' mkr = 'r*';
end
end
hold on
plot3(ax(i),ay(i),T3,mkr,'LineWidth',4);
end
legend(legstr,'Location','Best');




Week 6:- Constrained Optimization using the method of Lagrange’s Multipliers

Example 1:
Find the minimum of f(x, y) = x 2 + y 2 subject to the constraint x + y = 10
Matlab code
clc
clearvars
syms x y L
f = input('Enter the function f(x,y): ');
g = input('Enter the constraint function g(x,y): ');
F = f + L*g;
gradF = jacobian(F,[x,y]);
[L,x1,y1] = solve(g,gradF(1),gradF(2),'Real',true);
x1 = double(x1); y1 = double(y1);
xmx = max(x1); xmn = min(x1);
ymx = max(y1); ymn = min(y1);
range = [xmn-3 xmx+3 ymn-3 ymx+3];
ezmesh(f,range);hold on; grid on;
h = ezplot(g,range); set(h,'LineWidth',2);
tmp = get(h,'contourMatrix');
xdt = tmp(1,2:end);
ydt = tmp(2,2:end);
zdt = double(subs(f,{x,y},{xdt,ydt}));
plot3(xdt,ydt,zdt,'r','LineWidth',2);axis(range);
for i = 1:numel(x1)
G(i) = subs(f,[x,y],[x1(i),y1(i)])
plot3(x1(i),y1(i),G(i),'*k','MarkerSize',20);
end
title('Constrained Maxima/Minima')


Example 2:
Find the maximum and minimum distances from the origin to the curve 3x 2+4xy+6y 2−140.
Matlab code
clc
clearvars
syms x y z L
f = input('Enter the function f(x,y,z): ');
g = input('Enter the constraint function g(x,y,z): ');
F = f + L*g;
gradF = jacobian(F,[x,y,z]);
[L,x1,y1,z1] = solve(g,gradF(1),gradF(2),gradF(3));
Z = [x1 y1 z1];
disp('[x y z]=')
disp(Z)




Week 7:- Evaluating Volume Under surfaces
Example 1:
To find ∫ ∫ dydx
Matlab code
clc
clear all
close all
syms x y z
int(int((x+y)/4,y,x/2,x),x,1,2)
viewSolid(z,0+0*x+0*y,(x+y)/4,y,x/2,x,x,1,2)


Example 2
To find the volume of the prism whose base is the triangle in the xy plane bounded by the x-axis
and the lines y = x and x = 1 and whose top lies in the plane z = f(x,y) = 3-x-y. The limits of
integration here are y = 0 to 1 while x = y to 1
Matlab code
clc
clear all
close all
syms x y z
int(int(3-x-y,x,y,1),y,0,1)
viewSolid(z,0+0*x+0*y,3-x-y,x,y,1,y,0,1)


Example 3
Evaluate the integral ∫ ∫ ( ) by changing the order of integration. As per the
given limits of integration x=0 to 2 while y = x
2
to 2x
Matlab code
clc
clear all
close all
syms x y z
int(int(4*x+2,y,x^2,2*x),x,0,2)
viewSolid(z,0+0*x+0*y,4*x+2,y,x^2,2*x,x,0,2)


Example 4:
Evaluate ∬ dA where R = ,(x,y)|0≤x≤2, 1≤y≤2-
Matlab code
clc
clear all
close all
syms x y z
viewSolid(z,0+0*x+0*y,x-3*y^2,1+0*x,2+0*x,x,0,2)
int(int(x-3*y^2,y,1,2),x,0,2)

Example 5:Evaluate ∬ ( ) dA where R = *1,2+x*0,π+
Matlab code
clc
clear all
close all
syms x y z
viewSolid(z,0+0*x+0*y,y*sin(x*y),x,1+0*y,2+0*y,y,0,pi)
int(int(y*sin(x*y),x,1,2),y,0,pi)

Example 6:
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 = 2x and y = x^2
Matlab code
clc
clear all
close all
syms x y z
int(int(x^2+y^2,x,y/2,sqrt(y)),y,0,4)
viewSolid(z,0+0*x+0*y,x^2+y^2,x,y/2,sqrt(y),y,0,4)



Week 8:- Evaluating Triple Integrals

Example 1:
Find the volume of the region D enclosed by the surfaces z = x2
+ 3y2
and z = 8 – x
2
– y
2
.
Matlab code
clear
clc
syms x y z
xa = -2;
xb = 2;
ya = -sqrt(2-x^2/2);
yb = sqrt(2-x^2/2);
za = x^2+3*y^2;
zb = 8-x^2-y^2;
I = int(int(int(1+0*z,z,za,zb),y,ya,yb),x,xa,xb)
viewSolid(z,za,zb,y,ya,yb,x,xa,xb)

Example 2:
Find the volume of the region cut from the cylinder x2
+ y2
= 4 by the plane z = 0 and the plane
x + z = 3
Matlab code
clear
clc
syms x y z
ya = -2;
yb = 2;
xa = -sqrt(4-y^2);
xb = sqrt(4-y^2);
za = 0+0*x+0*y;
zb = 3-x-0*y;
I = int(int(int(1+0*z,z,za,zb),x,xa,xb),y,ya,yb)
viewSolid(z,za,zb,x,xa,xb,y,ya,yb


Example 3:
Find the volume of the region in the first octant bounded by the coordinate planes, the plane y
= 1 -x, and the surface z = cos(πx/2), 0≤x≤1.
Matlab code
clear
clc
syms x y z real
xa = 0;
xb = 1;
ya = 0+0*x;
yb = 1-x;
za = 0*x+0*y
zb = cos(pi*x/2)+0*y;
I = int(int(int(1+0*z,z,za,zb),y,ya,yb),x,xa,xb)
viewSolid(z,za,zb,y,ya,yb,x,xa,xb)


Week 9:- Gradient, Curl and Divergence

Example 1:
Find the Gradient of the function f = 2xy
Matlab code
clc
clear all
close all
syms x y
f = input('Enter the function f(x,y):')
grad = gradient(f,[x,y])
P(x,y) = grad(1);Q(x,y)= grad(2);
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
fcontour(f,[-2,2])


Example 2:
Find the divergence of the vector field F = xy2
i + x2
j and visualise it.
Maltab code
clear all
close all
clc
syms x y
f = input("Enter the 2D vector function in the form [f1,f2]:");
div(x,y) = divergence(f,[x,y])
P(x,y) = f(1);Q(x,y)= f(2);
x = linspace(-4,4,20);y = x;
[X,Y] = meshgrid(x,y);
U = P(X,Y);V = Q(X,Y);
figure
pcolor(X,Y,div(X,Y))
shading interp
hold on;
quiver(X,Y,U,V,1)
axis on
xlabel('x');ylabel('y')
hold off;
title("Vector field of F(x,y)=[f1,f2]");


Example 3:
Find and visualise the curl of a vector function F = -yi + xj
Matlab code
clear all
close all
clc
syms x y z
f = input("Enter the 2D vector function in the form [f1,f2,f3]:");
P(x,y,z) = f(1);
Q(x,y,z)= f(2);
R(x,y,z)=f(3);
crl = curl(f,[x,y,z])
C1(x,y,z)=crl(1);
C2(x,y,z)=crl(2);
C3(x,y,z)=crl(3);
x = linspace(-4,4,20);
y=x;
z=x;
[X,Y,Z] = meshgrid(x,y,z);
U = P(X,Y,Z);
V = Q(X,Y,Z);
W = R(X,Y,Z);
CR1=C1(x,y,z);
CR2=C2(x,y,z);
CR3=C3(x,y,z);
figure;
subplot(1,2,1);
quiver3(X,Y,Z,U,V,W);
title('3-D View of vector field');
subplot(1,2,2);
quiver3(X,Y,Z,CR1,CR2,CR3);
title('3-D View of CURL');
Editor is loading...