How to plot a 3D graph?

1 vue (au cours des 30 derniers jours)
MUHAMMAD NURADZEEM KAMARULBAHRIN
Modifié(e) : John D'Errico le 2 Juin 2021
Hi, I'm new to matlab.
I'm trying to plot the 3D graph of this equation
for (-1 <= x <= 1), (-1 <= y <= 1.5), and (1 <= z <= 6) but I get errors:
Error using surf (line 71)
X, Y, Z, and C cannot be complex.
Error in heart (line 8)
surf(X,Y,Z);
Here is my code:
x = linspace(-1,1,20);
y = linspace(-1,1.5,30);
[X,Y] = meshgrid(x,y);
Z = 5 - sqrt(1 - X.^2 - (Y - abs(X)).^2).*cos(30.*(1 - X.^2 - (Y - abs(X)).^2));
surf(X,Y,Z);
zlim([1 6]);
xlim([-1 1]);
ylim([-1 1.5]);
Please help me fix this code, thank you.

Réponse acceptée

John D'Errico
John D'Errico le 2 Juin 2021
Modifié(e) : John D'Errico le 2 Juin 2021
This is a perfect problem for a tool like ezsurf.
Z = @(X,Y) 5 - sqrt(1 - X.^2 - (Y - abs(X)).^2).*cos(30.*(1 - X.^2 - (Y - abs(X)).^2));
H = ezsurf(Z,[-1,1,-1,1.5],200);
view(-11,81)
shading interp
As you can see, the surface is a fairly complicated one. But it may be most apprpriate to plot this on the 14th of February.

Plus de réponses (1)

KSSV
KSSV le 2 Juin 2021
Z matrix is complex number. Try plotting real, imag or abs.
surf(X,Y,real(Z)) % real value
surf(X,Y,imag(Z)) % imaginary value
surf(X,Y,abs(Z)) % absolute value

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by