How to fix 'Index exceeds the number of array elements. Index must not exceed 1.'?

64 次查看(过去 30 天)
The error is 'Index exceeds the number of array elements. Index must not exceed 1.'
in Line 11, T1_old=T1(i); T2_old=T2(i); T3_old=T3(i);
I don't know how should I fix it... Does anyone could please help me?
T1(1)=0; T2(1)=0; T3(1)=0;
T4(1)=0; T5(1)=0; T6(1)=0;
T7(1)=0; T8(1)=0; T9(1)=0;
error(1)=100; error(2)=100; error(3)=100;
error(4)=100; error(5)=100; error(6)=100;
error(7)=100; error(8)=100; error(9)=100;
i=1;
while 1
T1_old=T1(i); T2_old=T2(i); T3_old=T3(i);
T4_old=T4(i); T5_old=T5(i); T6_old=T6(i);
T7_old=T7(i); T8_old=T8(i); T9_old=T9(i);
T1_new = (-600-T2(i)-T4(i))/(-4);
T2_new = (-500-T1_new-T3(i)-T5(i))/(-4);
T3_new = (-567-2*T2_new-T6(i))/(-4.67);
T4_new = (-100-T1_new-T5(i)-T7(i))/(-4);
T5_new = (-T2_new-T4_new-T6-T8(i))/(-4);
T6_new = (-67-T3_new-2*T5_new-T9(i))/(-4.67);
T7_new = (-167-2*T4_new-T8(i))/(-4.67);
T8_new = (-67-2*T5_new-T7_new-T9(i))/(-4.67);
T9_new = (-67-T6_new-T8_new)/(-2.67);
error1(i+1)=abs(T1_new-T1_old)/T1_new;
error2(i+1)=abs(T2_new-T2_old)/T2_new;
error3(i+1)=abs(T3_new-T3_old)/T3_new;
error4(i+1)=abs(T4_new-T4_old)/T4_new;
error5(i+1)=abs(T5_new-T5_old)/T5_new;
error6(i+1)=abs(T6_new-T6_old)/T6_new;
error7(i+1)=abs(T7_new-T7_old)/T7_new;
error8(i+1)=abs(T8_new-T8_old)/T8_new;
error9(i+1)=abs(T9_new-T9_old)/T9_new;
if(error1(i+1)<0.01 && error2(i+1)<0.01 && error3(i+1)<0.01 && ...
error4(i+1)<0.01 && error5(i+1)<0.01 && error6(i+1)<0.01 && ...
error7(i+1)<0.01 && error8(i+1)<0.01 && error9(i+1)<0.01)
break
end
i=i+1;
end
  2 个评论
Riccardo Scorretti
Riccardo Scorretti 2022-4-10
Hi. You published a screenshot (= an image) so it is impossible to run the code without copying it. Pleaase, publish your code as text (and properly formatted, as a code) so we can help without having to retype your code.

请先登录,再进行评论。

回答(2 个)

Walter Roberson
Walter Roberson 2022-4-10
You never assign to T1(2) so when i becomes 2, T1(i) becomes out of range.
You do create T1_new but you do not use it to update T1

Riccardo Scorretti
Riccardo Scorretti 2022-4-10
By a visual inspection of your code, one sees that at line 11 you assign to variables T1_old .. T3_old the value of T1(i) .. T3(i) respectively. The problem is that in the loop variables T1 ... T3 are never updated, and their size if always 1. In fact, these variable are defined as scalar at line 1, and they are never modified, so their size is fixed to 1. Your code will logicaly fail at the second iteration because you try to access to T1(2). The same holds for variables T4 ... T9.
I presume that the solution is to modify lines 14 - 22: there you must replace T1_new by T1(i+1), etc. and T1(i) by T1_old(i). For instance, for line 14:
T1(i+1) = (-600-T2_old(i)-T4_old(i))/(-4);
and so on. The same consideration applies to lines 24-32.

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by