% Author: Thomas Lee
% basin of attraction
% using Newton's method to track initial values to
% convergence of the roots for z^3 -1
% 3 blocks for each root, z1=1,z2,z3 as following
x = -1:0.001:1; y = -1:0.001:1;
[xx, yy] = meshgrid(x,y);
n = 300;
z2 = -1./2. + i*sqrt(3.)/2.;
z3 = -1./2. - i*sqrt(3.)/2.;
for a = 1:length(y),
for b = 1:length(x),
z = xx(a,b) + i*yy(a,b);
for loop = 1:100,
z = z - (z.^3 - 1)./(3*z.^2);
if abs(z - 1) < 1e-4,%just adjust it
n(a,b) = loop; break,
end,
end,
end,
end,
pcolor(xx,yy,n);
shading flat
for a = 1:length(y),
for b = 1:length(x),
z = xx(a,b) + i*yy(a,b);
for loop = 101:200,
z = z - (z.^3 - 1.)./(3*z.^2);
if abs(z - z3) < 1e-4,
n(a,b) = loop; break,
end,
end,
end,
end,
pcolor(xx,yy,n);
shading flat
for a = 1:length(y),
for b = 1:length(x),
z = xx(a,b) + i*yy(a,b);
for loop = 201:300,
z = z - (z.^3 - 1.)./(3*z.^2);
if abs(z - z3) < 1e-4,
n(a,b) = loop; break,
end,
end,
end,
end,
pcolor(xx,yy,n);
shading flat
Newton's Basins