ReportEM Lab2
ReportEM Lab2
Where R1 = ( x−x q 1 ). ^x + ( y − y q 1 ) ∙ ^y is the position vector of the point (x, y) with respect to
the charge.
MATLAB code:
1. clear all;
2.
3. % Define the vacuum electric permittivity
4. eps0 = 8.854e-12;
5.
6. % Create a x-y grid [-2cm -> 2cm] x [-2cm -> 2cm]
7. [x,y] = meshgrid(-2:0.2:2, -2:0.2:2);
8.
9. % Define the charge q1
10.q1 = 1; % charge q1 = +1 Coulomb
11.% Coordinate of the charge q1: P1(xq1, yq1)
12.% Note that
13.% to avoid division by zero
14.xq1 = -0.5; % xq1 = -0.5 cm
15.yq1 = -0.5; % yq1 = -0.5 cm
16.
17.% Calculate distance from each grid point
18.% to the point charge q1
19.R1 = sqrt((x-xq1).^2+(y-yq1).^2);
20.% Note that: '.^' is elementwise exponential
21.
22.% Calculate x-component of the electric field
23.% Note that: '.*' is elementwise multiplication
24.% './' is elementwise division
25.E1x = 1e+4*q1.*(x-xq1)./(4*pi*eps0.*R1.^3);
26.% Calculate y-component of the electric field
27.E1y = 1e+4*q1.*(y-yq1)./(4*pi*eps0.*R1.^3);
28.
29.%Plot the E-field
30.figure
31.streamslice(x, y, E1x, E1y);
32.axis([-2 2 -2 2]);
33.hold on;
34.xticks([-2 -1 0 1 2]);
35.xlabel('x [cm]');
36.yticks([-2 -1 0 1 2]);
37.ylabel('y [cm]');
38.hold off;
39.subtitle('QuynhAnh-HanhPhuc');
40. colormap('jet');
The plot:
The plot:
3. Exercise 3: Electric field and equipotential lines of two charges
3.1 Write your program
In this exercise, you have to write a MATLAB program to plot electric field and
equipotential lines of two charges q 1 = +1C and q 2 = -1C which co-locate in the same
space. The charge q 1 is located at the position P1(-0.5 cm, -0.5 cm), and the charge q 2 is
located at the position P2(0.5 cm, 0.5 cm). Note that the electric field is the
superposition of fields generated by each charge. Refer to previous exercises to write
your program. Briefly describe the electric field and equipotential lines.
MATLAB code:
1. clear all;
2.
3. % Define the vacuum electric permittivity
4. eps0 = 8.854e-12;
5.
6. % Create a x-y grid [-2cm -> 2cm] x [-2cm -> 2cm]
7. [x,y] = meshgrid(-2:0.2:2, -2:0.2:2);
8.
9. % Define the charge q1
10.q1 = 1; % charge q1 = +1 Coulomb
11.% Coordinate of the charge q1: P1(xq1, yq1)
12.% Note that
13.% to avoid division by zero
14.xq1 = -0.5; % xq1 = -0.5 cm
15.yq1 = -0.5; % yq1 = -0.5 cm
16.
17.% Define the charge q2
18.q2 = -1; % charge q2 = -1 Coulomb
19.% Coordinate of the charge q2: P2(xq2, yq2)
20.% Note that
21.% to avoid division by zero
22.xq2 = 0.5;
23.yq2 = 0.5;
24.
25.% Calculate distance from each grid point
26.% to the point charge q1 & q2
27.R1 = sqrt((x-xq1).^2+(y-yq1).^2);
28.R2 = sqrt((x-xq2).^2+(y-yq2).^2);
29.% Note that: '.^' is elementwise exponential
30.
31.% Calculate x-component of the electric field
32.% Note that: '.*' is elementwise multiplication
33.% './' is elementwise division
34.E1x = 1e+4*q1.*(x-xq1)./(4*pi*eps0.*R1.^3);
35.E2x = 1e+4*q2.*(x-xq2)./(4*pi*eps0.*R2.^3);
36.Ex=E1x+E2x;
37.% Calculate y-component of the electric field
38.E1y = 1e+4*q1.*(y-yq1)./(4*pi*eps0.*R1.^3);
39.E2y = 1e+4*q2.*(y-yq2)./(4*pi*eps0.*R2.^3);
40.Ey=E1y+E2y;
41.
42.% Calculate voltage
43.V1 = 1e+2*q1./(4*pi*eps0.*R1);
44.V2 = 1e+2*q2./(4*pi*eps0.*R2);
45.V=V1+V2;
46.
47.% Plot the E-field
48.figure
49.contour(x, y, V, 21, 'linewidth', 0.4);
50.hold on;
51.streamslice(x, y, Ex, Ey);
52.axis([-2 2 -2 2]);
53.hold on;
54.title('Hanh Phuc - Quynh Anh');
55.xticks([-2 -1 0 1 2]);
56.xlabel('x [cm]');
57.yticks([-2 -1 0 1 2]);
58.ylabel('y [cm]');
59.hold off;
60.colormap('jet');
The plot:
Comment:
Two charges interact with each other.
Charges of the same sign repel each other.
Charges with different signs attract each other.
The plot:
q2 = -2 C
q2 = 2 C
q2 = 2 C
Comment:
Two charges interact with each other.
Charges of the same sign repel each other.
Charges with different signs attract each other.
4. Exercise 4: Electric field and equipotential lines of two charges
In this exercise, you have to write a MATLAB program to plot electric field and
equipotential lines of four charges:
q 1 = +1 C at P1(-0.5 cm, -0.5 cm),
Comment:
Four charges interact with each other.
Charges of the same sign repel each other.
Charges with different signs attract each other.