A Q
A Q

Reputation: 166

parametric function plotting in matlab

I need to plot x = cos(u)*cos(v), y=cos(v)*sin(u), z = -sin(v) where u,v both are from (0,\pi). I looked at fplot3 function but it takes only one parameter. Can anyone point to some function or is there some other way like writing a script to do the plotting?

Upvotes: 0

Views: 975

Answers (2)

Florian
Florian

Reputation: 1824

I would use meshgrid for this:

u = linspace(0,pi,51);
v = linspace(0,pi,51);
[U,V] = meshgrid(u,v);
X =  cos(U).*cos(V);
Y =  sin(U).*cos(V);
Z = -sin(V);

Then you can plot it as a mesh (using mesh(X,Y,Z)) or as a set of lines (e.g., shown here via plot3(X',Y',Z')), as you like:

Plotted via mesh Plotted via plot3

Upvotes: 2

rinkert
rinkert

Reputation: 6863

You can use fsurf for parametric surface plots. fsurf accepts two inputs, u, and v.

% your functions
f_x = @(u,v) cos(u).*cos(v);
f_y = @(u,v) cos(v).*sin(u);
f_z = @(u,v) -sin(v);

% plot
umin = 0;
umax = pi;
vmin = 0;
vmax = pi;

figure(1); clf;
fsurf(f_x,f_y,f_z, [umin umax vmin vmax])

enter image description here

You can also check out fcontour and fmesh.

Upvotes: 4

Related Questions