Create a surface from a 2D function in Matlab

We might be interested in creating a surface from a 2D function. Matlab's command "cylinder", although does something similar, will not be of help for this. Here's how to do it:

"f" function
Suppose we want to revolve a function "f" around the "z" axis. Let's start by defining the range of the "x" axis as:

>> x = 0:10:350;

Next define the range of angles and the number of elements.

>> angs = 0 : 10 : 180;

Now, rotate the axis "x" and "y" by doing:

for i = 1 : length(angs)
    X(:,i) = x * cosd(angs(i));
    Y(:,i) = x * sind(angs(i));
end

We will store the values of "f" in the Z matrix by doing:

>> Z = repmat(f,1,length(angs));

That's it. We now plot the surface with:

surfc(X,Y,Z,'FaceColor','interp',...
    'EdgeColor','none',...
    'FaceLighting','phong')

Here's the result:

2 comments:

  1. hello, I tried this, but it is not working for me, might be how i defined my function, could you post an example that can run by itself?

    ReplyDelete
  2. Hi there, just paste your function here to check what's wrong. Best!

    ReplyDelete