Max
Max

Reputation: 45

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:

enter image description here

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.

Upvotes: 0

Views: 415

Answers (2)

Siva Srinivas Kolukula
Siva Srinivas Kolukula

Reputation: 1251

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));

Upvotes: 0

TroyHaskin
TroyHaskin

Reputation: 8401

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);

Mesh plot of the surface.

Upvotes: 3

Related Questions