tricky ode system from m file
Afficher commentaires plus anciens
Starting from t=0 there is one equation system, then after some time, constraint is met and other system is necessary, then this 2nd system meets constrain and changes back to first and so on. I tried some ideas, but they didn't quite work.
Constraints are simple: once x(2) reaches X2 other system should turn in and once x(3) reaches X3 1st system should turn back in.
function xp=uzdevums1(t,x)
%parameters
r1 = 0.1; r2 = 1; r3 = 0.2;K1=100;K2 = 100; K3 = 100;X2=25;X3=10;speedx2 = 0.02; speedx3=0.02;
xp=zeros(3,1);
if (x(2)>X2)&&(xp(3)>=0)
xp(1)=r1*(1-x(1)/(x(2)+x(3)))*x(1);
xp(2)=r2*(1-x(2)/K2)*x(2)-x(2)*x(1)*speedx2;
xp(3)=r3*(1-x(3)/K3)*x(3);
elseif (x(3)>X3)&&(xp(2)>=0)
xp(1)=r1*(1-x(1)/(x(2)+x(3)))*x(1);
xp(2)=r2*(1-x(2)/K2)*x(2);
xp(3)=r3*(1-x(3)/K3)*x(3)-x(3)*x(1)*speedx3;
end
This is one way that doesn't quite work. I hope there is some easier way to do this, because i don't see how to do it in the hard way.
Réponse acceptée
Plus de réponses (1)
Ryan G
le 7 Jan 2013
What do you mean doesn't work? It sounds like you need a new state to define what condition you are currently in. This way you will have a default operating point instead of using the if-else condition every time.
Technically I would use Simulink/Stateflow to accomplish this and it would be ideal. However, within the bounds of the ode solver you could do something like this:
if(myState == true)
%ode equations
myState = x(3)>X3)&&(xp(2)>=0;
else
%other ode equations
myState = (x(2)>X2)&&(xp(3)>=0);
end
This will keep you in the current state until the condition is met to go back to the previous (or next) state.
2 commentaires
Jan
le 7 Jan 2013
You could store the state persistenly, see "help persistent". But as explained in my answer, I recommend to avoid this.
Catégories
En savoir plus sur Ordinary Differential Equations dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!