Solving system of ODEs with event functions
Ältere Kommentare anzeigen
Hello guys, I'm trying to solve a system of two different equation of motions dv and dw which change depending on the displacements v(1) and w(1). I got the tip to solve it with event functions which I think is the right way to go. I got three different conditions on which my eqm will change, leaving me with 6 different functions which you can see at the end of my code below. The problem is, it doesn't work. I don't get any error messages but the calculation will never get to an end.
I have a few ideas why it won't work but I didn't got a solution yet. So here are my questions, maybe you can help me. I tried to comment everything in my code for you:
1) I need to set a starting equation before the event functions begin to get the first values for v(1) and w(1) on which the events depend. Otherwise I get an error message (not enough input parameters). You can see it in my code just after the beginning of the main loop. I tried to use different stiff and nonstiff ODEs but everytime it just calculates for ages on [t,w] = ode15s(@(t,w) eqm3ai(t,w), tspan, w0);. Where is the problem with that and how can I correct that?
2) I want my event-functions to stop integrating if the conditions (also seen in my code down below) are met. I know that normally it stops integrating if the desired value crosses zero from the desired direction but this is a little problematic in my case because in one case it should include v(1)-w(1)=s and in another case it shouldn't include v(1)-w(1)=s. But maybe I oversee something important. How can I include my conditions seen down below?
3) Which ODE-solver is the best for me? I tried a few (stiff and nonstiff) and none worked.
4) A little redundant but is my event setup more or less correct for my case?
Thank you in advance and sorry for my bad english.
-------------------------code begins here---------------------------------------
function mfc
%%Parameters
m1=4; m2=4; m3=34.6; k3=350; k2=(k3*(m1+m2))/m3; my=m1/(m1+m2); s=0.25 r=0.25; om=5.37; dv0s=5; dv0=dv0s-my*r*om; FW=20;
tspan = [0 10]; % Initial conditions
v0 = [0 dv0];
w0 = [0 0];
tout = tspan(1); % copied from ballode example
vout = v0.';
wout = w0.';
teout = [];
veout = [];
weout = [];
ieout = [];
% main loop
while tout(end) <= tspan(end)
while tout(end) == 0 % starting equations, for first v(1), w(1)
[t,v] = ode15s(@(t,v) eqm2ai(t,v), tspan, v0);
[t,w] = ode15s(@(t,w) eqm3ai(t,w), tspan, w0);
end
[t,v,te,ve,ie] = ode15s(@(t,v) eqm2ai(t,v), tspan, v0, odeset('Events', @ZustandI));
[t,w,te,we,ie] = ode15s(@(t,w) eqm3ai(t,w), tspan, w0, odeset('Events', @ZustandI));
[t,v,te,ve,ie] = ode15s(@(t,v) eqm2bii(t,v), tspan, v0, odeset('Events', @ZustandII));
[t,w,te,we,ie] = ode15s(@(t,w) eqm3bii(t,w), tspan, w0, odeset('Events', @ZustandII));
[t,v,te,ve,ie] = ode15s(@(t,v) eqm2biii(t,v), tspan, v0,odeset('Events', @ZustandIII));
[t,w,te,we,ie] = ode15s(@(t,w) eqm3biii(t,w), tspan, w0, odeset('Events', @ZustandIII));
nt = length(t); % Accumulate output. Copied from ballode example
tout = [tout; t(2:nt)];
vout = [vout; v(2:nt,:)];
wout = [wout; w(2:nt,:)];
teout = [teout; te];
veout = [veout; ve];
weout = [weout; we];
ieout = [ieout; ie];
v0 = [y(nt,1) y(nt,2)]; % new IC and tspan. Copied from ballode
w0 = [y(nt,1) y(nt,2)];
tspan(1) = t(nt);
end
figure;
plot(teout,veout(:,1),'ro')
hold on
plot(teout,weout(:,1),'ro')
xlabel('time');
ylabel('displacement');
title('placeholder');
legend('y_1','y_2');
hold off
box on
% functions------------------------------------------------------------
function dv = eqm2ai(t,v)
dv = zeros(2,1);
dv(1) = v(2);
dv(2) = my*r*om^2*sin(om*t);
end
function dw = eqm3ai(t,w)
dw = zeros(2,1);
dw(1) = w(2);
dw(2) = (FW*sin(om*t)*t-k3*w(1))/m3;
end
function dv = eqm2bii(t,v)
dv = zeros(2,1);
dv(1) = v(2);
dv(2) = my*r*om^2*sin(om*t)-(k2*(v(1)-w(1)-s))/(m1+m2);
end
function dw = eqm3bii(t,v)
dw = zeros(2,1);
dw(1) = w(2);
dw(2) = (FW*sin(om*t)-k3*w(1)+k2*(v(1)-w(1)-s))/m3;
end
function dv = eqm2biii(t,v)
dv = zeros(2,1);
dv(1) = v(2);
dv(2) = my*r*om^2*sin(om*t)-(k2*(v(1)-w(1)+s))/(m1+m2);
end
function dw = eqm3biii(t,v)
dw = zeros(2,1);
dw(1) = w(2);
dw(2) = (FW*sin(om*t)-k3*w(1)+k2*(v(1)-w(1)+s))/m3;
end
% events --------------------------------------------------------------
function [value,isterminal,direction] = ZustandI(t,v,w)
value = all(v(1)-w(1)<=-s && v(1)-w(1)>=s);
isterminal = 1;
direction = 0;
end
function [value,isterminal,direction] = ZustandII(t,v,w)
value = all(v(1)-w(1)>-s);
isterminal = 1;
direction = 0;
end
function [value,isterminal,direction] = ZustandIII(t,v,w)
value = all(v(1)-w(1)<s);
isterminal = 1;
direction = 0;
end
end
13 Kommentare
The first thing you'll have to change is to put the equations for v and w together in one function - thus solving for v(1), v(2) and w(1), w(2) simultaneously, not one after the other.
Come back with the modified code after this change.
Best wishes
Torsten.
Torsten
am 14 Jun. 2018
function dy=eqmai(t,y)
dy=zeros(4,1)
dy(1)=y(2);
dy(2)=my*r*om^2*sin(om*t);
dy(3)=y(4);
dy(4)=(FW*sin(om*t)*t-k3*y(3))/m3;
end
Torsten
am 18 Jun. 2018
You don't reset tout(end) from 0 after solving your starting equation - so it will be solved over and over again.
Further I don't understand why you call eqmai, eqmbii and eqmbiii one after the other - I thought it depends on y(1)-y(3) which one is to be called next.
Best wishes
Torsten.
Lennart
am 18 Jun. 2018
yout = y0;
...
yout = [yout; y(2:nt,1:4)];
And calling "ode45" with "eqmbii" after "eqmai" means that you know that for your initial conditions you have -s<v(1)-w(1)<s and that you will switch to the condition v(1)-w(1)<=-s. Are you sure this will happen ?
Steven Lord
am 19 Jun. 2018
I've skimmed the code you've posted here and in previous questions, and I'm not sure reading through the code will be sufficient to understand what you're trying to do. Can you post the text description and/or equations (no code) of the problem you're trying to solve? Starting from the original problem description we may be able to offer a more efficient way to solve that problem than calling ode45 multiple times.
Torsten
am 19 Jun. 2018
@Steven Lord:
The original code can serve as a problem description:
https://de.mathworks.com/matlabcentral/answers/402546-how-to-solve-differential-equations-of-motion-of-second-order-which-are-changing-depending-on-if-el
@Lennart Hartmann: You set tspan(1)=t(end) which makes the first and last entry of tspan identical.
You should only use one event function for all cases with
value = (y(1)-y(3)-s)*(y(1)-y(3)+s)
This covers zero crossings at s and -s.
Lennart
am 27 Jun. 2018
Lennart
am 27 Jun. 2018
Torsten
am 27 Jun. 2018
The results you collect in your solution vectors tout,yout,... all stem from the set of equations defined in eqmiii(t,y), regardless of the relationship between y1-y3 and s. So your solution can't be correct - sorry.
Best wishes
Torsten.
Antworten (0)
Kategorien
Mehr zu Common Operations finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!