0% found this document useful (0 votes)
15 views

MATLAB code Week 10 and 11

matlab codes for beginners week 10 & 11
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

MATLAB code Week 10 and 11

matlab codes for beginners week 10 & 11
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

GRADIENT, DIVERGENCE AND CURL

Aim:
• To write Matlab codes to visualize the vector field of 2-Dimension as well as 3-
Dimension.
• To find the gradient vector and visualize it with contour curves.
• To find divergence, curl and scalar potential

Mathematical form:
Draw the two dimensional vector field for the vector ⃗𝐹→ = ⃗𝑓⃗⃗⃗→ ( x , y) + ⃗𝑓⃗⃗⃗→ (x,y)
1 2

• Draw the three dimensional vector field for the vector ⃗𝐹→ = ⃗𝑓⃗⃗⃗→ ( x, y,z) + ⃗𝑓⃗⃗⃗→ (x,y,z)
1 2
+⃗𝑓⃗⃗⃗→
3 (x,y,z)
• Find the gradient vector for the following function F(x,y) at the point (x1,y1) .
let the given function be f(x,y). grad (f)=(∂f/∂x)𝚤→ +(∂f/∂y)𝚥→. Then [grad (f)] at (a,b) is
(∂f/∂x) (a,b) 𝚤→ + (∂f/∂y) (a,b)𝚥→.
• Find the directional derivative of the function F(x,y,z) in the direction of the vector 𝑉⃗→ =
V1𝚤→ + V2j + V 3 𝑘⃗→ the point (x1,y1,z1).
Let the given function be F(x,y,z). Find[ grad f] at (x1,y1,z1). Find the unit tangent
normal by ⃗⃗[⃗⃗𝑉
⃗→ /|𝑉|] at(x1,y1,z1). Then directional derivative is given by
(gradf).( ⃗⃗[⃗⃗𝑉
⃗→ /|𝑉|].

MATLAB Syntax used:


inline(expr) Constructs an inline function object from the
MATLAB expression contained in the string
expr.
vectorize(fun) Inserts a . before any ^, * or / in s. The result
is a character string
quiver(x,y,u,v) Displays velocity vectors as arrows with
components (u,v) at the points (x,y)
quiver3(x,y,z,u,v,w) Plots vectors with components (u,v,w) at the
points (x,y,z))
vectarrow(p0,p1) Plots a line vector with arrow pointing from
point p0 to point p1. The function can plot
both 2D and 3D vector with arrow depending
on the dimension of the input
g = gradient(f) finds the gradient vector of the scalar function
f with respect to a vector constructed from all
symbolic scalar variables found in f. The
order of variables in this vector is defined by
symvar.

Example 1:
Draw the two dimensional vector field for the vector 𝑥𝜄→ +𝑦𝖩→
MATLAB Code:
clc
clear all
syms x y
F=input( 'enter the vector as i, and j order in vector form:');
P = inline(vectorize(F(1)), 'x', 'y');
Q = inline(vectorize(F(2)), 'x', 'y');
x = linspace(-1, 1, 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')

Output:
In the Command window:

Enter the vector as i,j and k order in vector form:[x y]

1.2

0.8

0.6
y
0.4

0.2

-0.2
-0.2 0 0.2 0.4 0.6 0.8 1 1.2
x
Example 2:
Draw the three dimensional vector field for the vector 𝑥𝜄→ -𝑦𝖩→ +𝑧⃗⃗𝑘→

MATLAB Code:
syms x y z
F=input( 'enter the vector as i,j and korder in vector form:')
P = inline(vectorize(F(1)), 'x', 'y','z');
Q = inline(vectorize(F(2)), 'x', 'y','z');
R = inline(vectorize(F(3)), 'x', 'y','z');
x = linspace(-1, 1, 5); 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);
quiver3(X,Y,Z,U,V,W,1.5)
axis on
xlabel('x')
ylabel('y')
zlabel('z')

Output:
In the Command Window:

enter the vector as i, j and k order in vector form:[x -y z]

F =[ x, -y, z]

1.

0.

z 0

-0.5

-1

-1.5
1
0. 2
0 1
0
-0.5 -1
-1
Example 3: y -2
x

Find the gradient vector field of (𝑥, 𝑦) = 𝑥2𝑦 − 𝑦3 . Plot the gradient vector field together with
a contour map of f. How are they related?
MATLAB Code:
clc
clear all
syms x y
f=input( 'enter the function f(x,y):');
F=gradient(f)
P = inline(vectorize(F(1)), 'x', 'y');
Q = inline(vectorize(F(2)), 'x','y');
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
ezcontour(f,[-2 2])

Output:
Command Window:

enter the function f(x,y):

x^2*y-y^3

Inference:
The gradient vectors are orthogonal to the contours.

Example 4

Find (a) the curl and (b) the divergence of the vector field.
Matlab code

