Reputation: 1373
I have a function expressed in spherical coordinates:
f(r,theta,phi) = 4*exp(-r)*cos(theta)*sin(phi)
I'd like to plot this in MATLAB in these ways:
Is there a straightforward way to do this?
Upvotes: 1
Views: 6863
Reputation: 7046
Just do the conversion and plot in Cartesian coordiantes:
f = @(r, theta, phi) 4*exp(-r).*cos(theta).*sin(phi)
[XX YY ZZ] = meshgrid(x_range, y_range, z_range)
% R = sqrt(XX.^2 + YY.^2 + ZZ.^2)
% Th = acos(XX./YY)
% Phi = acos(ZZ./R)
% This is faster. . . and significantly more correct. See the comments below.
[Th,Phi,R] = cart2sph(XX,YY,ZZ)
fvals = f(R, Th, Phi)
I like isosurface
to visualize 3D data like this. For the 2D slice through Z=0 you could use imagesc(fvals(:,:,N))
or contour(fvals(:,:,N))
Upvotes: 1
Reputation: 21643
You can use sph2cart()
to convert the coordinates, then use plot()
/plot3()
to plot the function.
Upvotes: 1