I keep on getting 'index exceeds array bounds'

1 view (last 30 days)
Cezar John Gorada
Cezar John Gorada on 5 Sep 2018
Answered: Ji Hoon Jeong on 5 Sep 2018
>> i=1; >> x1(i)=1;x2(i)=1;x3(i)=1; >> error_x1(i)=999999999999999999999999; >> while error_x1(i)>=0.01 x1(i+1) = (7-x2(i)-3*x2(i))/2 x2(i+1) = (9-3*x1(i+1)+2*x3(i))/4 x3(i+1) = (4-x2(i+1)-x3(i+1))/1
error_x1(i+1) = abs((x1(i+1)-x1(i))/x1(i+1))*100;
error_x1(i+1) = abs((x2(i+1)-x2(i))/x2(i+1))*100;
error_x1(i+1) = abs((x3(i+1)-x3(i))/x3(i+1))*100;
i=i+1;
end
x1 =
1.0000 1.5000
x2 =
1.0000 1.6250
Index exceeds array bounds.

Answers (1)

Ji Hoon Jeong
Ji Hoon Jeong on 5 Sep 2018
I presume this is your code.
i=1;
x1(i)=1;
x2(i)=1;
x3(i)=1;
error_x1(i)=999999999999999999999999;
while error_x1(i)>=0.01
x1(i+1) = (7-x2(i)-3*x2(i))/2
x2(i+1) = (9-3*x1(i+1)+2*x3(i))/4
x3(i+1) = (4-x2(i+1)-x3(i+1))/1
error_x1(i+1) = abs((x1(i+1)-x1(i))/x1(i+1))*100;
error_x1(i+1) = abs((x2(i+1)-x2(i))/x2(i+1))*100;
error_x1(i+1) = abs((x3(i+1)-x3(i))/x3(i+1))*100;
i=i+1;
end
The third line in your while statement is causing the problem.
x3(i+1) term in the right side of the equal sign.
Since x3(1+1) does not exist, you should change your algorithm.

Community Treasure Hunt

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

Start Hunting!