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?

Answers


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


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


Need Your Help

Adding Service Reference for WCF Service generates empty reference.cs

c# .net wcf service-reference

I have seen this question before (here for one), however the solution is to not reuse referenced assemblies.

ListBox ItemTemplate Hit Test

wpf mvvm

I have the following ListBox with an ItemTemplate

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.