简体   繁体   English

在Matlab中绘制轮廓3D图

[英]Plotting a contour 3D plot in Matlab

I entered this code in Matlab: 我在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? 2D轮廓图解决了。给定代码的问题是什么?

The problem is, that you are introducing complex numbers contour3 cannot handle. 问题是,你正在引入复杂数字contour3无法处理。

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 . 对于2D contour命令,它会自动获取实际部分 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. 粗线表示虚部,细线表示实部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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