I want to plot a heart shape wireframe as shown in the following image (source):
I have tried to make it by using this MATLAB program:
n=100;
x=linspace(-3,3,n);
y=linspace(-3,3,n);
z=linspace(-3,3,n);
[X,Y,Z]=ndgrid(x,y,z);
F=((-(X.^2) .* (Z.^3) -(9/80).*(Y.^2).*(Z.^3)) + ((X.^2) + (9/4).* (Y.^2) + (Z.^2)-1).^3);
isosurface(F,0)
lighting phong
caxis
axis equal
colormap('flag');
view([55 34]);
But I didn't get the desired shape of framework as shown in the figure.
I have identified the problem: to create a wireframe we usually use the command mesh()
. But this plotting facility only allow us to plot a function of two variables such as z=f(x,y)
. But my program makes use of three variables: F(x,y,z)
.
How can I solve the problem?
Here's my best attempt at reproducing the entire figure:
Generating the contoured heart mesh:
I used the
contourc
function to generate a series of contours in the x-y, x-z, and y-z planes. Notice that in the image you want to reproduce, the mesh lines on the back-facing side of the heart are not rendered. The quickest and easiest way I could think of to reproduce that aspect of the plot was to useisosurface
to render a white surface just beneath the inside surface of the mesh, blocking the view of the back side.Here's the code for the function
heart
:Putting the figure together:
To reproduce the entire figure I first generated the heart mesh using the
heart
function above, then added the other elements around it. I also used a few submissions from The MathWorks File Exchange:Here's the code for the function
I_Heart_Math
(which generates the above figure):This code plots the shaded surface:
We could instead plot the wireframe only:
A very elegant solution is given by @gnovice. I though I extend it by adding the other elements to replicate the figure pointed by the OP. I also added some cool animations!
(The above GIF file was created using GETFRAME and IMWRITE).