# Untitled

unknown
plain_text
2 years ago
9.1 kB
1
Indexable
Never
```#curl

clear
clc
syms x y z
f=input("enter the 3d vectir 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('3d view of vector field');
subplot(1,2,2);
quiver3(X,Y,Z,CR1,CR2,CR3);
title("3d view of curl");

# divergence

clear
clc;

syms x y
f=input("enter the funtion [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);
hold on
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])

#greens

clear
clc
syms x y t
F=input("ENTER the vector function M(x,y)I+N(x,y) in the form [M N] :");
M(x,y)=F(1);
N(x,y)=F(2);
r=input("Enter the parametric form of the curve 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 the limits of integration for t [t1,t2] :");
t1=T(1);
t2=T(2);
LHS=int(F1,t,t1,t2);
yL=input("Enter the lmits for y in terms of x: [y1,y2]:");
xL=input("enter the limits for x as constanasts : [x1,x2]:");
y1=yL(1);
y2=yL(2);
x1=xL(1);
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 Greens theorem is verified .');
end

# alngarqnegs

clc
clear all
syms x y z L
f=x*x+y*y+z*z;
g=3*x*x+4*x*y+6*y*y-140;
F=f+L*g;
j=jacobian(F,[x y z]);
s=solve(g,j(1),j(2),j(3));
p=double([s.x,s.y,s.z]);
h1=p(:,1);
h2=p(:,2);
h3=p(:,3);
X=[h1];
Y=[h2];
Z=[h3];
st_pts=table([X,Y,Z])
[n,m]=size(st_pts);
for i=1:n
F(i)=subs(f,{x,y,z},{s.x(i),s.y(i),s.z(i)});
end
if n>1
F_max=max(F);
disp("f_max");
disp(F_max);
F_min=min(F);
disp("f_min")
disp(F_min);
end

# line integral

clc
clear all
syms x y t
f=input("enter the function in form [x,y] ");
r=input("enter x,y in the form paramateric form");
g=input("enter limits of t");
a=g(1);
b=g(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')

#
%%To find the maxima and minima of the single variable function and visualize

clc
clear all
syms x real
f=9*x-6*x*x+x^3+1
fx= diff(f,x)
fxx= diff(fx,x)
c = solve(fx);
%z=size(c)
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

%%Definite Integrals and its applications
%%To find the area of the regions enclosed by curves and visualize it.

clc
clear all
syms x y real
y1=-x*x+4*x;
y2=x*x;
fg=figure;
ax=axes;
t=solve(y1-y2);
k=double(t)
n=length(k)
m1=min(k)
m2=max(k)
ez1=ezplot(y1,[m1-1,m2+1]);
hold on
ez2=ezplot(y2,[m1-1,m2+1]);
TA=0;
A=int(y1-y2,t(1),t(2));
TA=abs(A)
x1 = linspace(k(1),k(2));
yy1 =subs(y1,x,x1);
yy2 = subs(y2,x,x1);
x1 = [x1,fliplr(x1)];
yy = [yy1,fliplr(yy2)];
fill(x1,yy,'g')

clc
clear all
syms x y real
y1=-x*x+4*x;
y2=x*x;
fg=figure;
ax=axes;
t=solve(y1-y2);
k=double(t)
n=length(k)
m1=min(k)
m2=max(k)
ez1=ezplot(y1,[m1-1,m2+1]);
hold on
ez2=ezplot(y2,[m1-1,m2+1]);
TA=0;
if n>2
for i=1:n-1
A=int(y1-y2,t(i),t(i+1));
TA= TA+abs(A)
x1 = linspace(k(i),k(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
else
A=int(y1-y2,t(1),t(2));
TA=abs(A)
x1 = linspace(k(1),k(2));
yy1 =subs(y1,x,x1);
yy2 = subs(y2,x,x1);
x1 = [x1,fliplr(x1)];
yy = [yy1,fliplr(yy2)];
fill(x1,yy,'g')
end

#
% To find Maximum and Minimum values (Extreme values) of a function f (x, y) using
% MATLAB.
clc
clear
syms x y
f(x,y)=2*(x^2-y^2)-x^4+y^4;
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');

% Volume of the Solid of Revolution

clc
clear all
syms x
f = x^2+2;
fL = 0:10; %Enter the interval on which the function is defined
yr = 3; %the axis of rotation y = c
iL = 0:2; %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');
[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);

clc
clear all
% 1. Matrix operations
A=[1 2;3 4];
B=[4 5;8 12];
C=A+B
D=A*B
E=inv(A)
F=B'
% H=A.^2
I= det(A)
%2.Circle with centre(1,3)
t = linspace(0, 2*pi);
x = 1 +2*cos(t);
y = 3 +2*sin(t);
plot(x,y)
axis equal
%3.plotting three functions without hold on
x = linspace(0,1,101)
plot(x,x.^3,'r+',x,sin(x),'b-',x,exp(x),'g.')
%4. Solving system of linear equations, say, 4x+5y=7,7x+8y=21
% In general,
%X = A\B solves for X in A*X = B and
% X = B/A solves for X in X*A = B.
A=[4 5;7 8]
b=[7 21]'
x=A\b
%5.solving quadratic equations
syms ('x')
solve(2*x^2+5*x+12)
%6.ezplotting
syms x
f=sin(2*x)+cos(3*x)
ezplot(f)
%7.subplot
x=0:.1:2*pi;
subplot(2,2,1);
plot(x,sin(x));
subplot(2,2,2);
plot(x,cos(x));
subplot(2,2,3)
plot(x,exp(-x));
subplot(2,2,4);
%8.symbolic differentiation
syms x y
f =x^2+2*x*y+y*sin(x)
diff(f,x)
diff(f,y)
%9.Symbolic integration
syms x
f=x^3+3*x^2+5*x+12
int(f,x,0,2)

%%To Plot the Circle
syms r a b
r= 5;
a= 6;
b= 4;
t = linspace(0, 2*pi);
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 on Matlab Code
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('x2', 'cos(x)' , 'sin(x)' , 'ex')
%%Multiple plots without command \hold on" 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)','x^3', 'tan(x)')
%%Graph of a curve and its tangent line in the neighbourhood D of a point.
syms x
y=x^2;
x1 =5;
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')
hold on
Tgtline = slope*(x-x1)+y1
ezplot(Tgtline, D)
%To Plot the function and its derivatives
clc
clear all
syms x real
f= 3*x^2-4;
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')

```