I have to solve a series of differential equations along with a constraint that has to be satisfied at each point of time where $t\in [0, 100]$ The model parameters are
β = 0.1; c1 = 10; c2 = 30; k1 = 100; k2 = 300; T = 100;
δ1 = 0.1; δ2 = 0.3; Subscript[y, 0] = 1000; a = 4000; b = 200;
To solve the differential equations, I first assume that μ[t]=0solves the following in order to obtain $\lambda (t)$ and $x(t)$
sol1 = NDSolve[{λ'[t] == -((c1^2 - 4*k1*β*λ[t] -
2*c1*(λ[t] + μ[t]) + (λ[t] + μ[t])^2)/(
4 k1)), x'[t] == -((c2^2 - 4*k2*(β*x[t] + δ1 (x[t] - λ[t])) - 2*c2*(x[t] + μ[t]) +
(x[t] + μ[t])^2)/(4 k2)), λ[T] == 0, x[T] == 0}, {λ, x}, {t, 0, 100}];
having λ[t] and x[t], I calculate ρ1[t] and ρ2[t]
ρ1[t_] := (c1 - {λ[t] /. sol1} - μ[t])/(2*k1);
ρ1[t][[1, 1]];
ρ2[t_] := (c2 - {x[t] /. sol1} - μ[t])/(2*k2);
ρ2[t][[1, 1]];
Having ρ1[t] and λ[t], ρ2[t] and x[t], I have to solve the following differential equations where `
d[t_] := a + b*Sin[(2*π*t/25)];
s = NDSolve[{y3'[t] == d[t] - δ2*y3[t],
y2'[t] == δ2*y3[t] - (δ1 - ρ2[t][[1, 1]]) y2[t],
y1'[t] == δ1*y2[t] - ρ1[t][[1, 1]]*y1[t],
y3[0] == Subscript[y, 0], y2[0] == Subscript[y, 0],
y1[0] == Subscript[y, 0]}, {y3, y2, y1}, {t, 0, 100}];
The problem is my constraint is based on ρ1[t], y1[t], ρ2[t] and y2[t]. So only after calculating everything with the assumption of μ[t]=0, I can actually check my constraint to see if it is satisfied, the constraint is the following: if
d[t] - (ρ1[t]*{y1[t] /. s} + ρ2[t]*{y2[t] /. s}) >= 0
then μ[t] = 0. Otherwise calculate the $μ[t]$ from below
μ[t] := (-2*d[t]*k1*k2 + c1*k2*{y1[t] /. s} + c2*k1*{y2[t] /. s} -
k2*{y1[t] /. s}*{λ[t] /. sol1} -
k1*{y2[t] /. s}*{x[t] /. sol1})/(k2*{y1[t] /. s} +
k1*{y2[t] /. s})]
and then I have to solve everything with from the beginning with new value of μ[t]. With μ=0 I get something like this
Plot[{d[t], ρ1[t]*{y1[t] /. s} + ρ2[t]*{y2[t] /. s}}, {t, 0,
100}, PlotRange -> {0, 6000}, Frame -> True,
FrameLabel -> {"time", "d(t)-(ρ1(t)y1(t)+ρ2(t)y2(t))"}]

basically after the intersection point both lines should be adjusted (this should be taken care of by the new value of μ[t]) Is there anyways to code this model to check μ[t] at each point of time and see if the constraint is satisfied or not? do I need to create a loop to do this?


WhenEvent. – Michael Seifert Jul 22 '15 at 19:39WhenEventis in 9. I think you will need to explicitly show theudependance onrho, egrho1[t_,u_]:=..– george2079 Jul 22 '15 at 20:17d[t] - (ρ1[t]*{y1[t] /. s} + ρ2[t]*{y2[t] /. s}) < 0. what i have to do is at that point of time i should calculateμ[t]and then go back to the first step and recalcuate everyhing based on the value ofμ[t]. – Baboda Jul 24 '15 at 09:41μ[t]seems to depend onρ1[t]andρ1[t]depends onμ[t], which would lead to infinite recursion. Please clarify howμ[t]is to be defined. At first, it is just0, but why not omit it then? Or isμ[t]to be replaced at some point by another definition, and issol1to be recomputed at that point? – Michael E2 Jul 24 '15 at 12:12μ[t] := (-2*d[t]*k1*k2 + c1*k2*{y1[t] /. s} + c2*k1*{y2[t] /. s} - k2*{y1[t] /. s}*{λ[t] /. sol1} - k1*{y2[t] /. s}*{x[t] /. sol1})/(k2*{y1[t] /. s} + k1*{y2[t] /. s})]and then go back and compute sol1 based on the new values of μ[t]. so for example in the graph up to t=20μ[t]=0is fine but after that I have to recalculateμ[t]andsol1. – Baboda Jul 24 '15 at 12:42d[t] - (ρ1[t]*{y1[t] /. s} + ρ2[t]*{y2[t] /. s}) >= 0is satisfied. Do you mean to iterate the process and stop when it converges to a stable definition of μ[t]? Perhaps it just happens to work with a single iteration? – Michael E2 Jul 24 '15 at 12:54μ[t]everywhere, if the constraint is not satisfied anywhere, or that you need to changeμ[t]only fortat which the constraint is not satisfied? – bbgodfrey Jul 24 '15 at 13:29μ[t]only fortat which the constraints is not satisfied. so we have to switch fromμ[t]=0to newμ[t]and continue with newμ[t]towards the end of the planning horizon. – Baboda Jul 24 '15 at 13:340<t<20constraint is satisfied so we keepμ[t]=0and then att=20we should switch to newμ[t]and continue with that. I think depending ond[t]we might switch more than once. – Baboda Jul 24 '15 at 13:41