quiver3 using 3D spherical coordinates?

3 views (last 30 days)
John Draper
John Draper on 5 Jan 2016
Commented: Bin Jiang on 2 Mar 2023
Hi, I've read a lot of posts on this site about using quiver3 to plot a function in non-rectangular coordinates and I am still a little bit confused.
Here is my attempt to plot a 3D function in spherical coordinates using quiver3
if true
[r,theta,phi] = meshgrid(0:0.1:100,-pi/2:pi/100:pi/2, 0:pi/100:pi); %defines meshgrid coverage
%r=(X.^2+Y.^2+Z.^2).^0.5 ;
const=1; %mu_0 * M_4 / 4*pi
Br= const*2*cos(theta)./(r.^3); %mag field in rho
Btheta= const*sin(theta)./(r.^3);%mag field in theta
Bphi=0;
%magnetic field function
figure
quiver3(r,theta,phi,Br,Btheta,Bphi,5)% no defines arrow length, other define direction
%defines arrow direction (u,v,w) at point (x,y,z)
hold on
%surf(X,Y,Z) %produces surface
axis equal %defines axis parameters
hold off
% code
end
This didn't seem to work (I got an error saying V and W need to be the same size). So I tried converting to rectangular before using the quiver function.
if true
[r,theta,phi] = meshgrid(0:0.1:100,-pi/2:pi/100:pi/2, 0:pi/100:pi); %defines meshgrid coverage
%r=(X.^2+Y.^2+Z.^2).^0.5 ;
const=1; %mu_0 * M_4 / 4*pi
U= const*2*cos(theta)./(r.^3); %mag field in rho
V= const*sin(theta)./(r.^3);%mag field in theta
W=0;
%magnetic field function
r=(X.^2+Y.^2+Z.^2).^0.5 ;
X=r.*sin(phi).*cos(theta);
Y=r.*sin(phi).*sin(theta);
Z=r.*cos(phi);
figure
quiver3(X,Y,Z,U,V,W,5)% no defines arrow length, other define direction
%defines arrow direction (u,v,w) at point (x,y,z)
hold on
%surf(X,Y,Z) %produces surface
axis equal %defines axis parameters
hold off
% code
end
Which doesn't seem to work either. Is it not possible to use spherical coordinate in quiver or am I doing something wrong?
Thanks, John
  2 Comments
John Draper
John Draper on 5 Jan 2016
After doing some more reading, I think I realise the answer to my question is no. It is better to convert back into Cartesian and then use the quiver3 function.
Bin Jiang
Bin Jiang on 2 Mar 2023
It seems that you only specified one element to W, while it should be something like sz=size(U); W= zeros(sz). Perhaps, there is a better way!

Sign in to comment.

Answers (0)

Categories

Find more on Vector Fields in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!