%
%  code01a.m -- djm -- 07 sept 2006
%
%  2D complex plotter, real & imag contours
%
%  to get help on any command, type "help" followed by the command name
%  at the matlab prompt

%
%  NOTE:  in the matlab execution window you likely need to set the
%  working directory to the folder where you download the .m files to
%

%  define grid data (trailing semi-colons suppress screen output)
N = 100;  Box = 5;

%  1D data (type "whos x")
x = -Box:2*Box/N:Box;  y = x;

%  2D data (type "whos x2", also help "meshgrid")
[x2,y2] = meshgrid(x,y);

%  2D complex variable (sqrt(-1) is little "i")
z2 = x2 + i*y2;

%  change POLYNOMIAL here
%  complex-valued polynomial (note "." arithmetic)
poly = z2.^3 - 2*z2.^2 - 2*z2.^1 - 3;

%  2D contours of real & imag parts (type "help clf", etc)
figure(1);  clf
hold on

%  negative-valued contours
contour(x,y,real(poly),(-8:-1)*10,'k--')
contour(x,y,imag(poly),(-8:-1)*10,'b--')

%  positive-valued contours
contour(x,y,real(poly),( 1: 8)*10,'k')
contour(x,y,imag(poly),( 1: 8)*10,'b')

%  zero contours
contour(x,y,real(poly),[0 0],'k','linewidth',2)
contour(x,y,imag(poly),[0 0],'b','linewidth',2)

%  axis control & titles
axis([-Box Box -Box Box])
axis square
title('\bf contours of real (black) & imag (blue) parts of P(z)')
xlabel('Real-axis')
ylabel('Imag-axis (positive = solid, zero = thick, negative = dashed)')

%  vector of complex-valued ROOTS
zroots = [(-1+i*sqrt(3))/2, (-1-i*sqrt(3))/2, 3];
plot(real(zroots),imag(zroots),'ro','linewidth',2)
