Attempted to access y(2); index out of bounds because numel(y)=1

14 ビュー (過去 30 日間)
ANCHIT
ANCHIT 2013 年 5 月 27 日
I am trying to make sir epidemic model uding ode45 but constantly I am getting this error "Attempted to access y(2); index out of bounds because numel(y)=1".please suggest some method to remove this.
thank you
if true
% code
function sir_model = sir_model(t,y)
a=1;
b=1;
sir_model(1) = -a*y(1)*y(2);
sir_model(2)= a*y(1)*y(2) -b*y(2);
sir_model(3)= b*y(2);
sir_model=[sir_model(1) sir_model(2) sir_model(3)]';
end

回答 (2 件)

Image Analyst
Image Analyst 2013 年 5 月 27 日
Are you trying to do a recursive function? If so, I see no exit condition. Or are you just use to Visual Basic where you set the return argument of a function by setting the function name equal to something?
Since sir_model is a function, I don't think this will work:
sir_model(1) = -a*y(1)*y(2);
Then to make it worse, you're recursing back into sir_model three separate times with this statement:
sir_model=[sir_model(1) sir_model(2) sir_model(3)]';
As if that weren't bad enough, you're passing in a y that's just a single value (1, 2, or 3), not an array of two numbers.
By chance do you mean this:
function sir_model_output = sir_model(t,y)
a = 1;
b = 1;
term1 = -a * y(1) * y(2);
term2 = a * y(1) * y(2) - b * y(2);
term3 = b * y(2);
sir_model_output = [term1, term2, term3]';

Youssef  Khmou
Youssef Khmou 2013 年 5 月 27 日
hi y's length must >2 : try :
H=sir_model(t,rand(2,1));

カテゴリ

Help Center および File ExchangeGet Started with Statistics and Machine Learning Toolbox についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by