简体   繁体   中英

Plotting a contour 3D plot in Matlab

I entered this code in Matlab:

[x,y]=meshgrid(-3:1:3,-3:1:3);
z=sqrt((y.*y)-(x.*x))
contour3(x,y,z)

But am getting error for the same. 2D contour plot works out.What's the problem with the given code?

The problem is, that you are introducing complex numbers contour3 cannot handle.

use either

z = abs( sqrt((y.*y)-(x.*x)) )

or

z = real( sqrt((y.*y)-(x.*x)) )

or rethink whether you really want what you are doing.

For the 2D contour command it automatically takes the real part . You could also do something like this to get both plotted.

[x,y] = meshgrid(-3:1:3,-3:1:3);

zr = real( sqrt((y.*y)-(x.*x)) )
contour(x,y,zr,'linewidth',1); hold on

zi = imag( sqrt((y.*y)-(x.*x)) )
contour(x,y,zi,'linewidth',3); hold off

gives:

在此输入图像描述

where the bold lines illustrate the imaginary part and the slim ones the real part.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM