Download as pdf or txt
Download as pdf or txt
You are on page 1of 38

LAB REPORT

COMPUTATIONAL ELECTROMAGNETICS
19CCE203

Amrita School of Engineering

Amrita Vishwa Vidyapeetham

Chennai – 601 103, Tamil Nadu, India.

Shrie Varshini V
CH.EN.U4CCE22040
1. Calculate divergence and curl of vectors (take any vector)

Abstract:
The divergence of a vector field represents the net flow of the vector field's
quantity per unit volume at a given point at the same time the curl of a vector
field is a vector operator that represents the rotational behaviour of the field at
each point in space. In this experiment we are finding the divergence and curl of
any definite vector in MATLAB using specific keywords used in the
application.

Introduction with equations:


Divergence = .A
Curl = xA

Is called as Del operator

A is represented as

 Divergence of the given vector is calculated by the dot product of del


operator and the given vector A
 Curl of the given vector is calculated by the cross product of del operator
and the given vector A
Program:
close all
clear all
clc
syms x y z
U1=[x^2*y*z 0 x*z];
d=divergence(U1,[x y z])
c=curl(U1,[x y z])

Output:

Fig.1 The MATLAB output command window displaying the divergence ‘d’ and curl ‘c’ for a known
vector ‘U1’

Conclusion:
In the MATLAB application close all, clear all, clc commands are given to clear
and refresh the command window. Then using ‘syms’ command w declares x, y,
z variables. Then a vector U1 in defined and putting them in commands
‘divergence’ and ‘curl’ we get d and c in the command window below. In coding
part, the curly brackets are meant for separation purposes and the square
brackets are meant for the definition of matrices.
2. Calculate gradient of a scalar (take any scalar)

Abstract:
The gradient is a vector that represents the rate of change and the direction of
the steepest increase of a scalar-valued function in multi-dimensional space. In
this experiment we are finding the divergence and curl of any definite vector in
MATLAB using specific keywords used in the application.

Introduction with equations:

Gradient of a scalar V is written as V.

Program:
close all
clear all
clc
syms x y z
U1=x.*exp(-x.^2-y.^2);
g=gradient(U1,[x y z])
Output:

Fig.2 The MATLAB output command window displaying the gradient ’g’ for a
known vector ‘U1’

Conclusion:
In the MATLAB application close all, clear all, clc commands are given to clear
and refresh the command window. Then using ‘syms’ command w declares x, y,
z variables. Then a scalar U1 in defined and putting them in commands
‘gradient’ we get the vector g in the command window below. In coding part,
the curly brackets are meant for separation purposes and the square brackets are
meant for the definition of matrices.
3. Plot the function graph and gradient of the function

A. x exp(-x^2-y^2)

Abstract:
In this experiment, we use MATLAB to calculate the values of a scalar function
and its gradient at multiple points on a grid. We then visualize the function by
plotting it as a 3D surface, giving us a clear view of its behaviour. Additionally,
we represent the gradient of the function as arrows on a 2D grid, allowing us to
understand how the function changes direction and magnitude at different
points.

Introduction with equations:

Gradient of a scalar V is written as V.

Program:
1.close all
clear all
clc
[x,y]=meshgrid(-3:0.5:3);
U1=x.*exp(-x.^2-y.^2);
contour(x,y,U1);

xlabel('x')
ylabel('y')
zlabel('z')

2.close all
clear all
clc
[x,y]=meshgrid(-3:0.5:3);
U1=x.*exp(-x.^2-y.^2);
d1=gradient(x.*exp(-x.^2-y.^2))
[x,y]=meshgrid(-3:0.5:3);
[px,py] = gradient(U1);
quiver(x,y,px,py);
U1=x.*exp(-x.^2-y.^2);

xlabel('x')
ylabel('y')
zlabel('z')

Graph:

Fig.3 The MATLAB output screen displaying the functional graph of the
function x exp(-x^2-y^2)
Fig.4 The MATLAB output screen displaying the gradient graph of the function
x exp(-x^2-y^2)

