Plot 3D vector field
6 views (last 30 days)
Show older comments
I'm trying to plot a 3D vector field, the values of Ex Ey and Ez, which are functions r, the distance between a fixed point (x,y,z) and a meshgrid of points in all 3 dimensions (in the code its IX, IY, IZ), and are also functions of ux, uy, uz, which in turn are functions of r. I keep getting errors in the surfnorm function that X and Y have to be the same size as Z, but I checked their size and it says that they are the same length, 21x21x21. Here's what I have now. Any help would be appreciated, thanks in advance.
% Charged particles
q = 1.602e-19; %charge on proton
m = 1.67e-27; %mass of proton
k = 8.99e9; %coulombs constant
c = 3.0e8; %speed of light
%trajectory
t = 0;
x = 5*cos(t);
y = 5*sin(t);
z = sin(t)+cos(t)+t;
%plot3(x,y,z,'*r');
%sample velocity
vx = -5*sin(t);
vy = 5*cos(t);
vz = cos(t) - sin(t) + 1;
%sample acceleration
ax = -5*cos(t);
ay = -5*sin(t);
az = -sin(t) - cos(t);
%magnitude of r
ix = -10:10; %observation points
iy = -10:10;
iz = -10:10;
[IX,IY,IZ] = meshgrid(ix,iy,iz);
r = sqrt((IX-x).^2+(IY-y).^2+(IZ-z).^2); %% this is |r-r'|- the displacement vector used in Ex, Ey, Ez
%trying to plot a surfnorm of r before using it in Ex Ey Ez
[U, V, W] = surfnorm(r);
figure
quiver3(U,V,W,r)
% u values
ux = c*(IX-x)./r-vx; %here, u is a function of r & x &IX
uy = c*(IY-y)./r-vy;
uz = c*(IZ-z)./r-vz;
%u x a
uax = uy.*az-ay.*uz;
uay = -(ux.*az-ax.*uz);
uaz = ux.*ay-ax.*uy;
%% here I need a vector field of the electric and magnetic fields
% w/r/t different observation points defined by r and u
%electric field
Ex = k*q*r./((x-IX).*ux).*[(c^2-vx.^2).*ux + ((IY-y).*uaz - uay.*(IZ-z))];
Ey = k*q*r./((y-IY).*uy).*[(c^2-vy.^2).*uy - ((IX-x).*uaz - uax.*(IZ-z))];
Ez = k*q*r./((z-IZ).*uz).*[(c^2-vz.^2).*uz + ((IZ-z).*uay - uax.*(IY-y))];
2 Comments
Star Strider
on 3 Dec 2018
The surfnorm documentation section on Input Arguments (link) indicates that the matrices have to be 2-D, not 3-D matrices.
You might consider working with the isosurface (link) and other Volume Visualization (link) functions instead.
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!