How to create a 3-D Cardioid shape in MatLab? (a cylinder with a cardioid base)

8 views (last 30 days)
Currently, I am able to plot a 2-D cardioid in MatLab using this code:
% 2-Dimensional Cardioid
theta = linspace(0, 2*pi);
r = 1-(0.7*sin(theta));
plot(r.*cos(theta) *1.25, (r.*sin(theta) *1.25))
axis equal
xlabel('x-axis')
ylabel('y-axis')
zlabel('z-axis')
xlim([-3.5 3.5])
ylim([-3.5 3.5])
zlim([-3.5 3.5])
However, I would like to plot a 3-D cardioid, such that there is the z-dimension included.
(At the moment, the cardioid is only in 2-D, as it solely includes the x-dimension and y-dimension)
Here is the idea I want to visualize in MatLab please:
(The idea is a 3D cylinder with a cardioid-shaped base)
Is anyone able to provide some advice or insight into how to create this please? Thank you for your help.

Accepted Answer

DGM
DGM on 26 Jun 2023
Edited: DGM on 26 Jun 2023
Here's one way.
% 2-Dimensional Cardioid
theta = linspace(0, 2*pi);
r = 1-(0.7*sin(theta));
x = r.*cos(theta)*1.25;
y = r.*sin(theta)*1.25;
% expand everything
zlevels = 10;
zrange = [-1 1];
X = repmat(x,[zlevels 1]);
Y = repmat(y,[zlevels 1]);
Z = linspace(zrange(1),zrange(2),zlevels);
Z = repmat(Z.',[1 numel(x)]);
% plot using surf()
hs = surf(X,Y,Z);
hs.FaceColor = [0.65 0.6 1];
hl = lightangle(gca,45,45);
material(hs,[0.5 1 1])
set(gca,'projection','perspective')
view([-46 38])
axis equal
xlabel('x-axis')
ylabel('y-axis')
zlabel('z-axis')
xlim([-2 2])
ylim([-2.5 1.5])
zlim([-2 2])
You'll probably want to play with the colormapping and lighting as you see fit.
  4 Comments
DGM
DGM on 26 Jun 2023
I was just trying to approximate the figure setup that would have been used to create the sample image. All of the additional fluff (lighting, color, projection) can simply be omitted.
% 2-Dimensional Cardioid
theta = linspace(0, 2*pi);
r = 1-(0.7*sin(theta));
x = r.*cos(theta)*1.25;
y = r.*sin(theta)*1.25;
% expand everything
zlevels = 10;
zrange = [-1 1];
X = repmat(x,[zlevels 1]);
Y = repmat(y,[zlevels 1]);
Z = linspace(zrange(1),zrange(2),zlevels);
Z = repmat(Z.',[1 numel(x)]);
% plot using surf()
surf(X,Y,Z);
axis equal
xlabel('x-axis')
ylabel('y-axis')
zlabel('z-axis')
xlim([-2 2])
ylim([-2.5 1.5])
zlim([-2 2])

Sign in to comment.

More Answers (1)

Dyuman Joshi
Dyuman Joshi on 26 Jun 2023
Edited: Dyuman Joshi on 26 Jun 2023
Use the implicit equation of Cardiod as input to fimplicit3 and define x, y and z limits required -
% 2-Dimensional Cardioid
a = 1.25;
%Equation referenced from Wikipedia, not the same as used above
eqn = @(x,y,z) (x.^2+y.^2).^2 + 4.*a.*x.*(x.^2+y.^2) - 4.*a^2.*y.^2;
%The intervals defined are [xmin xmax ymin ymax zmin zmax]
fimplicit3(eqn, [-2*pi 2*pi -5 5 -2 2])
xlabel('x-axis')
ylabel('y-axis')
zlabel('z-axis')

Products


Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!