Conclusion:
In the MATLAB application close all, clear all, clc commands are given to clear
and refresh the command window. Then we have done a code in MATLAB for
plotting the functional graph and the gradient graph. I have plotted the 2 graphs
separately since I faced confusions in plotting them one together. I have first
plotted functional graph for the function x exp(-x^2-y^2), then its gradient
graph. The command ‘meshgrid’ is used for defining the size of each grid in the
functional graph and the command ‘contour’ is used for plotting the functional
graph at the same time ‘quiver’ is used for plotting the gradient graph. The x, y,
z label function are used for plotting the given graph in the output screen.
B. xy

Abstract:
In this experiment, we use MATLAB to calculate the values of a scalar function
and its gradient at multiple points on a grid. We then visualize the function by
plotting it as a 3D surface, giving us a clear view of its behaviour. Additionally,
we represent the gradient of the function as arrows on a 2D grid, allowing us to
understand how the function changes direction and magnitude at different
points.

Introduction with equations:

Gradient of a scalar V is written as V.

Program:
1.close all
clear all
clc
[x,y]=meshgrid(-3:0.5:3);
U1=x.*y;
contour(x,y,U1);

xlabel('x')
ylabel('y')
zlabel('z')

2.close all
clear all
clc
[x,y]=meshgrid(-3:0.5:3);

U1=x.*y;
d1=gradient(x.*exp(-x.^2-y.^2))
[x,y]=meshgrid(-3:0.5:3);
[px,py] = gradient(U1);
quiver(x,y,px,py);
U1=x.*exp(-x.^2-y.^2);

xlabel('x')
ylabel('y')
zlabel('z')

Graph:

Fig.5 The MATLAB output screen displaying the functional graph of the
function xy
Fig.6 The MATLAB output screen displaying the gradient graph of the function
xy

Conclusion:
In the MATLAB application close all, clear all, clc commands are given to clear
and refresh the command window. Then we have done a code in MATLAB for
plotting the functional graph and the gradient graph. I have plotted the 2 graphs
separately since I faced confusions in plotting them one together. I have first
plotted functional graph for the function xy, then its gradient graph. The
command ‘meshgrid’ is used for defining the size of each grid in the functional
graph and the command ‘contour’ is used for plotting the functional graph at the
same time ‘quiver’ is used for plotting the gradient graph. The x, y, z label
function are used for plotting the given graph in the output screen.
C. cosx.cosy

Abstract:
In this experiment, we use MATLAB to calculate the values of a scalar function
and its gradient at multiple points on a grid. We then visualize the function by
plotting it as a 3D surface, giving us a clear view of its behaviour. Additionally,
we represent the gradient of the function as arrows on a 2D grid, allowing us to
understand how the function changes direction and magnitude at different
points.

Introduction with equations:

Gradient of a scalar V is written as V.

Program:
1.close all
clear all
clc
[x,y]=meshgrid(-3:0.5:3);
U1=cos(x).*cos(y);
contour(x,y,U1);

xlabel('x')
ylabel('y')
zlabel('z')

2.close all
clear all
clc
[x,y]=meshgrid(-3:0.5:3);

U1= cos(x).*cos(y);
d1=gradient(x.*exp(-x.^2-y.^2))
[x,y]=meshgrid(-3:0.5:3);
[px,py] = gradient(U1);
quiver(x,y,px,py);
U1=x.*exp(-x.^2-y.^2);

xlabel('x')
ylabel('y')
zlabel('z')

Graph:

Fig.7 The MATLAB output screen displaying the functional graph of the
function cosx.cosy
Fig.8 The MATLAB output screen displaying the gradient graph of the function
cosx.cosy

Conclusion:
In the MATLAB application close all, clear all, clc commands are given to clear
and refresh the command window. Then we have done a code in MATLAB for
plotting the functional graph and the gradient graph. I have plotted the 2 graphs
separately since I faced confusions in plotting them one together. I have first
plotted functional graph for the function cosx.cosy, then its gradient graph. The
command ‘meshgrid’ is used for defining the size of each grid in the functional
graph and the command ‘contour’ is used for plotting the functional graph at the
same time ‘quiver’ is used for plotting the gradient graph. The x, y, z label
function are used for plotting the given graph in the output screen.
D. cosxy

