nick_name
nick_name

Reputation: 1373

MATLAB - Plot a Function Currently Expressed in Spherical Coordinates

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?

Upvotes: 1

Views: 6863

Answers (2)

Carl F.
Carl F.

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

Mansoor Siddiqui
Mansoor Siddiqui

Reputation: 21643

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

Upvotes: 1

Related Questions