Untitled

 avatar
user_2143278
plain_text
2 years ago
8.8 kB
53
Indexable
lagrange 2 variable
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); xmn = min(x1); % Finding max and min of x-coordinates for plot range
ymx = max(y1); ymn = min(y1); % Finding max and min of y-coordinates for plot range
range = [xmn-3 xmx+3 ymn-3 ymx+3]; % Setting 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')

langrange 3 variable
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)

greenstheorem

clear
clc
syms x y t
F=input('Enter the vector function M(x,y)i+N(x,y)j in the form [M N]:');
M(x,y)=F(1); N(x,y)=F(2);
r=input('Enter the parametric form of the curbe C as [r1(t) r2(t)]:');
    r1=r(1);r2=r(2) 
    P=M(r1,r2);Q=N(r1,r2);
    dr=diff(r,t);
    F1=sum([P,Q].*dr);
    T=input('Enter limits of integration for t [t1,t2]:');
    t1=T(1); t2=T(2);
    LHS=int(F1,t,t1,t2);
    yL=input('Enter limits for y in terms of x: [y1,y2]: ');
    xL=input('Entr limits for y as constants:[x1,x2]:');
    y1=yL(1);y2=yL(2); x1=xL(2);x2=xL(2);
    F2=diff(N,x)-diff(M,y);
    
    RHS=int(int(F2,y,y1,y2),x,x1,x2);
    if(LHS==RHS)
        disp('LHS of Greens theorem=')
        disp(LHS)
        disp('RHs of Greens theorem=')
        disp(RHS)
        disp('Hence dreens theorem is veriified.');
    end

extreme values
clc 
clear 
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 Legend
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=[legstr,{'Case of Further investigation'}]; 
mkr='ko'; 
elseif (double(T1)<0) 
sprintf('The point (%f,%f) is a saddle point', ax(i),ay(i)) 
legstr=[legstr,{'Saddle Point'}]; % updating Legend
mkr='bv'; % marker
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+';% marker
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*'; % marker
end
end
hold on
plot3(ax(i),ay(i),T3,mkr,'Linewidth',4); 
end
legend(legstr,'Location','Best'); 

lineintegral 2D
clc
clear all
syms x y t
f=input('Enter the components of 2D vector function[u,v]');
r=input('Enter x,y in parametric form');
I=input('Enter the limits of integration for t in the form [a,b]');
a=I(1);b=I(2);
dr=diff(r,t);
F=subs(f,{x,y},r);
Fdr=sum(F.*dr);
I=int(Fdr,t,a,b)
P(x,y)=f(1);Q(x,y)=f(2);
x1=linspace(-2*pi,2*pi,10);y1=x1;
[X,Y]=meshgrid(x1,y1);
U=P(X,Y); V=Q(X,Y);
quiver(X,Y,U,V,1.5)
hold on 
t1=linspace(0,2*pi);
x=subs(r(1),t1);y=subs(r(2),t1);
plot(x,y,'r')


line integral 3D
clc
clear all
syms x y z t
f=input('Enter the components of 3D vector function [u,v,w]');
r=input('Enter x,y,z in parametric form');
I=input('Enter the limits of integration for t in the form [a,b]');
a=I(1); b=I(2);
dr=diff(r,t);
F=subs(f,{x,y,z},r);
Fdr=sum(F.*dr);
I=int(Fdr,t,a,b)
P(x,y,z)=f(1);Q(x,y,z)=f(2);R(x,y,z)=f(3);
x1=linspace(0,1,10);y1=x1;z1=x1;
[X,Y,Z]=meshgrid(x1,y1,z1);
U=P(X,Y,Z);V=Q(X,Y,Z); W=R(X,Y,Z);
quiver3(X,Y,Z,U,V,W,1.5)
hold on 
t1=linspace(0,1,10);
x=subs(r(1),t1); y=subs(r(2),t1);z=subs(r(3),t1);
plot3(x,y,z,'r')



viewsolid 1
clc
clear all
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)


viewsolidone 1

clc
clear all
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)
viewSolidone(z,za,zb,x,xa,xb,y,ya,yb)

volume viewsolid

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)


volume viewsolidone

syms x y z
int(int(3-x-y,x,y,1),y,0,1)
viewSolidone(z,0+0*x+0*y,3-x-y,x,y,1,y,0,1)


circle

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


multiple plots using hold

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)','e^x')


multiple plots without using hold


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)','x^3','tan(x)')


multiple plots using subplots'


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



ezplot


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


tangent

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



derivatives


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);
set(h,'color’,‘r');
e=ezplot(fxx,D);
set(e,'color’,‘g');
legend('f’,‘fx’,‘fxx')
legend('Location’,‘northeastoutside')



maxima minima single variable


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


area under curve


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


curl


clear 
clc
syms x y z
f=input('Enter the 3D 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,10);
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- view of curl');


divergence


clear 
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 
hold off;
title('Vector field of F(x,y)=[f1,f2]');



gradient


clear 
clc
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])
Editor is loading...