Help plotting equations in 3D
5 views (last 30 days)
Show older comments
Joseph Nettesheim
on 25 Aug 2015
Commented: Joseph Nettesheim
on 26 Aug 2015
I am looking to plot the following in MATLAB to create a figure similar to the picture attached.
The equations are:
x=a*cos(phi);
y=a*sin(phi);
z=sqrt((c.^2./(r-(x.^2 + y.^2))).*(r-((1+epsilon).*(x.^2 + y.^2))));
where r=(a-A.*((y.^2.*(3.*x.^2-y.^2))./(x.^2+y.^2)).^B).^2;
And constants are:
a=1;
A=0.1339;
B=0.397;
c=1;
phi=0:0.001:2*pi;
epsilon=10.^-5;
NOTE: The original z equation is attached (I just solved for z and made a variable r to save space):
I have tried using plot3(X,Y,Z) using
[X,Y]=meshgrid(x,y);
z=peaks(6284);
because I found something similar in another tread.
The result is:
I appreciate any help you can offer!
2 Comments
Star Strider
on 25 Aug 2015
The plot3 function requires three column vectors for its arguments. It doesn’t use meshgrid.
It will help if you post your code, or attach it if it is longer than about 20 lines. (Use the ‘paperclip’ icon and complete both steps.)
Accepted Answer
Mike Garrity
on 25 Aug 2015
Actually, I feel like we're missing a piece of the question here.
In your code, meshgrid and peaks(6284) are creating 2D arrays. That's what you want when you're drawing a parametric surface where each point is a function of 2 parameter values. But it doesn't look like you've got a parametric surface.
From the picture, it kind of looks like you're trying to draw a parametric curve where each point is a function of a single parameter value (perhaps phi?). In that case, you're going to be generating 1D arrays for your X, Y, and Z coordinates. As Star Strider and Walter said, once you have these 1D arrays, you're probably going to pass them to the plot3 function.
But that doesn't look like what you've got either. In your code, you're generating Z as a function of X and Y, rather than as a function of the parameter value phi. But that doesn't match your picture, because in your picture there are multiple Z values for the same X & Y pair.
On the other hand, your function actually looks like an implicit surface. This is where you're drawing a surface through all of the points where the left hand side of the equation are exactly equal to 1. But, if I code that up:
cla
n = 100;
[y,x,z] = ndgrid(linspace(-.5,.5,n), ...
linspace(-.5,.5,n), ...
linspace(-1,1,n));
r = (a-A.*((y.^2.*(3.*x.^2-y.^2))./(x.^2+y.^2)).^B).^2;
f = ((x.^2+y.^2)./r).*(1+epsilon-z.^2/c.^2) + z.^2./c.^2;
isosurface(x,y,z,f,1)
view(3)
camlight
xlabel('X')
ylabel('Y')
zlabel('Z')
Then I get this:
which doesn't look at all like your picture.
Perhaps you could give us a bit more background?
4 Comments
More Answers (0)
See Also
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!