Speed to graph basin of attraction of Newton's method is very slow

3 次查看(过去 30 天)
I am trying to perform the basin of attraction for Newton's method for nonlinear systems of equations. However the speed is very slow and I am using a grid, even using a grid it takes about 1 minute to make the basin of attraction. I show you the code I used.
close all; clc;
%Systems of equation and Jacobian matrix
F1 = @(x) [x(1)^2-1; x(2)^2-1];
JF1 = @(x)[2*x(1),0 ; 0, 2*x(2)];
%Number of points
nx=400; ny=400;
xmin=-2; xmax=2; ymin=-2; ymax=2;
digits(1000)
x=linspace(xmin,xmax,nx);
y=linspace(ymin,ymax,ny);
[X,Y]=meshgrid(x,y);
%Roots of the systems
r1 = 1+1i;
r2 = -1-1i;
r3 = -1+1i;
r4 = 1-1i;
Z=X+1i*Y;
W = Z; tol = 1e-3; maxiter = 20;
for i=1:nx
for j=1:ny
try
x0 = real(Z(i,j));
y0 = imag(Z(i,j));
w0 = vpa([x0;y0]);
w = Newton(F1,JF1,w0,tol,maxiter);
W(i,j) = w(1)+1i*w(2);
catch
W(i,j) = inf+1i*inf;
end
end
end
Z1 = abs(W-r1) < 1e-3;
Z2 = abs(W-r2) < 1e-3;
Z3 = abs(W-r3) < 1e-3;
Z4 = abs(W-r4) < 1e-3;
Z5 = ~(Z1+Z2+Z3+Z4);
figure;
map = [0.7921 0.1372 0.1725; 0 0.8745 0.2117; 0.03 0.07 0.8667; 0.8549 0.5372 0 ; 0 0 0]; colormap(map);
Z=(Z1+2*Z2+3*Z3+4*Z4+5*Z5);
image([xmin xmax], [ymin ymax], Z);
xlabel("$x_1$","Interpreter","latex"); ylabel("$x_2$","Interpreter","latex");
set(gca, 'YDir','normal')
%Function for Newton's Method
function x = Newton(F,JF,x0,tol,maxiter)
iter = 0;
error = tol+1;
error_f = tol+1;
while error>tol && error_f>tol && iter<maxiter
x = x0-JF(x0)\F(x0);
error = norm(x-x0);
error_f = norm(F(x));
iter = iter+1;
x0 = x;
end
end

采纳的回答

Steven Lord
Steven Lord 2024-5-22
A few comments on sections of your code.
close all; clc;
tic % Adding for purposes of running in MATLAB Answers
%Systems of equation and Jacobian matrix
F1 = @(x) [x(1)^2-1; x(2)^2-1];
JF1 = @(x)[2*x(1),0 ; 0, 2*x(2)];
%Number of points
nx=400; ny=400;
xmin=-2; xmax=2; ymin=-2; ymax=2;
digits(1000)
Why are you setting the number of digits to display symbolic expressions to 1000? If you're not performing symbolic calculations, you can delete this call.
x=linspace(xmin,xmax,nx);
y=linspace(ymin,ymax,ny);
[X,Y]=meshgrid(x,y);
%Roots of the systems
r1 = 1+1i;
r2 = -1-1i;
r3 = -1+1i;
r4 = 1-1i;
Z=X+1i*Y;
W = Z; tol = 1e-3; maxiter = 20;
for i=1:nx
for j=1:ny
try
x0 = real(Z(i,j));
y0 = imag(Z(i,j));
% w0 = vpa([x0;y0]);
Your tolerance is 1e-3; why are you performing the calculations symbolically? Try changing w0 to [x0; y0] (no vpa call) as I did and that should improve the performance significantly.
w0 = [x0; y0];
w = Newton(F1,JF1,w0,tol,maxiter);
W(i,j) = w(1)+1i*w(2);
catch
W(i,j) = inf+1i*inf;
end
end
end
Z1 = abs(W-r1) < 1e-3;
Z2 = abs(W-r2) < 1e-3;
Z3 = abs(W-r3) < 1e-3;
Z4 = abs(W-r4) < 1e-3;
Z5 = ~(Z1+Z2+Z3+Z4);
figure;
map = [0.7921 0.1372 0.1725; 0 0.8745 0.2117; 0.03 0.07 0.8667; 0.8549 0.5372 0 ; 0 0 0]; colormap(map);
Z=(Z1+2*Z2+3*Z3+4*Z4+5*Z5);
image([xmin xmax], [ymin ymax], Z);
xlabel("$x_1$","Interpreter","latex"); ylabel("$x_2$","Interpreter","latex");
set(gca, 'YDir','normal')
toc % Added for purposes of running in MATLAB Answers
Elapsed time is 1.631134 seconds.
That's much quicker, isn't it?
%Function for Newton's Method
function x = Newton(F,JF,x0,tol,maxiter)
iter = 0;
error = tol+1;
error_f = tol+1;
while error>tol && error_f>tol && iter<maxiter
x = x0-JF(x0)\F(x0);
error = norm(x-x0);
error_f = norm(F(x));
iter = iter+1;
x0 = x;
end
end

更多回答(0 个)

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by