user3410944
user3410944

Reputation: 17

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?

Upvotes: 0

Views: 936

Answers (1)

Robert Seifert
Robert Seifert

Reputation: 25232

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:

enter image description here

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

Upvotes: 2

Related Questions