Abstract:
In this experiment, we use MATLAB to calculate the values of a scalar function
and its gradient at multiple points on a grid. We then visualize the function by
plotting it as a 3D surface, giving us a clear view of its behaviour. Additionally,
we represent the gradient of the function as arrows on a 2D grid, allowing us to
understand how the function changes direction and magnitude at different
points.

Introduction with equations:

Gradient of a scalar V is written as V.

Program:
1.close all
clear all
clc
[x,y]=meshgrid(-3:0.5:3);
U1=cos(x.*y);
contour(x,y,U1);

xlabel('x')
ylabel('y')
zlabel('z')

2.close all
clear all
clc
[x,y]=meshgrid(-3:0.5:3);

U1= cos(x.*y);
d1=gradient(x.*exp(-x.^2-y.^2))
[x,y]=meshgrid(-3:0.5:3);
[px,py] = gradient(U1);
quiver(x,y,px,py);
U1=x.*exp(-x.^2-y.^2);

xlabel('x')
ylabel('y')
zlabel('z')

Graph:

Fig.7 The MATLAB output screen displaying the functional graph of the
function cosxy
Fig.8 The MATLAB output screen displaying the gradient graph of the function
cosxy

Conclusion:
In the MATLAB application close all, clear all, clc commands are given to clear
and refresh the command window. Then we have done a code in MATLAB for
plotting the functional graph and the gradient graph. I have plotted the 2 graphs
separately since I faced confusions in plotting them one together. I have first
plotted functional graph for the function cosxy, then its gradient graph. The
command ‘meshgrid’ is used for defining the size of each grid in the functional
graph and the command ‘contour’ is used for plotting the functional graph at the
same time ‘quiver’ is used for plotting the gradient graph. The x, y, z label
function are used for plotting the given graph in the output screen.
E. sinx.siny

Abstract:
In this experiment, we use MATLAB to calculate the values of a scalar function
and its gradient at multiple points on a grid. We then visualize the function by
plotting it as a 3D surface, giving us a clear view of its behaviour. Additionally,
we represent the gradient of the function as arrows on a 2D grid, allowing us to
understand how the function changes direction and magnitude at different
points.

Introduction with equations:


Gradient of a scalar V is written as V.

Program:
1.close all
clear all
clc
[x,y]=meshgrid(-3:0.5:3);
U1= sin(x).*sin(y);
contour(x,y,U1);

xlabel('x')
ylabel('y')
zlabel('z')

2.close all
clear all
clc
[x,y]=meshgrid(-3:0.5:3);

U1= sin(x).*sin(y);
d1=gradient(x.*exp(-x.^2-y.^2))
[x,y]=meshgrid(-3:0.5:3);
[px,py] = gradient(U1);
quiver(x,y,px,py);
U1=x.*exp(-x.^2-y.^2);

xlabel('x')
ylabel('y')
zlabel('z')

Graph:

Fig.7 The MATLAB output screen displaying the functional graph of the
function sinx.siny
Fig.8 The MATLAB output screen displaying the gradient graph of the function
sinx.siny

Conclusion:
In the MATLAB application close all, clear all, clc commands are given to clear
and refresh the command window. Then we have done a code in MATLAB for
plotting the functional graph and the gradient graph. I have plotted the 2 graphs
separately since I faced confusions in plotting them one together. I have first
plotted functional graph for the function sinx.siny, then its gradient graph. The
command ‘meshgrid’ is used for defining the size of each grid in the functional
graph and the command ‘contour’ is used for plotting the functional graph at the
same time ‘quiver’ is used for plotting the gradient graph. The x, y, z label
function are used for plotting the given graph in the output screen.
4. Plot the function graph and divergence of the function

A. -sin(x)sin(y)ax + cos(x)cos(y)ay

