Tuesday, July 27, 2010

Plotting complex polynomials with octave and octaviz (vtk)

I got to wondering what happens when you feed complex numbers into polynomials, so I poked around and found the awesome vtk library binding for the octave language, called octaviz.  (octave is a free implementation of the MATLAB language).


When we plot z = f(x), where x and z are complex numbers, we get 4 dimensions to plot, so I shifted each point in the surface plot N units + or - in the Y direction, where N is the imaginary part of the output.  The color also indicates how far each point was stretched in the + or - Y direction. 

#!/usr/bin/octave

global a = 1
global b = 0
global c = 0

function z = polynomial (i,j)
  global a
  global b
  global c
  x = i + (j * 1j);
  z = a*x*x + b*x + c;
end

[I,J] = meshgrid(-1:0.2:1);
REAL = arrayfun(@real, arrayfun(@polynomial, I, J));
IMAG = arrayfun(@imag, arrayfun(@polynomial, I, J));
vtk_surf(I,J+IMAG,REAL, IMAG);

input ("Orient it the way you want it before I save.");
vtk_print('polynomial.jpg', '-djpeg');
input ("Done!");

2 comments:

Coila said...

:o

.
.
That is *so* cool!

King Yin Yan said...

That looks nice, but Octaviz seems to have gone obsolete. I'm trying to reproduce this in Mathematica or Sage or similar software.

Also a question: that plot does not look like a simple (real) quadratic function. Can you explain what are the coordinates and colors?