Index exceeds the number of array elements. Index must not exceed 2.

24 ビュー (過去 30 日間)
Hazel Can
Hazel Can 2022 年 5 月 23 日
コメント済み: Jan 2022 年 5 月 23 日
Hi. I have an error : Index exceeds the number of array elements. Index must not exceed 2. for line 16. Why ? and How can i fix this problem ?
clc; clear all;
t=[0 1];
h=0.01;
n=(t(2)-t(1))/h;
alpha=0.5;
%initials%
y_m(1)=2;
y_m(2)=exp(20.*h)+cos(h);
f_m(1)=20.*(y_m(1)-cos(t(1)))-sin(t(1));
f_m(2)=20.*(y_m(2)-cos(t(2)))-sin(t(2));
%Midpoint Two step method%
for i=3:n
y_m(i)=y_m(i-2)+2.*h.*f_m(i-1);
f_m(i)=20.*(y_m(i)-cos(t(i)))-sin(t(i));
end
  1 件のコメント
Jan
Jan 2022 年 5 月 23 日
Instead of writing "line 16" post a copy of the complete error message. Then the readers do not have to count and most of all: the 16th line is running without a problem, but the 17th fails...

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

採用された回答

Jan
Jan 2022 年 5 月 23 日
for i=3:n
y_m(i)=y_m(i-2)+2.*h.*f_m(i-1); % <-- This is line 16
f_m(i)=20.*(y_m(i)-cos(t(i)))-sin(t(i)); % <-- This line is failing
end
t(3) is not defined.

その他の回答 (1 件)

Voss
Voss 2022 年 5 月 23 日
This error happens because t has only two elements, so there is no element t(3).
How to fix it depends on what the code should be doing.
  1 件のコメント
Hazel Can
Hazel Can 2022 年 5 月 23 日
Thank you for your answer. I fixed this error.

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

カテゴリ

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