tricky ode system from m file
3 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Jan
2013-1-7
编辑:Jan
2013-1-7
And you want to integrate uzdevums1 by ODE45 or a similar function?
Do not do this. The ODE-integrators have a stepsize control, which is substantially confused by discontinuities. In consequence the number of steps and function evaluations can be increased very much, such that the accumulated rounding errors will influence the results.
Use event functions to get the switching points, stop the integrator and restart it with the former final values. See doc odeset.
更多回答(1 个)
Ryan G
2013-1-7
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 个评论
Jan
2013-1-7
You could store the state persistenly, see "help persistent". But as explained in my answer, I recommend to avoid this.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!