clc
clear all
syms x y z real
F=input( 'enter the vector as i, j and k order in vector form:')
curl_F = curl(F, [x y z])
div_F = divergence(F, [x y z])
Output:

enter the vector as i,j and k order in vector form:


[x^2*y*z x*y^2*z x*y*z^2]
F =
[x^2*y*z, x*y^2*z, x*y*z^2]
curl_F =

x*z^2 - x*y^2
y*x^2 - y*z^2
- z*x^2 + z*y^2

div_F =

6*x*y*z

Example 5

Determine whether or not the vector field is


conservative. If it is conservative, find a function f such that .

Matlab code

clc
clear all
syms x y z real
F=input( 'enter the vector as i,j and k order in vector form:')
curl_F = curl(F, [x y z])
if (curl_F ==[0 0 0])
f = potential(F, [x y z])
else
sprintf('curl_F is not equal to zero')
end

Output:
curl_F =
0
0
0
f =
x*y^2*z^3

Exercise
1. Plot the gradient vector field of f together with a contour map of f . Explain how they are
related to each other
(a) 𝑓 = 𝑠𝑖𝑛(𝑥) + 𝑠𝑖𝑛(𝑦)

Output:
enter the function f(x,y):
sin(x)+sin(y)

F =

cos(x)
cos(y)

Inference:
The gradient vectors are orthogonal to the contours.

(b) (c) 𝑓 = 𝑥2 + 𝑦2
Output:
enter the function f(x,y):
x^2+y^2

F =
2*x
2*y

Inference:
The gradient vectors are orthogonal to the contours.

2. Determine whether or not the vector field is conservative.


If it is conservative, find a function f such that .

Output:
nter the vector as i,j and k order in vector form:
[2*x*y+z^3 x^2 3*x*z^2]

F =

[z^3 + 2*x*y, x^2, 3*x*z^2]

curl_F =

0
0
0

f =

x*(z^3 + x*y)
LINE INTEGRAL AND GREEN’S THEOREM

Definition: let 𝐹⃗ be a continuous vector filed defined on a smooth curve C given by a vector
function 𝑟⃗(𝑡 ), 𝑎 ≤ 𝑡 ≤ 𝑏. Then the line integral of 𝐹⃗ along C is integral
𝑏
∫ 𝐹⃗ ∙ 𝑑𝑟⃗ = ∫ 𝐹⃗ (𝑟⃗(𝑡)) ∙ 𝑟⃗ ′(𝑡)𝑑𝑡
𝐶 𝑎

inline(expr) Constructs an inline function object from


the MATLAB expression contained in the
string expr.
vectorize(fun) Inserts a . before any ^, * or / in s. The
result is a character string
quiver(x,y,u,v) Displays velocity vectors as arrows with
components (u,v) at the points (x,y)
quiver3(x,y,z,u,v,w) Plots vectors with components (u,v,w)
at the points (x,y,z))
vectarrow(p0,p1) Plots a line vector with arrow pointing
from point p0 to point p1. The function
can plot both 2D and 3D vector with
arrow depending on the dimension of the
input

