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

7 ビュー (過去 30 日間)
Charles
Charles 2023 年 6 月 26 日
コメント済み: Charles 2023 年 6 月 27 日
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.

採用された回答

DGM
DGM 2023 年 6 月 26 日
編集済み: DGM 2023 年 6 月 26 日
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 件のコメント
DGM
DGM 2023 年 6 月 26 日
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])
Charles
Charles 2023 年 6 月 27 日
Awesome! Thank you, this is great!

サインインしてコメントする。

その他の回答 (1 件)

Dyuman Joshi
Dyuman Joshi 2023 年 6 月 26 日
編集済み: Dyuman Joshi 2023 年 6 月 26 日
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')
  1 件のコメント
Charles
Charles 2023 年 6 月 26 日
Thanks! This is also a nice method. Thank you for the help!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAnimation についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by