Message Boards Message Boards

How to solve a constraint deferential equations with If function

Posted 10 years ago

I have to solve a series of differential equations along with a constraint that has to be satisfied at each point of time. To solve the differential equations, I first solves the following in order to obtain ?(t) and x(t)

sol1 = NDSolve[{?'[t] == -((c1^2 - 4*k1*?*?[t] - 
   2*c1*(?[t] + ?) + (?[t] + ?)^2)/(
  4 k1)), x'[t] == -((c2^2 - 4*k2*(?*x[t] + ?1 (x[t] - [Lambda][t])) - 2*c2*(x[t] + ?) + (x[t] + ?)^2)/(4 k2)), ?[T] == 0, x[T] == 0}, {?, x}, {t, 0, 100}];

Then using those values I calculate ?1(t) and ?2(t)

?1[t_] := (c1 - {?[t] /. sol1} - ?)/(2*k1);
?1[t][[1, 1]];

?2[t_] := (c2 - {x[t] /. sol1} - ?)/(2*k2);
?2[t][[1, 1]];

Having ?1(t) and ?(t), ?2(t) and x(t) I 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}];

Now I have to check if the constraint is the satisfied, the constraint is the following: if

d[t] - (?1[t]*{y1[t] /. s} + ?2[t]*{y2[t] /. s}) >= 0

then ? == 0

Otherwise calculate the ?(t) from below

? == (-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 the new value of ?.

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;

Is there anyways to code this model to check for ?(t) and assign the value for it based on the defined condition? I also need to plot d[t] - (?1[t]*{y1[t] /. s} + ?2[t]*{y2[t] /. s})to see for what intervals my constraint is violated.

p.s. I'm using mathematica v.9.0. Thanks in advanced for any help

POSTED BY: afshar_saman
Posted 10 years ago

You do not give the initial value used for ? in your first NDSolve. I assume there was some trial value and I picked zero.

You seem to be using { and } in an unconventional way to try to limit the scope of variable substitution. { and } are almost exclusively used for list construction. You might try using ( and ) instead.

For what seems to be the essential bit of your question, you might look at using

FindMinimum[d[t] - (?1[t]*(y1[t] /. s) + ?2[t]*(y2[t] /. s)),{t, 50, 0, 100}]

which should give you a non-negative value if your constraint is satisfied and a negative value otherwise. Look at the information hidden behind Details and Options on the documentation page for FindMinimum to see how this should restrict the search for the minimum to lie between 0 and 100. That is only guaranteed to find local minima. There are a few other functions in Mathmatica, but I don't think they may guarantee a global minima with several interpolating functions. A function that has an option of using random search might stumble onto a minima less than zero when others might be trapped in local minima greater than zero.

I was able to plot the function you desired by using

Plot[First[d[t] - (\[Rho]1[t]*(y1[t] /. s) + \[Rho]2[t]*(y2[t] /. s))], {t, 0, 100}]

but I can't be certain that the use of First has not missed essential behavior perhaps hiding behind other solutions.

Hopefully you will see how you might adapt this to serve your purposes. All this should work with version 9.0.

POSTED BY: Bill Simpson
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract