Array indices must be positive integers or logical values.

1 回表示 (過去 30 日間)
Daren Wade
Daren Wade 2019 年 10 月 4 日
コメント済み: Shubham Gupta 2019 年 10 月 4 日
tFinal = 2;
N = 33;
h=tFinal/N;
t=linspace(0,tFinal,N+1);
y=zeros(1,N+1);
yExact=9./(3*t-1+10*exp(t*-3));
y(0) = 1;
for n=1:N
y(n+1) = y(n) + h * y(n)*(3-t(n)*y(n));
end
plot(t,y,'-'); xlabel('t'); ylabel('y'); title('2nd Part') ;
hold all
plot(t,yExact,'bx');
error100= abs(y(N+1)-yExact(N+1));
fprintf('The error of N equaling 33 is %f.\n',error100);
Array indices must be positive integers or logical values.
  1 件のコメント
Walter Roberson
Walter Roberson 2019 年 10 月 4 日
Shubham Gupta is correct: 0 is not a permitted index. You need to add 1 to every index operation of y that you have.
y(0+1) = 1;
y(n+1+1) = y(n+1) + h * y(n+1)*(3-t(n)*y(n+1));
... You will find, by the way, that you fail to assign a value to what was y(1) in your notation.

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

採用された回答

Shubham Gupta
Shubham Gupta 2019 年 10 月 4 日
編集済み: Shubham Gupta 2019 年 10 月 4 日
y(0) = 1; % y(i) = k; i must be a natural number
Not allowed in MATLAB.
Instead write
y(1) = 1;
  2 件のコメント
Walter Roberson
Walter Roberson 2019 年 10 月 4 日
If you start n at 2, then the loop will access y(n) which is y(2) but y(2) has not been assigned.
What the user probably wants is to start n at 1 like it is now.
Shubham Gupta
Shubham Gupta 2019 年 10 月 4 日
Thanks for pointing out my mistake. I have edited the answer accordingly

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by