why am I getting this error ? "Index exceeds array bounds."

h=1; c=0.1; k=0.1; m=1;
y(1)=0; z(1)=0; t(1)=0;
g=@(t,y,z) 1-(c*z)-(k*y);
f=@(t,y,z) (m*z);
for i=1:10
t(i+1)=t(i)+h;
k1=f(t(i),y(i),z(i));
l1=g(t(i),y(i),z(i));
k2=f(t(i)+(h*0.5),y(i)+(k1*0.5*h),z(i)+(l1*0.5*h));
l2=g(t(i)+(h*0.5),y(i)+(k1*0.5*h),z(i)+(l1*0.5*h));
k3=f(t(i)+(h*0.5),y(i)+(k2*0.5*h),z(i)+(l2*0.5*h));
l3=g(t(i)+(h*0.5),y(i)+(k2*0.5*h),z(i)+(l2*0.5*h));
k4=f(t(i)+h,y(i)+(k3*h),z(i)+(l3*h));
y(i+1)=y(i)+(k1+2*k2+2*k3+k4)*h/6;
end
plot(t,y)

 採用された回答

madhan ravi
madhan ravi 2018 年 11 月 6 日

0 投票

Becuase size of t ,y and z is one but you are trying to accessing the elements from 1 to 10 via loop!

3 件のコメント

Walter Roberson
Walter Roberson 2018 年 11 月 6 日
More t and y values are stored as you go along, but you do not store more z values so you run out of those.
madhan ravi
madhan ravi 2018 年 11 月 6 日
Bullseye sir Walter thank you very much?
Ens S
Ens S 2018 年 11 月 6 日
編集済み: Ens S 2018 年 11 月 6 日
Thank you for your explanations. Walter's answer worked.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

2018 年 11 月 6 日

編集済み:

2018 年 11 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by