Matlab code
clc
clear
syms t x y
f=input('enter the f vector as i and j order in vector form:');
rbar = input('enter the r vector as i and j order in vector
form:');
lim=input('enter the limit of integration:');
vecfi=input('enter the vector field range'); % knowledge of the
curve is essential
drbar=diff(rbar,t);
sub = subs(f,[x,y],rbar);
f1=dot(sub,drbar)
int(f1,t,lim(1),lim(2))
P = inline(vectorize(f(1)), 'x', 'y');
Q = inline(vectorize(f(2)), 'x', 'y')
x = linspace(vecfi(1),vecfi(2), 10); y = x;
[X,Y] = meshgrid(x,y);
U = P(X,Y);
V = Q(X,Y);
quiver(X,Y,U,V)
hold on
fplot(rbar(1),rbar(2),[lim(1),lim(2)])
axis on
xlabel('x')
ylabel('y')

Example 1
Find the work done by the force field 𝐹⃗ (𝑥, 𝑦) = 𝑥 2 𝚤⃗ − 𝑥𝑦𝚥⃗ in moving a particle along the
𝜋
quarter – circle 𝑟⃗(𝑡) = cos(𝑡 ) 𝚤⃗ + sin (𝑡)𝚥⃗ , 0≤ 𝑡 ≤
2

Sol: ∫ 𝐹 ∙ 𝑑𝑟⃗
𝐶
Output:
enter the f vector as i and j order in vector form:
[x^2 -x*y]
enter the r vector as i and j order in vector form:
[cos(t) sin(t)]
enter the limit of integration:
[0 pi/2]
enter the vector field range
[0 2]
ans = -2/3

Example 2
Evaluate the line integral ∫𝐶 𝐹⃗ ∙ 𝑑𝑟⃗, 𝐹⃗ (𝑥, 𝑦) = (𝑥 − 𝑦)𝚤⃗ + 𝑥𝑦𝚥⃗, C is the arc of the circle
𝑥 2 + 𝑦 2 = 4 traversed counter-clockwise from (2, 0) to(0, -2)
Output:
enter the f vector as i and j order in vector form:
[x-y x*y]
enter the r vector as i and j order in vector form:
[2*cos(t) 2*sin(t)]
enter the limit of integration:
[0, 3*pi/2]
enter the vector field range
[-2.5 2.5]
ans = 3*pi + 2/3
Exercise
1. Find the work done by the force field 𝐹⃗ (𝑥, 𝑦) = (𝑦 + 𝑧⃗, 𝑥 + 𝑧⃗, 𝑥 + 𝑦) on a particle
that moves along the line segment (1, 0, 0) to (3, 4, 2)

Code
clc
clear
syms t x y z
f=input('enter the f vector as i j and korder in vector form:');
rbar = input('enter the r vector as i j and k order in vector form:');
lim=input('enter the limit of integration:');
vecfi=input('enter the vector field range'); % knowledge of the curve is essential
drbar=diff(rbar,t);
sub = subs(f,[x,y,z],rbar);
f1=dot(sub,drbar)
int(f1,t,lim(1),lim(2))
P = inline(vectorize(f(1)), 'x', 'y', 'z');
Q = inline(vectorize(f(2)), 'x', 'y', 'z');
R = inline(vectorize(f(3)), 'x', 'y', 'z');
x = linspace(vecfi(1),vecfi(2), 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);
quiver3(X,Y,Z,U,V,W)
hold on
t=linspace(lim(1),lim(2),50);
a=subs(rbar(1),t);
b=subs(rbar(2),t);
c=subs(rbar(3),t);
plot3(a,b,c)
axis on
xlabel('x')
ylabel('y')
zlabel('z')
Output:

enter the f vector as i j and korder in vector form:


[y+z x+z x+y]
enter the r vector as i j and k order in vector form:
[1+2*t 4*t 2*t]
enter the limit of integration:
[0 1]
enter the vector field range
[-2 2]

ans =

26

GREEN’S THEOREM
Let C be a positively oriented, piecewise-smooth, simple closed curve in the plane
and let D be the region bounded by C. If P and Q have continuous partial derivatives
on an open region that contains D, then
𝜕𝑄 𝜕𝑃
∫ 𝑃𝑑𝑥 + 𝑄 𝑑𝑦 = ∬ ( − ) 𝑑𝐴
𝐶 𝐷 𝜕𝑥 𝜕𝑦
Example 1
Evaluate ∮𝐶 (3𝑦 − 𝑒 sin(𝑥) )𝑑𝑥 + (7𝑥 + √𝑦 4 + 1)𝑑𝑦, where C is the circle 𝑥 2 + 𝑦 2 =
9.
clc
clear all
syms x y r t
F=input('enter the F vector as i and j order in vector form:');
integrand=diff(F(2),x)-diff(F(1),y);
polarint=r*subs(integrand,[x,y],[r*cos(t),r*sin(t)]);
sol=int(int(polarint,r,0,3),t,0,2*pi);
P = inline(vectorize(F(1)), 'x', 'y');
Q = inline(vectorize(F(2)), 'x', 'y')
x = linspace(-3.2,3.2, 10); y = x;
[X,Y] = meshgrid(x,y);
U = P(X,Y);
V = Q(X,Y);
quiver(X,Y,U,V)
hold on
fplot(3*cos(t),3*sin(t),[0,2*pi])
axis equal
In the command window
enter the F vector as i and j order in vector form:
[3*y-exp(sin(x)) 7*x+ sqrt(y^4+1)]
sol = 36*pi
In the figure window

Exercise
Evaluate ∮𝐶 (𝑦 2 )𝑑𝑥 + (3𝑥𝑦)𝑑𝑦, where C is the boundary of the circles 𝑥 2 + 𝑦 2 = 1
Code:

clc
clear all
syms x y r t
F=input('enter the F vector as i and j order in vector form:');
integrand=diff(F(2),x)-diff(F(1),y);
polarint=r*subs(integrand,[x,y],[r*cos(t),r*sin(t)]);
sol=int(int(polarint,r,0,1),t,0,2*pi)
P = inline(vectorize(F(1)), 'x', 'y');
Q = inline(vectorize(F(2)), 'x', 'y')
x = linspace(-3.2,3.2, 10); y = x;
[X,Y] = meshgrid(x,y);
U = P(X,Y);
V = Q(X,Y);
quiver(X,Y,U,V)
hold on
fplot(cos(t),sin(t),[0,2*pi])
axis equal

Output
enter the F vector as i and j order in vector form:
[y^2 3*x*y]

sol =

0
Q=

Inline function:
Q(x,y) = 3.*x.*y

You might also like