# How to find Basins of attraction for the Halley method?

3 views (last 30 days)
Omar B. on 4 Nov 2023
Edited: Omar B. on 6 Nov 2023
I am trying to implwment Halley's Mathod to find basin of attraction of the roots, but when I run the code I got a black image. Could you please check the code and help me with this problem
f = @(z) z.^3 +1;
df = @(z) 3*z.^2;
ddf=@(z) 6*z;
% its roots
r1 = -1;
r2 = 1/2 + 1i*sqrt(3)/2;
r3 =1/2 - 1i*sqrt(3)/2;
%% range of basin
nx = 1000;
ny = 1000;
xmin = -4; xmax = 4;
ymin = -2; ymax = 2;
x = linspace(xmin,xmax,nx); % Real space
y = linspace(ymin,ymax,ny); % Imaginary space
[X, Y] = meshgrid(x,y);
Z = X + 1i*Y;
%% Perform halley's iterations
nit = 100; % Maximum number of Newton iterations to be done
for i = 1:nit
Z = Z - (2*f(Z)*df(Z)) ./ (2*(df(Z)).^2-ddf(Z)*f(Z)); %%%% %%% dot multiply
end
%% plotting
eps = 1e-6; % Tolerance to determine closeness to 0.0
Z1 = abs(Z - r1) < eps; Z2 = abs(Z - r2) < eps;
Z3 = abs(Z - r3) < eps; Z4 = ~(Z1 + Z2 + Z3);
figure;
map = [0 1 0; 0 0 1;1 0 0 ; 0 0 0];
colormap(map);
Z = Z1 + 2*Z2 + 3*Z3 + 4*Z4;
image([xmin xmax],[ymin ymax], Z);
set(gca,'YDir','normal');
axis equal; axis tight;
xlabel('\$x\$','Interpreter','latex','FontSize',14)
ylabel('\$y\$','Interpreter','latex','FontSize',14)
title('Basins of attraction for the root of \$f(x)=z^3+1\$.','Interpreter','latex','FontSize',14)

Alan Stevens on 4 Nov 2023
Don'tforget the dot multiply:
f = @(z) z.^3 +1;
df = @(z) 3*z.^2;
ddf=@(z) 6*z;
% its roots
r1 = -1;
r2 = 1/2 + 1i*sqrt(3)/2;
r3 =1/2 - 1i*sqrt(3)/2;
%% range of basin
nx = 1000;
ny = 1000;
xmin = -4; xmax = 4;
ymin = -2; ymax = 2;
x = linspace(xmin,xmax,nx); % Real space
y = linspace(ymin,ymax,ny); % Imaginary space
[X, Y] = meshgrid(x,y);
Z = X + 1i*Y;
%% Perform halley's iterations
nit = 100; % Maximum number of Newton iterations to be done
for i = 1:nit
Z = Z - (2*f(Z).*df(Z)) ./ (2*(df(Z)).^2-ddf(Z).*f(Z)); %%%% %%% dot multiply
end
%% plotting
eps = 1e-6; % Tolerance to determine closeness to 0.0
Z1 = abs(Z - r1) < eps; Z2 = abs(Z - r2) < eps;
Z3 = abs(Z - r3) < eps; Z4 = ~(Z1 + Z2 + Z3);
figure;
map = [0 1 0; 0 0 1;1 0 0 ; 0 0 0];
colormap(map);
Z = Z1 + 2*Z2 + 3*Z3 + 4*Z4;
image([xmin xmax],[ymin ymax], Z);
set(gca,'YDir','normal');
axis equal; axis tight;
xlabel('\$x\$','Interpreter','latex','FontSize',14)
ylabel('\$y\$','Interpreter','latex','FontSize',14)
title('Basins of attraction for the root of \$f(x)=z^3+1\$.','Interpreter','latex','FontSize',14)
Omar B. on 4 Nov 2023
Thank you so much