06 - Simulation of Electromagnetic Fields
06 - Simulation of Electromagnetic Fields
SIMULATION OF
ELECTROMAGNETIC
FIELDS
1
”Simulation of Electromagnetic Fields”
CONTENTS
1. MATLAB FOR BEGINNERS
1.1 Getting Started
1.2 Addition operation in MATLAB
1.3 Subtraction Operation
1.4 Multiplication Operation
1.5 Writing a program in MATLAB
1.6 General programming Commands in MATLAB
2. MATLAB COMMANDS
2.1 quiver3
2.2 vectarrow
2.3 hold
2.4 num2str
2.5 sqrt
2.6 text
2.7.1 xlabel
2.7.2 ylabel
2.7.3 zlabel
2.7.4 title
2.8 patch
2.9 surf
3. Co-Ordinate Systems
3.1 Addition of two vectors using Parallelogram rule
3.2 Addition of two vectors using head to tail rule
3.3 Plotting of unit vectors
3.4 Calculation and representation of unit vector
3.5 Illustration of position vector
3.6 Plotting of position vectors
3.7 Calculation of angle between vectors
3.8 Representation of two planes with ‘x’ constant
3.9 Representation of planes in X-Y and Z co-ordinates
3.10 Representation of differential element in Cartesian co-ordinates
3.11 Representation of surface with φ constant
3.12 Representation of surface with z constant
3.13 Representation of surface with ρ constant
3.14 Representation of surfaces in cylindrical co-ordinates
3.15 Representation of differential elements in cylindrical co-ordinates
3.16 Representation of surface with θ constant
3.17 Representation of surface with r constant
3.18 Representation of surfaces with r, θ and φ constant
3.19 Evaluation of surface and volume integrals of a cylinder
2
”Simulation of Electromagnetic Fields”
5. EXERCISE PROBLEMS
3
”Simulation of Electromagnetic Fields”
a. Identify the MATLAB Icon on the Desktop and double click it.
Workspace
Command Window
4
”Simulation of Electromagnetic Fields”
5
”Simulation of Electromagnetic Fields”
Example 1:
6
”Simulation of Electromagnetic Fields”
Example 2:
7
”Simulation of Electromagnetic Fields”
Example 1
8
”Simulation of Electromagnetic Fields”
9
”Simulation of Electromagnetic Fields”
Example:
Program to find the sum of 1 to n numbers
clc :clc clears the command window and homes the cursor.
10
”Simulation of Electromagnetic Fields”
clear : Clear variables and functions from memory. Clear removes all variables
from the workspace.
input :input Prompt for user input.
Disp : Display array. disp(X) displays the array, without printing the array
name
For Executing the program click on the run command and a dialogue box
will appear. Change Folder or Add to path option can be chosen.
11
”Simulation of Electromagnetic Fields”
If there are no errors in the program MATLAB waits for the input
12
”Simulation of Electromagnetic Fields”
For
For : Repeat statements a specific number of times.
for K = 1:M
for D = 1:M
B(K,D) = 1/(K+D);
end
end
13
”Simulation of Electromagnetic Fields”
14
”Simulation of Electromagnetic Fields”
IF:
if expression
statements
ELSEIF expression
statements
ELSE
statements
END
15
”Simulation of Electromagnetic Fields”
WHILE
The while loop repeatedly executes program statement(s) as long as the expression
remains true.
while expression
statements
end
a=10;
while (a<20)
a=a+1
end
Output:
a=11
a=12
a=13
a=14
a=15
a=16
a=17
a=18
a=19
a=20
16
”Simulation of Electromagnetic Fields”
2. MATLAB COMMANDS
2.1 quiver3
quiver3(X,Y,Z,U,V,W,S) plots velocity vectors as arrows with components (u,v,w) at
the points (x,y,z). The matrices X,Y,Z,U,V,W must all be the same size and contain the
corresponding position and velocity components. It automatically scales the arrows to
fit and then stretches them by S. Use S=0 to plot the arrows without the automatic
scaling. [Mathworks Documentation]
Example 2.1:
quiver3([0 0 0],[0 0 0],[0 0 0],[15 0 0],[0 15 0],[0 0 15],0);
It plots vectors of length equal to 15 in x,y,z planes from origin.
17
”Simulation of Electromagnetic Fields”
2.2 vectarrow
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. [Mathworks Documentation]
Example 2.2
p0 = [1 2 5]; % Coordinate of the first point p0
p1 = [2 5 6]; % Coordinate of the second point p1
vectarrow(p0,p1);
18
”Simulation of Electromagnetic Fields”
2.3 hold
hold ON holds the current plot and all axis properties, including the current color and
linestyle, so that subsequent graphing commands add to the existing graph without
resetting the color and linestyle.
hold OFF returns to the default mode whereby PLOT commands erase the previous
plots and reset all axis properties before drawing new plots.
hold, by itself, toggles the hold state.
[Mathworks Documentation]
Example 2.3
p0 = [1 2 5]; % Coordinate of the first point p0
p1 = [2 5 6]; % Coordinate of the second point p1
p2 = [3 6 7]; % Coordinate of the second point p2
vectarrow(p0,p1);
hold on;
vectarrow(p0,p2);
Fig 2.3 Plot indicating arrows connecting points p0 and p1 & p0 and p2, obtained
using hold command
19
”Simulation of Electromagnetic Fields”
2.4 num2str
num2str Convert numbers to character representation
T = num2str(X) converts the matrix X into its character representation T with about 4
digits and an exponent if required. This is useful for labeling plots with the TITLE,
XLABEL, YLABEL, and TEXT commands. [Mathworks Documentation]
Example 2.4
p0 = [1 2 5];
p1 = [2 5 6];
p2 = [3 6 7];
A=num2str(p0)
B=num2str(p1)
C=num2str(p2)
Output
A = '1 2 5'
B = '2 5 6'
C = '3 6 7'
2.5. sqrt
sqrt(X) is the square root of the elements of X. Complex results are produced if X is
not positive. [Mathworks Documentation]
Example 2.5.1
A=sqrt(30)
Output
A = 5.4772
Example 2.5.2
B=sqrt(-1)
Output
B= 0.0000 + 1.0000i
20
”Simulation of Electromagnetic Fields”
2.6. text
text(x,y,z,str) positions the text in 3-D coordinates. [Mathworks Documentation]
Example 2.6
p0 = [1 2 5]; % Coordinate of the first point p0
p1 = [2 5 6]; % Coordinate of the second point p1
p2 = [3 6 7]; % Coordinate of the second point p2
vectarrow(p0,p1)
hold on
vectarrow(p0,p2)
A=['p0 (',num2str(p0),')']
B=['p1 (',num2str(p1),')']
C=['p2 (',num2str(p2),')']
text(p0(1),p0(2),p0(3),A)
text(p1(1),p1(2),p1(3),B)
text(p1(1),p1(2),p2(3),C)
21
”Simulation of Electromagnetic Fields”
2.7.1 xlabel
xlabel('text') adds text beside the X-axis on the current axis.
2.7.2 ylabel
ylabel('text') adds text beside the Y-axis on the current axis.
2.7.3 zlabel
zlabel('text') adds text beside the Z-axis on the current axis.
2.7.4 title
title('text') adds text at the top of the current axis.
Example 2.7
quiver3([0 0 0],[0 0 0],[0 0 0],[15 0 0],[0 15 0],[0 0 15],0);
xlabel('x-axis');
ylabel('y-axis');
zlabel('z-axis')
title('Axes representation')
22
”Simulation of Electromagnetic Fields”
2.8 patch
patch(X,Y,Z,C) creates the polygons in 3-D coordinates using X, Y, and Z. To view the
polygons in a 3-D view, use the view(3) command. C determines the polygon colors.
Example 2.8
quiver3([0 0 0],[0 0 0],[0 0 0],[25 0 0],[0 25 0],[0 0 25]);
hold on;
quiver3([0 0 0],[0 0 0],[0 0 0],[-25 0 0],[0 -25 0],[0 0 -25]);
hold on;
x=6;
y1=-15;y2=15;y3=15;y4=-15;
z1=-15;z2=-15;z3=15;z4=15;
patch([x x x x x], [y1 y2 y3 y4 y1], [z1 z2 z3 z4 z1],'green');
A=[x y1 z1];B=[x y2 z2];C=[x y3 z3];D=[x y4 z4];
Astr=['A (',num2str(A),')'];Bstr=['B (',num2str(B),')'];
Cstr=['C (',num2str(C),')'];Dstr=['D (',num2str(D),')'];
text(A(1),A(2),A(3),Astr);text(B(1),B(2),B(3),Bstr);
text(C(1),C(2),C(3),Cstr);text(D(1),D(2),D(3),Dstr);
xlabel('x-axis');ylabel('y-axis');zlabel('z-axis');
title('Patch command-polygon');
23
”Simulation of Electromagnetic Fields”
2.9 surf
surf(X,Y,Z,C) plots the colored parametric surface defined by four matrix arguments.
The view point is specified by VIEW. The axis labels are determined by the range of X,
Y and Z, or by the current setting of AXIS. The color scaling is determined by the
range of C, or by the current setting of CAXIS. The scaled color values are used as
indices into the current COLORMAP.
surf(x,y,Z) and surf(x,y,Z,C), with two vector arguments replacing the first two matrix
arguments, must have length(x) = n and length(y) = m where [m,n] = size(Z). In this
case, the vertices of the surface patches are the triples (x(j), y(i), Z(i,j)). Note that x
corresponds to the columns of Z and y corresponds to the rows.
Example 2.9
quiver3([0 0 0],[0 0 0],[0 0 0],[25 0 0],[0 25 0],[0 0 25])
hold on
quiver3([0 0 0],[0 0 0],[0 0 0],[-25 0 0],[0 -25 0],[0 0 -25])
hold on
theta=0:pi/10:2*pi;
r=10;
x1=r*cos(theta);
y1=r*sin(theta);
for k=1:length(x1)
z1(k)=-5;
z2(k)=5;
end
x=[x1;x1];
y=[y1;y1];
z=[z1;z2];
surf(x,y,z);
xlabel('x-axis');ylabel('y-axis');zlabel('z-axis');
24
”Simulation of Electromagnetic Fields”
3.MATLAB PROGRAMS
3.1 Addition of two vectors using Parallelogram rule
Ex 3.1 Add two vectors A and B to obtain vector C. A=ax-4ay-6az and B=2ax+ay using
Parallelogram rule.
Program 3.1
quiver3([0 0 0],[0 0 0],[0 0 0],[10 0 0],[0 10 0],[0 0 10]);
hold on;
quiver3([0 0 0],[0 0 0],[0 0 0],[-10 0 0],[0 -10 0],[0 0 -10]);
hold on;
O=[0 0 0];A=[1 -4 -6];B=[2 1 0];
C=A+B;
vectarrow(O,A);hold on;
vectarrow(O,B);hold on;
vectarrow(O,C);hold on;
vectarrow(A,C);hold on;
vectarrow(B,C);
Astr=['A (',num2str(A),')'];
Bstr=['B (',num2str(B),')'];
Cstr=['C (',num2str(C),')'];
text(A(1),A(2),A(3),Astr);
text(B(1),B(2),B(3),Bstr);
text(C(1),C(2),C(3),Cstr);
xlabel('x-axis');ylabel('y-axis');zlabel('z-axis');
title('Ex-3.1 Addition of two vectors');
25
”Simulation of Electromagnetic Fields”
26
”Simulation of Electromagnetic Fields”
27
”Simulation of Electromagnetic Fields”
Output
magA = 7.0711
unitvectorA = 0.4243 0.5657 0.7071
28
”Simulation of Electromagnetic Fields”
29
”Simulation of Electromagnetic Fields”
30
”Simulation of Electromagnetic Fields”
Output
crossAB = -22 15 6
magcrossAB = 27.2947
dotAB = 3
magA = 5.0990
magB = 5.3852
costhetaAB = 0.1093
thetaAB1 = 83.7277
sinthetaAB = 0.9940
thetaAB2 = 83.7277
31
”Simulation of Electromagnetic Fields”
32
”Simulation of Electromagnetic Fields”
33
”Simulation of Electromagnetic Fields”
x=1;
y1=-1;y2=1;y3=1;y4=-1;
z1=-1;z2=-1;z3=1;
z4=1;
patch([x x x x x], [y1 y2 y3 y4 y1], [z1 z2 z3 z4 z1],'green');
hold on;
x=-1;
patch([x x x x x], [y1 y2 y3 y4 y1], [z1 z2 z3 z4 z1],'green');
y=1;
x1=-1;x2=1;x3=1;x4=-1;
z1=-1;z2=-1;z3=1;z4=1;
hold on;
patch([x1 x2 x3 x4 x1], [y y y y y], [z1 z2 z3 z4 z1],'blue');
hold on;
y=-1;
patch([x1 x2 x3 x4 x1], [y y y y y], [z1 z2 z3 z4 z1],'blue');
z=1;
x1=-1;x2=1;x3=1;x4=-1;
y1=-1;y2=-1;y3=1;y4=1;
hold on;
patch([x1 x2 x3 x4 x1], [y1 y2 y3 y4 y1], [z z z z z],'red');
hold on;
34
”Simulation of Electromagnetic Fields”
z=-1;
patch([x1 x2 x3 x4 x1], [y1 y2 y3 y4 y1], [z z z z z],'red');
35
”Simulation of Electromagnetic Fields”
36
”Simulation of Electromagnetic Fields”
37
”Simulation of Electromagnetic Fields”
38
”Simulation of Electromagnetic Fields”
39
”Simulation of Electromagnetic Fields”
40
”Simulation of Electromagnetic Fields”
41
”Simulation of Electromagnetic Fields”
42
”Simulation of Electromagnetic Fields”
43
”Simulation of Electromagnetic Fields”
44
”Simulation of Electromagnetic Fields”
Ex 3.19 Evaluation of surface and volume integrals of a cylinder with given ,φ and z
Program 3.19
clc; %clear the command line
clear; %removole all prevolious volariables
VOLol=0; %initialize vololume of the closed surface to 0
% initialize the area of A1-A6 to 0
A1=0;
A2=0;
A3=0;
A4=0;
A5=0;
A6=0;
ro=4; %initialize ro to the its lower boundary
z=8; %initialize z to the its lower boundary
ang=pi/6;%initialize ang to the its lower boundary
No_ro_values=100; %initialize the ro discretization
No_ang_values=100;%initialize the ang discretization
No_z_values=100;%initialize the z discretization
dro=(4-2)/No_ro_values;%The ro increment
dang=(pi/3-pi/9)/No_ang_values;%The ang increment
dz=(5-3)/No_z_values;%The z increment
%%the following routine calculates the vololume of the enclosed surface
for k=1:No_z_values
for j=1:No_ro_values
for i=1:No_ang_values
VOL=VOL+ro*dang*dro*dz;%add contribution to the vololume
end
ro=ro+dro;%p increases each time when z has been travoleled from its lower
boundary to its upper boundary
end
ro=2;%reset ro to its lower boundary
end
%%the following routine calculates the area of A1 and A2
ro1=2;%radius of A1
ro2=4;%radius of a2
for k=1:No_z_values
for i=1:No_ang_values
A1=A1+ro1*dang*dz;%get contribution to the the area of A1
A2=A2+ro2*dang*dz;%get contribution to the the area of A2
end
end
%%the following routing calculate the area of A3 and A4
ro=2;%reset ro to it's lower boundaty
for j=1:No_ro_values
for i=1:No_ang_values
A3=A3+ro*dang*dro;%get contribution to the the area of A3
end
ro=ro+dro;%p increases each time when ang has been travoleled from it's lower
boundary to it's upper boundary
end
A4=A3;%the area of A4 is equal to the area of A3
%%the following routing calculate the area of A5 and A6
for k=1:No_z_values
45
”Simulation of Electromagnetic Fields”
for j=1:No_ro_values
A5=A5+dz*dro;%get contribution to the the area of A3
end
end
A6=A5;%the area of A6 is equal to the area of A6
Surface_area=A1+A2+A3+A4+A5+A6;%the area of the enclosed surface
VOL
Surface_area
Output
VOL = 8.3497
Surface_area = 24.7272
46
”Simulation of Electromagnetic Fields”
47
”Simulation of Electromagnetic Fields”
[x,y]=meshgrid(2,3)
x=
y=
>> [x,y]=meshgrid(2:4,3:5)
48
”Simulation of Electromagnetic Fields”
x=
2 3 4
2 3 4
2 3 4
y=
3 3 3
4 4 4
5 5 5
>> [x,y]=meshgrid(3:4,4:5)
x=
3 4
3 4
y=
4 4
5 5
49
”Simulation of Electromagnetic Fields”
>> [x,y]=meshgrid(3:4,3:5)
x=
3 4
3 4
3 4
y=
3 3
4 4
5 5
>> [x,y]=meshgrid(3:4,3:6)
x=
3 4
3 4
3 4
3 4
y=
50
”Simulation of Electromagnetic Fields”
3 3
4 4
5 5
6 6
>> peaks
- 1/3*exp(-(x+1).^2 - y.^2)
51
”Simulation of Electromagnetic Fields”
4.3 Divergnce:
4.4 Contour:
Contour: Contour plot.
contour(Z) draws a contour plot of matrix Z in the x-y plane, with the x-
coordinates of the vertices corresponding to column indices of Z and the y-
coordinates corresponding to row indices of Z. The contour levels are chosen
automatically.
( ⁄ )
4.6 Calculate and plot the divergence of a vector field where
r=xax+yay
v=[-2:0.1:2];
[x,y]=meshgrid(v);
z=x;
r=sqrt(x.^2 + y.^2);
52
”Simulation of Electromagnetic Fields”
a=r.*exp(-(r./2).^2);
div = divergence(r,a);
contour(v,v,div);
hold on;
quiver(div,r);
1.5
0.5
-0.5
-1
-1.5
-2
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
53
”Simulation of Electromagnetic Fields”
20
18
16
14
12
10
2 4 6 8 10 12 14 16 18 20
54
”Simulation of Electromagnetic Fields”
-1
-2
-3
2
1 4
0 2
0
-1 -2
-2 -4
55
”Simulation of Electromagnetic Fields”
[cx,cy,cz]=curl(X,Y,Z,U,V,W);
quiver3(X,Y,Z,cx,cy,cz)
-1
-2
-3
4
2 4
0 2
0
-2 -2
-4 -4
56
”Simulation of Electromagnetic Fields”
-1
-2
-3
4
2 4
0 2
0
-2 -2
-4 -4
57
”Simulation of Electromagnetic Fields”
5. Exercise Problems
1. Find the angle between the vectors A=10 ax+5ay ,B=12ax-2ay+3az using dot
product and cross product.
2. Given A=-4 ax+2ay+3az and B=3ax+4ay-az. Find the vector component of A
parallel to B.
3. Find the sum of P=3 ax+4ay+5az and T=-5ax+4ay-3az using parallelogram law of
addition.
4. Find the sum of Q=10 ax-6ay+8az and R=-ax+12ay-7az using triangular law of
addition.
5. Draw the position vector of points D(3,0,-5) and F(-9,2,-1).
6. Let A=-2ax+3ay+4az;B=7ax+ay+2az and C=-ax+2ay+4az.Find
i) AXB ii) (AXB).C iii) A.(BXC)
7. Given vectors A=ax+2ay+5az and B=5ax-ay+3az. Find the vector component of A
along B.
8. Show that vectors a = (4, 0,- 1) , b = (1,3, 4), and c = (-5,- 3,- 3) form the sides of
a triangle.
9. If the position vectors of points T and S are 3ax-2ay+az and 4ax+6ay+2az find: (a)
the coordinates of T and S, (b) the distance vector from T to S, (c) the distance
between T and S.
10. Calculate the angles that vector H = 3ax + 5ay - 8az makes with the x-,y-, and z-
axes.
Dr.G.Pranava
Assistant Professor
EEE Department,VCE(A)
pranava.gopu@staff.vce.ac.in
9966744474
Dr.Ch.Ch.V.S.S.Sailaja
Associate Professor
EEE Department,VCE(A)
ch.sailaja@staff.vce.ac.in
8121981253
Dr.M.Chakravarthy
Professor,HoD EEE
hodeee@staff.vce.ac.in
9849979136
58