Mike James Johnson
Mike James Johnson

Reputation: 734

How to plot a parametric surface in Matlab

I have a parametric B-Spline surface, S

S=[x(:);y(:);z(:)];

Right now, I am plotting the surface by just plotting each column of S as a single point:

plot3(S(1,:),S(2,:),S(3,:),'.')

The result is this:

S plotted as a collection of points

Unfortunately, by plotting individual points, we lose the sense of depth and curvy-ness when we look at this picture.

Any ideas on how to implement SURF or MESH command for a parametric surface? These functions seem to require a matrix representing a meshgrid which I dont think I can use since the X x Y domain of S is not a quadrilateral. However, I like the lighting and color interpolation that can be conveniently included when using these functions, as this would fix the visualization problem shown in figure above.

I am open to any other suggestions as well.

Thanks.

Upvotes: 0

Views: 2138

Answers (1)

Suever
Suever

Reputation: 65460

Without seeing your equations it's hard to offer an exact solution, but you can accomplish this by using fsurf (ezsurf if you have an older version of MATLAB).

There are specific sections regarding plotting parametric surfaces using ezsurf and fsurf

syms s t
r = 2 + sin(7*s + 5*t);
x = r*cos(s)*sin(t);
y = r*sin(s)*sin(t);
z = r*cos(t);

fsurf(x, y, z, [0 2*pi 0 pi])   % or ezsurf(x, y, z, [0 2*pi 0 pi])

If you want to have a piece-wise function, you can either write a custom function

function result = xval(s)
    if s < 0.5
        result = 1 - 2*s;
    else
        result = 2 * x - 1;
    end
end

And pass a function handle to fsurf

fsurf(@xval, ...)

Or you can define x to be piece-wise using a little bit of manipulation of the function

x = (-1)^(s > 0.5) * (1 - 2*s)

Upvotes: 1

Related Questions