Basins of attraction and Newtons Method.

8 views (last 30 days)
Atique Khan
Atique Khan on 18 May 2019
I am working on basins of attraction and use the code with newton method but it gives error saying' index increases the dimension of matrix' I am new in matlab and just editing a code made already and cant understand the error please.
clc ; clear all
warning('off') % To off the warning which shows "Matrix is close to singular
% badly scaled" when algorithm passes through a point where the Jacobian
% matrix is singular
% The roots of the given governing equations
r1 = [1 ;2.8284] ;
r2 = [1 ;-2.8284] ;
%r3 = [1 ;1] ;
% Initial conditions
x = linspace(-3,3,200) ;
y = linspace(-3,3,200) ;
% Initialize the required matrices
Xr1 = [] ; Xr2 = [] ; Xr3 = [] ; %Xr4 = [] ;
tic
for i = 1:length(x)
for j = 1:length(y)
X0 = [x(i);y(j)] ;
% Solve the system of Equations using Newton's Method
X = NewtonRaphson1(X0) ;
% Locating the initial conditions according to error
if norm(X-r1)<1e-8
Xr1 = [X0 Xr1] ;
elseif norm(X-r2)<1e-8
Xr2 = [X0 Xr2] ;
else
Xr3 = [X0 Xr3];
end
end
end
toc
warning('on') % Remove the warning off constraint
% Initialize figure
figure
set(gcf,'color','w')
hold on
plot(Xr1(1,:),Xr1(2,:),'.','color','r') ;
plot(Xr2(1,:),Xr2(2,:),'.','color','b') ;
plot(Xr3(1,:),Xr3(2,:),'.','color','k') ;
%plot(Xr4(1,:),Xr4(2,:),'.','color','k') ;
title('Basin of attraction for f(x,y) = x^2+y^2=9 and (x-2)^2+y^2 = 9 ')
with the newtons method file
function X = NewtonRaphson(X)
NoIter = 10 ;
% Run a loop for given number of iterations
for j=1:NoIter
% Governing equations
f=[X(1)^2 - X(2)^2-9; (X(1)-2)^2 - X(2)^2-9] ;
% Jacobian Matrix
Jf=[2*X(1) 2*X(2); 2*(X(1)-2) 2*X(2)];
% Updating the root
X=X-Jf\f;
end

Answers (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 18 May 2019
Hi,
You are trying to plot the series of Xr1, Xr2, Xr3 without really computing to their values. Because your error tolerances are very tight. In your code, your have one small typo: X = NewtonRaphson1(X0) ; has to be: X = NewtonRaphson(X0)
If you wish to plot the evolutions of your computed roots, then here is a small edited part of your code:
for i = 1:length(x)
for j = 1:length(y)
X0 = [x(i);y(j)] ;
% Solve the system of Equations using Newton's Method
X(:,i) = NewtonRaphson(X0) ; % To collect all evolved/computed roots
% Locating the initial conditions according to error
if norm(X-r1)<1e-8
Xr1 = [X0 Xr1] ;
elseif norm(X-r2)<1e-8
Xr2 = [X0 Xr2] ;
else
Xr3 = [X0 Xr3];
end
end
end
toc
warning('on') % Remove the warning off constraint
% Initialize figure
figure
set(gcf,'color','w')
plot(X(1, :), X(2,:), 'b-'), grid on; shg
title('Basin of attraction for f(x,y) = x^2+y^2=9 and (x-2)^2+y^2 = 9 ')
Good luck.

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2013b

Community Treasure Hunt

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

Start Hunting!