Matlab 1
Matlab 1
PRANEETH
COURSE CODE:MAT1001
COURSE NAME:CALCULUS FOR ENGINEERS-
EMBEDDED LAB
SLOT:L20+L21
REGD N0.:23BCE9683
clc
clear all
LAB-1
1)
syms x
v=x*(12-2*x)^2;
dv=diff(v,x)
roots=solve(dv==0)
ddv=diff(dv,x)
for i=1:length(roots)
fv=subs(ddv,roots(i));
if fv<0
max=subs(v,roots(i))
end
end
output:
dv = x*(8*x - 48) + (2*x - 12)^2
roots = 2 6
ddv = 24*x - 96
max = 128
2)
syms x y
a=75;
a=x*y;
%c=4*x+8*x+8*y+8*y
c=12*x+1200/x;
dc=diff(c,x)
solutions=solve(dc==0)
ddc=diff(dc,x)
for i=1:length(solutions)
ac=subs(ddc,solutions(i))
if ac<0
continue
else ac>0
x_val=solutions(i)
end
end
y=double(75/x_val)
Output:
dc = 12 - 1200/x^2
solutions = -10 10
ddc = 2400/x^3
ac = -12/5
ac = 12/5
x_val = 10
y = 7.5000
3)
v=100;
syms r h
a=2*pi*r*(100/(pi*r^2))+2*pi*r^2;
da=diff(a,r)
root=double(solve(da==0))
dda=diff(da,r)
for i=1:length(root)
if root(i)>0
r=double(root(i))
end
end
h=100/(pi*r^2)
Output:
da = 4*pi*r - 200/r^2
root =
2.5154 + 0.0000i
-1.2577 + 2.1784i
-1.2577 - 2.1784i
r = 2.5154
h = 5.0308
4)
% T=Tswim+Trun=>velocity=distance/time
% Tswim=y/5=>Trun=100-x/15,x^2+(50)^2=y^2
syms x
T=1/5*(x^2+2500)^(0.5)+1/15*(100-x);
dt=diff(T,x)
sol=double(solve(dt==0))
Output:
sol = 17.6777
LAB-2
5)
syms x
a1=int(x-x^2,0,1)
a2=abs(int(x-x^2,1,1.5))
a=a1+a2
output:
a1 = 1/6
a2 = 1/6
a = 1/3
syms x
a1=abs(int(x^2-x,0,1))
a2=abs(int(x^2-x,1,2))
a=a1+a2
output:
a1 = 1/6
a2 = 5/6
a=1
syms x
a1=abs(int(x^3-3*x,-1,0))
a2=abs(int(x^3-3*x,0,1))
a=a1+a2
output:
a1 = 5/4
a2 = 5/4
a = 5/2
syms x b
a1=int(-2*(exp(3*x)),0,b)
output:
a1 = 2/3 - (2*exp(3*b))/3
6)
x=1:1:3
f=x.^4-3*x.^3-4*x.^2+10
g=40-x.^2
plot(f,x)
hold on
plot(g,x)
legend("f=x^4-3*x^3-4*x^2+10","g=40-x^2")
syms x
f=x^4-3*x^3-4*x^2+10;
g=40-x^2;
a=int(g-f,x,1,3)
output:
x=1 2 3
f = 4 -14 -26
g = 39 36 31
a = 488/5
7)
a)
t=0:0.1:3
E=2*t.^2
C=9+t.^2
plot(E,t)
hold on
plot(C,t)
legend("E=2*t^2","C=9+t^2")
output:
b)
syms t
solve(2*t^2==9+t^2)
output:
ans = -3 3
c)
syms t
e=2*t^2;
c=9+t^2;
a=abs(int(e-c,t,0,3)
output:
a = 18
LAB-3
8)
syms x y
f=2*x^3+6*x*y^2-3*y^3-150*x;
fx=diff(f,x)
fy=diff(f,y)
fxx=diff(fx,x)
fxy=diff(fx,y)
fyy=diff(fy,y)
H=hessian(f)
DD=det(H)
[x_val,y_val]=solve(fx==0,fy==0)
c=[x_val(:),y_val(:)]
for i=1:length(c)
fxx1=subs(fxx,{x,y},{x_val(i),y_val(i)});
D=subs(DD,{x,y},{x_val(i),y_val(i)});
if D<0
fprintf('f has saddle point at (%0.4f,%0.4f)',x_val(i),y_val(i))
elseif D>0 && fxx1<0
v=subs(f,{x,y},{x_val(i),y_val(i)});
fprintf('f has local maximum at(%0.4f,%0.4f)=%0.4f',x_val(i),y_val(i),v)
elseif D>0 && fxx1>0
a=subs(f,{x,y},{x_val(i),y_val(i)});
fprintf('f has local minimum at (%0.4f,%0.4f)=%0.4f',x_val(i),y_val(i),a)
else
fprintf('Test fails at (%0.4f,%0.4f)',x_val(i),y_val(i))
end
end
output:
fy = - 9*y^2 + 12*x*y
fxx = 12*x
fxy = 12*y
H=
[12*x, 12*y]
x_val =
-5
-3
3
y_val =
-4
c=
[-5, 0]
[ 5, 0]
[-3, -4]
[ 3, 4]
9)
syms x y
f=x^3+y^3-3*x-12*y+20;
fx=diff(f,x)
fy=diff(f,y)
fxx=diff(fx,x)
fxy=diff(fx,y)
fyy=diff(fy,y)
H=hessian(f)
DD=det(H)
[x_val,y_val]=solve(fx==0,fy==0)
c=[x_val(:),y_val(:)]
for i=1:length(c)
fxx1=subs(fxx,{x,y},{x_val(i),y_val(i)});
D=subs(DD,{x,y},{x_val(i),y_val(i)});
if D<0
fprintf('f has saddle point at (%0.4f,%0.4f)',x_val(i),y_val(i))
elseif D>0 && fxx1<0
v=subs(f,{x,y},{x_val(i),y_val(i)});
fprintf('f has local maximum at(%0.4f,%0.4f)=%0.4f',x_val(i),y_val(i),v)
elseif D>0 && fxx1>0
a=subs(f,{x,y},{x_val(i),y_val(i)});
fprintf('f has local minimum at (%0.4f,%0.4f)=%0.4f',x_val(i),y_val(i),a)
else
fprintf('Test fails at (%0.4f,%0.4f)',x_val(i),y_val(i))
end
end
Output:
fx = 3*x^2 - 3
fy = 3*y^2 - 12
fxx = 6*x
fxy = 0
fyy = 6*y
H=
[6*x, 0]
[ 0, 6*y]
DD = 36*x*y
x_val =
-1
-1
y_val =
-2
-2
c=
[-1, -2]
[ 1, -2]
[-1, 2]
[ 1, 2]
10)
syms x y
f=x^3*y^2*(1-x-y);
fx=diff(f,x)
fy=diff(f,y)
fxx=diff(fx,x)
fxy=diff(fx,y)
fyy=diff(fy,y)
H=hessian(f)
DD=det(H)
[x_val,y_val]=solve(fx==0,fy==0)
c=[x_val(:),y_val(:)]
for i=1:length(c)
fxx1=subs(fxx,{x,y},{x_val(i),y_val(i)});
D=subs(DD,{x,y},{x_val(i),y_val(i)});
if D<0
fprintf('f has saddle point at (%0.4f,%0.4f)',x_val(i),y_val(i))
elseif D>0 && fxx1<0
v=subs(f,{x,y},{x_val(i),y_val(i)});
fprintf('f has local maximum at(%0.4f,%0.4f)=%0.4f',x_val(i),y_val(i),v)
elseif D>0 && fxx1>0
a=subs(f,{x,y},{x_val(i),y_val(i)});
fprintf('f has local minimum at (%0.4f,%0.4f)=%0.4f',x_val(i),y_val(i),a)
else
fprintf('Test fails at (%0.4f,%0.4f)',x_val(i),y_val(i))
end
end
Output:
fx = - x^3*y^2 - 3*x^2*y^2*(x + y - 1)
fy = - x^3*y^2 - 2*x^3*y*(x + y - 1)
H=
x_val =
1/2
y_val =
1/3
c=
[1/2, 1/3]
11)
syms x y z lambda
f=x^2+y^2+z^2;
g=1/x+1/y+1/z-1;
H=f+lambda*g
Hx=diff(H,x)
Hy=diff(H,y)
Hz=diff(H,z)
Hlambda=diff(H,lambda)
system=[Hx,Hy,Hz,Hlambda]
[x_val,y_val,z_val,lambda_val]=solve(system,[x y z lambda])
disp('The critical points are')
c=[x_val(:),y_val(:),z_val(:)]
D=subs(f,{x,y,z},{x_val,y_val,z_val})
minf=double(min(D))
output:
Hx = 2*x - lambda/x^2
Hy = 2*y - lambda/y^2
Hz = 2*z - lambda/z^2
system = [2*x - lambda/x^2, 2*y - lambda/y^2, 2*z - lambda/z^2, 1/x + 1/y + 1/z - 1]
x_val =
3/2 - (3^(1/2)*1i)/2
(3^(1/2)*1i)/2 + 3/2
y_val =
3/2 - (3^(1/2)*1i)/2
(3^(1/2)*1i)/2 + 3/2
z_val =
3^(1/2)*1i
-3^(1/2)*1i
3/2 - (3^(1/2)*1i)/2
3/2 - (3^(1/2)*1i)/2
(3^(1/2)*1i)/2 + 3/2
(3^(1/2)*1i)/2 + 3/2
lambda_val =
54
-3^(1/2)*6i
3^(1/2)*6i
-3^(1/2)*6i
-3^(1/2)*6i
3^(1/2)*6i
3^(1/2)*6i
c=
[ 3, 3, 3]
D=
27
2*((3^(1/2)*1i)/2 - 3/2)^2 - 3
2*((3^(1/2)*1i)/2 + 3/2)^2 - 3
fx = y - 64/x^2
fy = x - 64/y^2
fxx = 128/x^3
fxy = 1
fyy = 128/y^3
H=
[128/x^3, 1]
[ 1, 128/y^3]
DD = -(x^3*y^3 - 16384)/(x^3*y^3)
x_val =
- 2 - 3^(1/2)*2i
- 2 + 3^(1/2)*2i
y_val =
- 2 - 3^(1/2)*2i
- 2 + 3^(1/2)*2i
c=
[ 4, 4]
[- 2 - 3^(1/2)*2i, - 2 - 3^(1/2)*2i]
[- 2 + 3^(1/2)*2i, - 2 + 3^(1/2)*2i]
x=4
y=4
z=2
13)
syms x y z lambda
u = 400*x*y*z^2
g = x^2+y^2+z^2-1
H = u+lambda*g
Hx = diff(H,x)==0
Hy = diff(H,y)==0
Hz = diff(H,z)==0
Hlambda = diff(H,lambda)==0
system = [Hx Hy Hz Hlambda]
[x_val,y_val,z_val,lambda_val] = solve(system,[x y z lambda])
disp('critical points are')
c = [x_val(:),y_val(:),z_val(:)]
d = subs(u,{x,y,z},{x_val,y_val,z_val})
minu = min(d)
maxu = max(d)
output:
H = lambda*(x^2 + y^2 + z^2 - 1) + 400*x*y*z^2
Hx = 400*y*z^2 + 2*lambda*x == 0
Hy = 400*x*z^2 + 2*lambda*y == 0
Hz = 2*lambda*z + 800*x*y*z == 0
Hlambda = x^2 + y^2 + z^2 - 1 == 0
system =
14)
syms r theta
%z=3-x-y
%x^2+y^2=r^2=>x=rcos(theta),y=rsin(theta)
z=3-r*cos(theta)-r*sin(theta);
innerint=int(z*r,theta,0,2*pi)
outerint=int(innerint,r,0,1)
output:
innerint = 6*pi*r
outerint = 3*pi
15)
syms x y
f=x*y*(x+y);
innerint=int(f,x,y,y^(0.5))
outerint=int(innerint,y,0,1)
output:
outerint = 3/56
16)
syms r theta
innerint=int(r^3,r,0,cos(theta))
outerint=int(innerint,theta,0,2*pi)
output:
innerint = cos(theta)^4/4
outerint = (3*pi)/16
17)
syms theta r
innerint=int(r^2*exp(3)*cos(theta)*sin(theta),r,0,1)
outerint=int(innerint,theta,0,2*pi)
output:
innerint = (2826788018837635*sin(2*theta))/844424930131968
outerint = 0