Abstract:
In this experiment, we use MATLAB to calculate the values of a vector function
and its divergence at multiple points on a grid. We then visualize the function by
plotting it as a 3D surface, giving us a clear view of its behaviour. Additionally,
we calculate and display the divergence of the vector field using 3D surface
plots created with the surf function. This divergence helps us understand if the
vector field behaves as a source or a sink at different locations in space.

Introduction to formula:

Divergence = .A

Is called as Del operator

A is represented as

Divergence of the given vector is calculated by the dot product of del operator
and the given vector A

Program:
1.clf;
[x, y] = meshgrid(-3:0.5:3);
Fx = -sin(x).* sin(y);
Fy = cos(x).* cos(y);
quiver(x, y, Fx, Fy);
xlabel('x');
ylabel('y');

2. clf;
[x,y]=meshgrid(-2:0.2:2,-2:0.2:2);
g1 = -sin(x) .* sin(y);
g2 = cos(x) .* cos(y);
[px]=divergence(x,y,g1,g2)
contour(x,y,px)
xlabel('x');
ylabel('y');

Graph:

Fig.9 The MATLAB output screen displaying the functional graph of the
function -sin(x)sin(y)ax + cos(x)cos(y)ay
Fig.10 The MATLAB output screen displaying the divergence of the
function -sin(x)sin(y)ax + cos(x)cos(y)ay

Conclusion:
In the MATLAB application close all, clear all, clc commands are given to clear
and refresh the command window. Then we have done a code in MATLAB for
plotting the functional graph and the gradient graph. I have plotted the 2 graphs
separately since I faced confusions in plotting them one together. I have first
plotted functional graph for the function F = -sin(x)sin(y)ax + cos(x)cos(y)ay,
then its gradient graph. The command ‘meshgrid’ is used for defining the size of
each grid in the functional graph. The given vector field, represented as
F = -sin(x)sin(y)ax + cos(x)cos(y)ay, was visualized using quiver plots to show
both the direction and magnitude of vectors. Additionally, the divergence of the
vector field was calculated and displayed using 3D surface plots created with
the surf function.
4. Plot the function graph and divergence of the function

B. (y sin xy)ax –(x sinxy) ay


Abstract:
In this experiment, we use MATLAB to calculate the values of a vector function
and its divergence at multiple points on a grid. We then visualize the function by
plotting it as a 3D surface, giving us a clear view of its behaviour. Additionally,
we calculate and display the divergence of the vector field using 3D surface
plots created with the surf function. This divergence helps us understand if the
vector field behaves as a source or a sink at different locations in space.

Introduction to formula:
Divergence = .A

Is called as Del operator

A is represented as

Divergence of the given vector is calculated by the dot product of del operator
and the given vector A

Program:
1. clf;
[x, y] = meshgrid(-3:0.5:3);
Fx = y .* sin(x.*y);
Fy = -x .* sin(x.*y);
quiver(x, y, Fx, Fy);
xlabel('x');
ylabel('y');
2. clf;
[x,y]=meshgrid(-2:0.2:2,-2:0.2:2);
g1 = y .* sin(x.*y);
g2 = -x .* sin(x.*y);
[px]=divergence(x,y,g1,g2)
contour(x,y,px)
xlabel('x');
ylabel('y');

Graph:

Fig.11 The MATLAB output screen displaying the functional graph of the
function -sin(x)sin(y)ax + cos(x)cos(y)ay
Fig.12 The MATLAB output screen displaying the divergence of the
function -sin(x)sin(y)ax + cos(x)cos(y)ay

Conclusion:
In the MATLAB application close all, clear all, clc commands are given to clear
and refresh the command window. Then we have done a code in MATLAB for
plotting the functional graph and the gradient graph. I have plotted the 2 graphs
separately since I faced confusions in plotting them one together. I have first
plotted functional graph for the function (y sin xy)ax –(x sinxy) ay, then its
gradient graph. The command ‘meshgrid’ is used for defining the size of each
grid in the functional graph. The given vector field, represented as (y sin xy)ax
–(x sinxy) ay was visualized using quiver plots to show both the direction and
magnitude of vectors. Additionally, the divergence of the vector field was
calculated and displayed using 3D surface plots created with the surf function.
5. plot the function and curl of the function

