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

Gradient divergence curl (1)

This is a matlabs code about gradient divergence curl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Gradient divergence curl (1)

This is a matlabs code about gradient divergence curl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

9.

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:

""""⃗𝟏 (x, y) + 𝒇
""⃗=𝒇
Draw the two dimensional vector field for the vector 𝑭 """"⃗𝟐 (x,y)

• """"⃗𝟏 (x,y,z)+ 𝒇
""⃗=𝒇
Draw the three dimensional vector field for the vector 𝑭 """"⃗𝟐 (x,y,z)
""""⃗𝟑 (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𝚤⃗ + V2𝑗 + V3𝑘 "⃗ 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 𝒙.⃗ +𝒚0⃗
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]

In the Figure window:

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 𝒙.⃗ -𝒚0⃗ +𝒛𝒌

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]

In the figure window:

1.

1!

0.

z! 0!

-0.5!

-1!

-1.5!
1!
0. 2!
0! 1!
0!
-0.5! -1!
y! -1! -2!
x!
!
Example 3:
Find the gradient vector field of (𝑥, 𝑦) = 𝑥 $ 𝑦 − 𝑦 % . 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) 𝑓 = 𝑠𝑖𝑛(𝑥) + 𝑠𝑖𝑛(𝑦) (b) 𝑓 = 𝑠𝑖𝑛(𝑥 + 𝑦) (c) 𝑓 = 𝑥 $ + 𝑦 $

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


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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy