Attempted to access y(2); index out of bounds because numel(y)=1
조회 수: 1 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (2개)
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]';
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Problem-Based Optimization Setup에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!