S B
S B

Reputation: 67

Plotting a 3d-function fails

I try to plot the function f(x,y)=(100-(x^2*y^2))^0.5 in Matlab using

x = 0:0.1:10;
y = 0:0.1:5;
[X,Y] = meshgrid(x,y)                            
Z = (100-(X.^2.*Y.^2)).^(0.5)
surf(X,Y,Z) 

I managed to do so with the following online-plotter: https://math24.pro and get this result plot

How can I do that in Matlab? Here I get an error which says that Z is complex instead of real. But I don't get why.

Upvotes: 2

Views: 50

Answers (1)

MichaelTr7
MichaelTr7

Reputation: 4767

Real-Component of a Matrix

Try taking the real() component of z. The imaginary components are a result of 100-(X.^2.*Y.^2) computing to a negative value at various X, Y values. The exponent 0.5 (square root) of a negative number results in imaginary/complex components causing MATLAB to display an error.

3D Plot

x = 0:0.1:10;
y = 0:0.1:5;
[X,Y] = meshgrid(x,y);                            
Z = real((100-(X.^2.*Y.^2)).^(0.5));
surf(X,Y,Z);

Upvotes: 1

Related Questions