How to find Basins of attraction for the Halley method?

1 次查看(过去 30 天)
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
Alan Stevens 2023-11-4
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)

更多回答(0 个)

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by