I keep on getting 'index exceeds array bounds'

1 次查看(过去 30 天)
>> 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 CenterFile Exchange 中查找有关 Write C Functions Callable from MATLAB (MEX Files) 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by