A. U=-y ax + x ay

Abstract:
In this experiment, we use MATLAB to calculate the values of a vector function
and its curl at multiple points on a grid. The vector field is represented by the
components Ux = -y and Uy = x, where ax and ay denote the unit vectors along
the x and y axes, respectively. We employ quiver plots to visualize the function
graph, showing the vector field's direction and magnitude using arrows.
Furthermore, we compute and display the curl of the vector field using 3D
surface plots generated by the surf function.

Introduction to Formula:
Curl = xA
Is called as Del operator

A is represented as

Curl of the given vector is calculated by the cross product of del operator and
the given vector A

Program:
1.clf;
[x, y] = meshgrid(-3:0.5:3);
Fx = -y;
Fy = x;
quiver(x, y, Fx, Fy);
xlabel('x');
ylabel('y');
2. clf;
[x, y, z] = meshgrid(-2:0.2:2, -2:0.2:2, -2:0.2:2);
g1 =-y;
g2 = x;
g3 = 0*z;
[px py pz] = curl(x,y,z,g1,g2,g3);
U1=[px py pz]
quiver3(x,y,z,px,py,pz)
xlabel('x');
ylabel('y');
zlabel('z');

Graph:

Fig.13 The MATLAB output screen displaying the functional graph of the
function U=-y ax + x ay
Fig.14 The MATLAB output screen displaying the curl graph of the function
U=-y ax + x ay

Conclusion:
In the MATLAB application close all, clear all, clc commands are given to clear
and refresh the command window. Then we have done a code in MATLAB for
plotting the functional graph and the gradient graph. I have plotted the 2 graphs
separately since I faced confusions in plotting them one together. I have first
plotted functional graph for the function U=-y ax + x ay, then its gradient graph.
The command ‘meshgrid’ is used for defining the size of each grid in the
functional graph. The vector field U = -y ax + x ay was plotted, visualizing its
behaviour and direction using quiver plots. Additionally, the curl of the vector
field was calculated and presented, indicating the field's rotation at different
points.
B. V=e-y2 ay

Abstract:
In this experiment, we use MATLAB to calculate the values of a vector function
and its curl at multiple points on a grid. We employ quiver plots to visualize the
function graph, showing the vector field's direction and magnitude using arrows.
Furthermore, we compute and display the curl of the vector field using 3D
surface plots generated by the surf function.

Introduction to Formula:
Curl = xA
Is called as Del operator

A is represented as

Curl of the given vector is calculated by the cross product of del operator and
the given vector A

Program:
1.clf;
[x, y] = meshgrid(-3:0.5:3);
Fx = 0*x;
Fy = exp(-y.^2);
quiver(x, y, Fx, Fy);
xlabel('x');
ylabel('y');

2. clf;
[x, y, z] = meshgrid(-2:0.2:2, -2:0.2:2, -2:0.2:2);
g1 =0*x;
g2 = exp(-y.^2);
g3 = 0*z;
[px py pz] = curl(x,y,z,g1,g2,g3);
U1=[px py pz]
quiver3(x,y,z,px,py,pz)
xlabel('x');
ylabel('y');
zlabel('z');

Graph:

Fig.15 The MATLAB output screen displaying the functional graph of the
function V=e-y2 ay
Fig.16 The MATLAB output screen displaying the curl graph of the function
V=e-y2 ay

Conclusion:
In the MATLAB application close all, clear all, clc commands are given to clear
and refresh the command window. Then we have done a code in MATLAB for
plotting the functional graph and the gradient graph. I have plotted the 2 graphs
separately since I faced confusions in plotting them one together. I have first
plotted functional graph for the function V=e-y2 ay, then its gradient graph. The
command ‘meshgrid’ is used for defining the size of each grid in the functional
graph. The vector field V=e-y2 ay was plotted, visualizing its behaviour and
direction using quiver plots. Additionally, the curl of the vector field was
calculated and presented, indicating the field's rotation at different points.
C. W= e-x2 ay
Abstract:
In this experiment, we use MATLAB to calculate the values of a vector function
and its curl at multiple points on a grid. We employ quiver plots to visualize the
function graph, showing the vector field's direction and magnitude using arrows.
Furthermore, we compute and display the curl of the vector field using 3D
surface plots generated by the surf function.

