How to plot a 3D graph?

2 Ansichten (letzte 30 Tage)
MUHAMMAD NURADZEEM KAMARULBAHRIN
Bearbeitet: John D'Errico am 2 Jun. 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.

Akzeptierte Antwort

John D'Errico
John D'Errico am 2 Jun. 2021
Bearbeitet: John D'Errico am 2 Jun. 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.

Weitere Antworten (1)

KSSV
KSSV am 2 Jun. 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

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by