1

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:

  1. R3
  2. R2 Contour Plot (x-y plane or x-z plane or y-z plane)

Is there a straightforward way to do this?

2 Answers 2

1

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))

Sign up to request clarification or add additional context in comments.

6 Comments

The conversion is a bit off, should be: Th=atan2(YY,XX); Phi=asin(ZZ./R);, but preferably use CART2SPH as it is designed to avoid numerical issues
Is there a less ugly way to do this?
@Amro : How could the above code be modified to use sph2cart()?
@nick_name: [Th,Phi,R] = cart2sph(XX,YY,ZZ). Also looks to me you have volumetric data (ie for each (x,y,z) you have a value), so you want to use functions like ISOSURFACE and SLICE as suggested..
Thanks for the peer review @Amro. I was a bit careless. I've updated the answer to make it better.
|
1

You can use sph2cart() to convert the coordinates, then use plot()/plot3() to plot the function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.