Code in MATLAB:: Exercise 1: Plot Electric Field of A Charge
Code in MATLAB:: 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: