Matlab 5b
Matlab 5b
Lab Assignment
Submitted To:
Padma R
Submitted by:
Aditi Prashar
20BCE0426
EXPERIMENT 5A
Ques 1. Draw the two dimensional vector field for the vector 2xi 3yj.
Code:
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]');
Input:
Output:
Ques2. Find the Gradient of the function f=x 2y3-4y.
Code:
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])
Input:
Output:
Ques3. Find the divergence of a vector field f [ xy,2x] .
Code:
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]');
Input:
Output:
Ques4. Visualize the curl of a vector function f [yz,3zx,z]
Code:
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); %Components of vector f
crl=curl(f,[x,y,z]) %Calculating curl
C1(x,y,z)=crl(1);C2(x,y,z)=crl(2);C3(x,y,z)=crl(3);%Components of curl(f)
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-D view of curl');
Input:
Output:
EXPERIMENT 5B
Ques 1. Find the work done for the force 𝐹⃗(x,y,z)= yz𝑖⃗ + xz 𝑗⃗ + (xy+2z) 𝑘⃗⃗ along the line segment from
(1,0,-2) to (4,6,3).
Code:
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);
S=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')
Input:
Output:
Ques 2. Find the work done for the force 𝐹⃗(x,y,)= x2 𝑖⃗ + y2 𝑗⃗ along the arc of the parabola y =2x2 from
(-1,2) to (2,8).
Code:
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);
S=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')
Input:
Output: