Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

MATLAB RECORD EXP 1-6

Muhammed Riza 23BMH1088 SLOT L55-56

EXPERIMENT 1 – Functions of matlab


i) ezplot function
clc
clear all
syms x
f=sin(2*x)+cos(3*x)
figure(1)
ezplot(f)
figure(2)
ezplot(f,[0,3])

OUTPUT
ii) subplot function
clc
clear all
close all
x=0:0.1:2*pi;
subplot(2,2,1)
plot(x,sin(x));
subplot(2,2,2)
plot(x,cos(x),'r-*');
subplot(2,2,3)
plot(x,exp(x),'go');
subplot(2,2,4)
plot(x,sin(3*x),'ms');

OUTPUT

iii) Multiple plots using hold on function


clc
clear all
close all
x=linspace(0,1)
plot(x,x.*x,'r*')
hold on
plot(x,sin(x),'g.')
plot(x,exp(x),'m+')
legend('x^2','sin(x)','exp(x)')
OUTPUT

iv) Parametric plotting


clc
clear all
close all
t=linspace(0,2*pi,20)
x=3+2*cos(t);
y=4+2*sin(t);
plot(x,y,'k-*')
axis equal
xlabel('x')
ylabel('y')
title('graph of (x-3)^2+(y-4)^2=4')
legend('(x-3)^2+(y-4)^2=4')

OUTPUT
v) Multiple graphs without hold on function
y=linspace(-10,10,100)
plot(y,cos(y),'b.',y,cos(2*y),'g.')
xlabel('x-axis')
ylabel('y-axis')
legend('cos(x)','cos(2*x)','location','northeast')

OUTPUT

vi) plot3 function


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')

OUTPUT
vii) ezsurf function
a)
syms x y
f=2*(x^2+y^2)
ezsurf(f)
colormap cool

OUTPUT
f =

2*x^2 + 2*y^2

b)
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

OUTPUT

Experiment 2 – Plotting of curves and tangents


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')
tgt_Line=slope*(x-x1)+y1
h=ezplot(tgt_Line,D)
set(h,'color','r')

OUTPUT
Enter the function f in terms of x:
x*x

y =

x^2

Enter x value at which tangent:


2
D =

0 4

tgt_line =

4*x - 4

Experiment 3a – Differential plotting


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(1,'color','b');
hold on
h=ezplot(fx,D);
set(h,'color','r');
e=ezplot(fxx,D);
set(e,'color','g');
legend('f','fx','fxx')
legend('Location','northeast outside')

OUTPUT
Enter the function f(x):
4*x^2-2*x+6

fx =

8*x - 2
fxx =

Experiment 3b – Maxima and minima for single variables


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

OUTPUT
Enter the function f(x):
x^2-4*x+4

c =

ans =

'The minimum point x is 2'

ans =

'The minimum value of the function is 0'

cmin =

cmax =

D =

0 4

Experiment 4 – Area between curves


syms x y real
y1=input("Enter the first(f) curve:");
y2=input("Enter the second(g) curve:");
t=solve(y1-y2)
B=double(t)
n=length(B);
m1=min(B)
m2=max(B)
ex1=ezplot(y1,[m1-1,m2+1]);
hold on
Ta=0
ex2=ezplot(y2,[m1-1,m2+1]);
for i=1:n-1
A=int(y1-y2,t(i),t(i+1))
TA=Ta+abs(A)
x1=linspace(B(i),B(i+1))
yy1=subs(y1,x,x1)
yy2=subs(y2,x,x1)
x1=[x1,fliplr(x1)]
yy=[yy1,fliplr(yy2)]
fill(x1,yy,'g')
grid on
end

OUTPUT

Experiment 5 – Maxima and minima for 2 variables


clc
clear all
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 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];
vlim=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');
[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);

OUTPUT
Enter the function:x*x
Enter the interval on which the function is defined:[0,100]
Enter the axis of rotation y=c(enter only c value):1
Enter integration limits:[1,2]
Volume is:7.9587
Experiment 6 – Lagrange’s multiplier method
a)2 variables
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); %Solving only for real
x and y
x1=double(x1);
y1=double(y1);
xmx=max(x1); %Finding the max and min of x coordinates for plot
range
xmn=min(x1);
ymx=max(y1); %Finding the max and min of y coordinates for plot
range
ymn=min(y1);
range=[xmn-3 xmx+3 ymn-3 ymx+3]; %Setting the plot range
ezmesh(f,range);
hold on;
grid on;
h=ezplot(g,range);
set(h,'LineWidth',2);
tmp=get(h,'contourMatrix');
xdt=tmp(1,2:end); %Avoiding first x-data point
ydt=tmp(2,2:end); %Avoiding first y-data point
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')
OUTPUT
Enter the function f(x,y):

x^2+y^2
Enter the constraint function g(x,y):
x+y-10

G =

50

b)3 variables
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)

OUTPUT
Enter the function f(x,y,z): x^2+y^2+z^2
Enter the constraint function g(x,y,z): 3*x^2+6*y^2+4*x*y-140
[x y z]=
[ -2, -4, 0]
[ 2, 4, 0]
[-2*14^(1/2), 14^(1/2), 0]
[ 2*14^(1/2), -14^(1/2), 0]

You might also like