Introduction to Formula:
Curl = xA
Is called as Del operator

A is represented as

Curl of the given vector is calculated by the cross product of del operator and
the given vector A

Program:
1.clf;
[x, y] = meshgrid(-3:0.5:3);
Fx = 0*x;
Fy = exp(-x.^2);
quiver(x, y, Fx, Fy);
xlabel('x');
ylabel('y');
2. clf;
[x, y, z] = meshgrid(-2:0.2:2, -2:0.2:2, -2:0.2:2);
g1 =0*x;
g2 = exp(-x.^2);
g3 = 0*z;
[px py pz] = curl(x,y,z,g1,g2,g3);
U1=[px py pz]
quiver3(x,y,z,px,py,pz)
xlabel('x');
ylabel('y');
zlabel('z');

Graph:

Fig.17 The MATLAB output screen displaying the functional graph of the
function W= e-x2 ay
Fig.18 The MATLAB output screen displaying the curl graph of the function
W= e-x2 ay

Conclusion:
In the MATLAB application close all, clear all, clc commands are given to clear
and refresh the command window. Then we have done a code in MATLAB for
plotting the functional graph and the gradient graph. I have plotted the 2 graphs
separately since I faced confusions in plotting them one together. I have first
plotted functional graph for the function W= e-x2 ay, then its gradient graph.
The command ‘meshgrid’ is used for defining the size of each grid in the
functional graph. The vector field W= e-x2 ay was plotted, visualizing its
behaviour and direction using quiver plots. Additionally, the curl of the vector
field was calculated and presented, indicating the field's rotation at different
points.
D.U=-y ax + x ay

Abstract:
In this experiment, we use MATLAB to calculate the values of a vector function
and its curl at multiple points on a grid. The vector field is represented by the
components Ux = -y and Uy = x, where ax and ay denote the unit vectors along
the x and y axes, respectively. We employ quiver plots to visualize the function
graph, showing the vector field's direction and magnitude using arrows.
Furthermore, we compute and display the curl of the vector field using 3D
surface plots generated by the surf function.

Introduction to Formula:
Curl = xA
Is called as Del operator

A is represented as

Curl of the given vector is calculated by the cross product of del operator and
the given vector A

Program:
1.clf;
[x, y] = meshgrid(-3:0.5:3);
Fx = -y;
Fy = x;
quiver(x, y, Fx, Fy);
xlabel('x');
ylabel('y');

2. clf;
[x, y, z] = meshgrid(-2:0.2:2, -2:0.2:2, -2:0.2:2);
g1 =-y;
g2 = x;
g3 = 0*z;
[px py pz] = curl(x,y,z,g1,g2,g3);
U1=[px py pz]
quiver3(x,y,z,px,py,pz)
xlabel('x');
ylabel('y');
zlabel('z');

Graph:

Fig.19 The MATLAB output screen displaying the functional graph of the
function -y ax + x ay
Fig.20 The MATLAB output screen displaying the curl graph of the function -y
ax + x ay

Conclusion:
In the MATLAB application close all, clear all, clc commands are given to clear
and refresh the command window. Then we have done a code in MATLAB for
plotting the functional graph and the gradient graph. I have plotted the 2 graphs
separately since I faced confusions in plotting them one together. I have first
plotted functional graph for the function -y ax + x ay, then its gradient graph.
The command ‘meshgrid’ is used for defining the size of each grid in the
functional graph. The vector field -y ax + x ay was plotted, visualizing its
behaviour and direction using quiver plots. Additionally, the curl of the vector
field was calculated and presented, indicating the field's rotation at different
points.

You might also like