Basins Of Attraction - Newtons Method
4 views (last 30 days)
Show older comments
When I run my code it gives me an error that I do not know how to fix. The error says "Index in position 1 exceeds array bounds." How can I fix this?
This is the code I used, I also have a separate code for the function NewtonRaphson in case you were wondering:
clc ; clear all
warning('off')
% The roots
r1 = [-0.672 ;-0.6074] ;
r2 = [0 ;0] ;
r3 = [0.672 ;0.6074] ;
x = linspace(-1,1,200) ;
y = linspace(-1,1,200) ;
Xr1 = [] ; Xr2 = [] ; Xr3 = [] ; Xr4 = [] ;
Tolerance = 0.1;
for i = 1:length(x)
for j = 1:length(y)
X0 = [x(i);y(j)] ;
X = NewtonRaphson(X0) ;
if norm(X-r1)<Tolerance %measuring L2 Norm
Xr1 = [X0 Xr1] ;
elseif norm(X-r2)<Tolerance
Xr2 = [X0 Xr2] ;
elseif norm(X-r3)<Tolerance
Xr3 = [X0 Xr3] ;
else
Xr4 = [X0 Xr4] ;
end
end
end
display(Xr1)
figure
set(gcf,'color','w')
hold on
plot(Xr1(1,:),Xr1(2,:),'.','color','m') ;
plot(Xr2(1,:),Xr2(2,:),'.','color','b') ;
plot(Xr3(1,:),Xr3(2,:),'.','color','g') ;
plot(Xr4(1,:),Xr4(2,:),'.','color','k') ;
title('Basin of attraction for f(x,y) = 2x^3-y = 0 and 3y^3-x=0')
warning('on')
0 Comments
Answers (1)
Shraddha Jain
on 3 Mar 2021
Hi Layan,
The error,
Index in position 1 exceeds array bounds
occurs when the index of the array accessed does not agree with the length of the array. Kindly check that the index at first position is neither less than 1 nor greater than the length of the array.
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!