MATLAB Examples

This is an example of how to create a surface plot from a function in MATLAB®.

Read about the fsurf function in the MATLAB documentation. This function is available in R2016a or newer.

For more examples, go to MATLAB Plot Gallery

% Check version
if verLessThan('matlab','9.0')
    error(['fsurf is available in R2016a or newer. ', ...
        'For older releases, use ezsurf instead.'])
end

% Create the plot using the functions
% x = sin(pi*u)*sin(pi*u)*cos(v) ; y = sin(pi*u)*sin(pi*u)*sin(v) ; z = u
% with - 1 < u < 1 and 0 < v < 2*pi
figure
fsurf(@(u,v)sin(pi*u).*sin(pi*u).*cos(v), ...
    @(u,v)sin(pi*u).*sin(pi*u).*sin(v), ...
    @(u,v)u, [-1 1 0 2*pi])

% Add labels and title
xlabel('x')
ylabel('y')
zlabel('z')
title({'x = sin(\pi*u)*sin(\pi*u)*cos(v)', ...
    'y = sin(\pi*u)*sin(\pi*u)*sin(v)', ...
    'z = u'})

% Change the view angle for the plot
view(135,15)