I keep on getting 'index exceeds array bounds'

5 ビュー (過去 30 日間)
Cezar John Gorada
Cezar John Gorada 2018 年 9 月 5 日
回答済み: Ji Hoon Jeong 2018 年 9 月 5 日
>> 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.

回答 (1 件)

Ji Hoon Jeong
Ji Hoon Jeong 2018 年 9 月 5 日
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.

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by