Reputation: 259
I am working on a project right now where I have modeled the motion of a multibody mechanical system. It is a complex, non-smooth system, and the motion of the system is not always described by the same set of equations, which means if I solve this numerically I have to stop the integration at times (and start it up again with new initial conditions and equations).
Now I would like to implement this in code using Matlab's event functions. The concept is easy, but as always the documentation is a bit nondescriptive when it comes to implementing it for a real problem. (There is a example in the documentation, but I cannot see the source code for some reason). I have nonetheless made some code, now I am just stuck at how to put it all together: I want the ODE-solver (e.g. ode45) to solve my equations, then stop when a event occurs, and begin to integrate again with a new set of ODEs and initial condtions, but I don't know how to pass the ODE-solver what equations to solve next and what the new intital conditions are!
Code:
function dy = firstODE(t,y)
dy = zeros(2,1);
dy(1) = y(2);
dy(2) = 1/(A1)*(B1-c*y(1));
end
function dy = secondODE(t,y)
dy = zeros(4,1);
dy(1) = y(2);
dy(2) = -c/A2*y(1);
dy(3) = y(4);
dy(4) = D2/B2 + c*C2/(A2*B2)*y(1);
end
function [value,isterminal,direction] = myEventsFcn(t,y)
value = [y(1)-K1, y(1)+K1, y(1)+K2, y(1)+K1, y(1)-K1];
isterminal = [1, 1, 1, 1, 1];
direction = [-1, -1, -1, 1, 1];
end
How do I tell the ODE-solver what new initial conditions and equations to use after an event occurs?
Upvotes: 0
Views: 213
Reputation: 108
Call two different solutions. i.e.
sol1 = ode45(@(t,x)firstODE(t,x),tspan1,init1);
% set some conditions
sol2 = ode45(@(t,x)secondODE(t,x),tspan2,init2);
If you want tspan2 to start where the previous one left off and go to the end of your originally defined tspan1, set tspan2 = [sol1.xe,tspan1(end)].
Upvotes: 1