Attempted to access y(2); index out of bounds because numel(y)=1
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (2)
Image Analyst
le 27 Mai 2013
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 commentaires
Voir également
Catégories
En savoir plus sur Get Started with Statistics and Machine Learning Toolbox dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!