Index exceeds the number of array elements. Index must not exceed 1
2 ビュー (過去 30 日間)
古いコメントを表示
x_exact = @(t) 1./3.*2.*exp(-3.*t).*(2.*exp(6.*t) + 1) + 1./3.*2.*exp(-3.*t).*(exp(6.*t) - 1) % x exact solution
y_exact = @(t) 2./3.*2.*exp(-3.*t).*(exp(6.*t) - 1) + 1./3.*2.*exp(-3.*t).*(exp(6.*t) + 2) % y exact solution
h = 0.1 % step
t0 = 0 % first time value
ts = 4 % last time value
t=t0:h:ts
for i = 1:length(t)
if (t(i) == t0)
x_ex(i) = x_exact(t(i))
x_iEm(i) = x_exact(t(i))
else
x_ex(i) = x_exact(t(i))
x_past = x_iEm(i-1)
y = y_exact(t(i))
x_iEm(i) - h*x_iEm(i) == x_past + h*2*y % error occur in this line code
end
end
hi guy , so i'm trying to solve the ODE systems , and calculate x_ex ( exactly solution ) and x_iEm ( implicit Euler method) , and i get the error " Index exceeds the number of array elements. Index must not exceed 1 " , does anyone know the way to fix it ???
it seem like the error occurs when i = 2 -> t(i) = 0.1 , in the line code i comment
0 件のコメント
採用された回答
Alan Stevens
2022 年 11 月 29 日
What are yoy trying to do with
x_iEm(i) - h*x_iEm(i) == x_past + h*2*y;
when i = 2, there is, as yet, no value assigned to x_iEm(i), so you sem to be testing for logical equality with values on the left that don't exist.
2 件のコメント
Alan Stevens
2022 年 11 月 29 日
The == test for equality. To assign something to a variable just use =.
If you intended to obtain x_iEm(i) from x_iEm(i) - h*x_iEm(i) == x_past + h*2*y;then you need
x_iEm(i) = (x_past + h*2*y)/(1-h);
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!