How to plot a 3D graph?
Show older comments
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.
Accepted Answer
More Answers (1)
KSSV
on 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
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!