0% found this document useful (0 votes)
149 views3 pages

Code in MATLAB:: Exercise 1: Plot Electric Field of A Charge

The document contains MATLAB code to plot the electric field and equipotential lines of a single point charge. The code first defines constants and sets up grids for the x and y coordinates. It then calculates the electric field components Ex and Ey and the electric potential V at each point due to a charge q1 located at coordinates xq1 and yq1. Finally, it uses streamslice and contour functions to plot the electric field vectors and equipotential lines on the same graph.

Uploaded by

Trung Nguyen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
149 views3 pages

Code in MATLAB:: Exercise 1: Plot Electric Field of A Charge

The document contains MATLAB code to plot the electric field and equipotential lines of a single point charge. The code first defines constants and sets up grids for the x and y coordinates. It then calculates the electric field components Ex and Ey and the electric potential V at each point due to a charge q1 located at coordinates xq1 and yq1. Finally, it uses streamslice and contour functions to plot the electric field vectors and equipotential lines on the same graph.

Uploaded by

Trung Nguyen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Exercise 1: Plot electric field of a charge

Code in MATLAB:
clear all;
eps0 = 8.854e-12;
[x,y] = meshgrid(-2:0.2:2, -2:0.2:2);
q1=1;
xq1= -0.5;
yq1= -0.5;
R1 = sqrt((x-xq1).^2+(y-yq1).^2);
E1x= 1e+4*q1.*(x-xq1)./(4*pi*eps0.*R1.^3);
E1y= 1e+4*q1.*(y-yq1)./(4*pi*eps0.*R1.^3);

figure
streamslice(x,y, E1x, E1y);
axis([-2 2 -2 2]);
hold on;
title 'Thanh Quang-Duc Trung';
xticks([-2 -1 0 1 2]);
xlabel('x [cm]');
yticks([-2 -1 0 1 2]);
ylabel('y [cm]');
hold off;
colormap('jet');

The result:
Exercise 2: Plot equipotential lines

Code in MATLAB:
clear all;
eps0 = 8.854e-12;
[x,y] = meshgrid(-2:0.2:2, -2:0.2:2);
q1=1;
xq1= -0.5;
yq1= -0.5;
R1 = sqrt((x-xq1).^2+(y-yq1).^2);
E1x= 1e+4*q1.*(x-xq1)./(4*pi*eps0.*R1.^3);
E1y= 1e+4*q1.*(y-yq1)./(4*pi*eps0.*R1.^3);
V1=q1./(4*pi*eps0.*R1);
figure
contour(x,y,V1,21, 'linewidth', 1);
hold on;
title 'Thanh Quang-Duc Trung';
streamslice(x,y, E1x, E1y);
axis([-2 2 -2 2]);
xticks([-2 -1 0 1 2]);
xlabel('x [cm]');
yticks([-2 -1 0 1 2]);
ylabel('y [cm]');
hold off;
colormap('jet');

The result:

You might also like