Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have no idea how to dynamically update a parameter inside NDSolve. For example consider following first-order DE:

Eq = x'[t] + (x[t] - λ) == 0;

where λ is a parameter. I want to solve this ODE but this parameter might change when x'[t] turns to zero:

WhenEvent[x'[t] == 0, λ -> x[t]].

I tried following code, but that doesn't work. Any idea?

λ = 1;
Eq = x'[t] + (x[t] - λ) == 0;
sol = NDSolve[{Eq, x[0] == 0,
               WhenEvent[x'[t] == 0., λ -> x[t]]
              }, x, {t, 0, 5}][[1]]
share|improve this question
    
Does this get what you need: NDSolve[{x'[ t] + (x[t] - Boole[x'[t] == 0.] x[t] - \[Lambda] (1 - Boole[x'[t] == 0.])) == 0, x[0] == 0}, x, {t, 0, 5}]? –  kguler May 1 at 20:17
    
.. modify your when-event to WhenEvent[x'[t] == 0., x[t] -> x'[t]? –  kguler May 1 at 20:25
    
Are you sure the condition x'[t]==0 actually occurs? Look at DSolve[{x'[t]+(x[t]-\[Lambda])==0,x[0]==0},x,t] –  chuy May 1 at 20:29

1 Answer 1

There are other approaches to achieve what you have described. As I guess this is only a simplification of what you really need, here is how you would do it with WhenEvent:

equation = x'[t] + (x[t] - λ[t]) == 0;
sol = NDSolveValue[{equation, x[0] == 0, λ[0] == 1, 
   WhenEvent[x'[t] == 0.25, λ[t] -> x[t]]}, x, {t, 0, 5}, 
  DiscreteVariables -> {λ}]

the trick is to make λ a discrete dependent variable for the equation. I think this is not straightforward to find in the documentation as I remember I was also struggling with it when I first needed it. A discrete dependent variable will need an initial value but no differential equation and can be changed at any event just as other dependent variables (and only there). It will also become a function of time and can be in the list of variables to solve for at position 2 of the call to NDSolve. Once one knows how it works all that seems to be very consistent. The documentation for DiscreteVariables has some examples which are very close to your problem...

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.