We now show that Newton's method extends easily to one complex equation in one complex unknown. For example, let us see whether Newton can confirm that the roots of
z2-2z+2
1+i and 1-i.
z=z-f(z)/f'(z)
roots([1 -2 2])
In particular, we will dissect Isaac's attack on a polynomial by first assigning each root a color. Then, depending on where Isaac takes us we paint accordingly. For example, if Isaac takes us to the first root we paint it red, if he takes us to the other root we paint it blue, if Isaac loses his way we paint the point black. As he has a much better chance of losing his way in the complex plane we will produce a number of gorgeous Newton wastelands.
You will write code that dissects a user specfied quartic. To get started we dissect the particular
(z2-1)(z2+0.16)
z = z - (z^2-1)*(z^2+0.16)/(4*z^3 - 1.68*z) (NS)
Basin of 1 is yellow Basin of -1 is red Basin of 0.4i is green Basin of -0.4i is blue Wasteland is black
With this scheme this pointilist driver produces the picture at right
for x = .15:.0025:.55,
for y = -.15:.0025:.15,
color = newt(x+i*y,1e-3,20);
plot(x,y,color)
hold on
end
end
Well, it takes a seed (x+i*y) and a tolerance (1e-3) and a maxiter (20) and returns a color ('y', 'r', 'b', 'g', or 'k') depending on where (NS) took the seed. The driver then paints that seed accordingly. The range of x and y specify the window of the complex plane to be painted, while the increment (0.0025) specifies the resolution. Here is the code that does the job.
Though this is indeed straightforward to understand, it does not exploit Matlab's ability to work directly on vectors and matrices. We shall see that the nested for loops can be contracted to a single (very fast) line.
x = .15:.0025:.55;
y = -.15:.0025:.15;
[X,Y] = meshgrid(x,y);
Z = X+i*Y;
for k=1:maxiter,
Z = Z - (Z.^2-1).*(Z.^2+0.16)./(4*Z.^3 - 1.68*Z);
end
Now, upon completing this for loop, Z will be a matrix whose elements are either close to one of the four roots, or not. To find out which ones are close to 1 I recommend the find command
[i1,j1] = find(abs(Z-1)<0.1);
If we paint the other
roots accordingly, and leave the wasteland blank, we arrive at the finer
(a markersize of 1) portrait below.
Our assignment this week is to produce such portraits for arbitrary quartics. Quartics are encoded by 5 complex scalars, for example, the quartic
2z4-4z3+(2-i)z2-iz+10
q = [2 -4 2-i -i 10]
f = polyval(q,z)
dq = [8 -12 4-2i -i]
df = polyval(dq,z)
The last innovation for this assignment is the translation of the q into a string that will be used to title your portrait. Typing help strfun will lead to hours of string fun - after which you'll know all you need for this week. But just to be sure, the line
qlab = strcat(qlab,'+',num2str(q(k)),'z^',num2str(5-k))
1z4+0z3+-0.84z2+0z+-0.16z0