简体   繁体   中英

Quadratic surface plotting in Octave

I've been assigned a project that requires me to plot some quadratic surfaces. I tried to be diligent and download some software so that my graphs look better than those done with other free online resources. I decided to try Octave and see if I can make it work but I've ran into a problem. When trying to plot:

在此处输入图片说明

I've checked some tutorials but so far I haven't been able to pinpoint my error. This is the code I was using:

clear;

x = [-3:1:3];
y = x;

[xx,yy] = meshgrid(x,y);

zz=sqrt(-9*xx.^2+9*yy.^2);

figure

mesh(xx,yy,zz);

Any suggestions are appreciated.

The error thrown to the command window for your script is:

   error: mesh: X, Y, Z, C arguments must be real
   error: called from
       mesh at line 61 column 5
       blah at line 15 column 1

Since you x and y are real, the imaginaries are coming from a square-root of a number less than 0. Looking at your equation, this will happen for any ( x , y ) pair where x is greater than y .

The easiest fix is to set all complex numbers (values of zz with a non-zero imaginary part) to 0 (which will plot the value) or NaN (which will not plot the value. Consider this script (yours plus filtering):

clear;

x = -3:0.1:3;
y = x;

[xx,yy] = meshgrid(x,y);

zz=sqrt(-9*xx.^2+9*yy.^2);

figure

% Set all zz with nonzero imaginary part to NaN
zz(imag(zz)~=0) = NaN;

% % Set all zz with nonzero imaginary part to 0
% zz(imag(zz)~=0) = 0;

mesh(xx,yy,zz);

表面的网格图。

I would prefer this:

x = -3:0.1:3;
y = x;    
[xx,yy] = meshgrid(x,y);  
zz=sqrt(-9*xx.^2+9*yy.^2);   % zz will have both + and - 

figure        
% zz = abs(zz) ; 

mesh(xx,yy,abs(zz));
hold on
mesh(xx,yy,-abs(zz));

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