Solving system of ODEs with event functions
2 Ansichten (letzte 30 Tage)
Ä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
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)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!