3D vector plot of electric field

134 views (last 30 days)
Quant.Phys42
Quant.Phys42 on 30 Jan 2020
Edited: David Goodmanson on 30 Jan 2020
I'm having some issues with 3D vector plotting an electric field assoicated with a charge over some range (picture attached)
This is what i have thus far:
x=linspace(0,2,10);
y=linspace(0,2,10);
z=linspace(0,2,10);
[XX,YY,ZZ]=meshgrid(x,y,z)
Q=1.e-9
C=9.e9
r=(x.^2+y.^2+z.^2).^.5
Ex=Q*C/r.^3*x
Ey=Q*C/r.^3*y
Ez=Q*C/r.^3*z
figure()
quiver3(x,y,z,Ex,Ey,Ez)
view(-35,45)

Answers (2)

David Goodmanson
David Goodmanson on 30 Jan 2020
Edited: David Goodmanson on 30 Jan 2020
Hello QP,
Once you have made the matrix variables XX,YY,ZZ, those are the ones you use for further calculation, no longer x,y,z. (Notationally, you could create the matrices with names x,y,z so as to not have to use the more cumbersome XX etc. further on down). In addition, you have not made use of the position of the charge. It's the displacements from the charge location that matter. Displacements in the x direction are XX-x0, etc. All of this becomes
x0 = 1;
y0 = 1;
z0 = 4;
r=((XX-x0).^2+(YY-y0).^2+(ZZ-z0).^2).^.5;
Ex = Q*C*(XX-x0)./r.^3;
etc.
quiver3(XX,YY,ZZ,Ex,Ey,Ez)
Several times ./ and .* are required, for element-by-element multiplication of variables.

KSSV
KSSV on 30 Jan 2020
Edited: KSSV on 30 Jan 2020
m = 10 ; n = 10 ; p = 10 ;
x=linspace(0,2,m);
y=linspace(0,2,n);
z=linspace(0,2,p);
[x,y,z]=meshgrid(x,y,z) ;
Q=1.e-9 ;
C=9.e9 ;
r=(x.^2+y.^2+z.^2).^.5;
Ex = Q*C./r.^3.*x ;
Ey = Q*C./r.^3.*y ;
Ez = Q*C./r.^3.*z ;
figure()
hold on
for i = 1:p
quiver3(x(:,:,i),y(:,:,i),z(:,:,i),Ex(:,:,i),Ey(:,:,i),Ez(:,:,i))
end
view(-35,45)
Without loop, you can also draw in a single stretch using:
quiver3(x(:),y(:),z(:),Ex(:),Ey(:),Ez(:),3)
When you use the above one, you need to specify the scaling factor and size of the arrows.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!