5
$\begingroup$

I have had success in using WhenEvent to reset or change a variable within NDSolve with ordinary differential equations. My working, functioning MWE is:

Clear[usol, u, t, x, λ];
usol = NDSolveValue[
  {
   D[u[t, x], t] == λ[t, x] D[u[t, x], x, x],
   u[0, x] == 0,
   u[t, 0] == Sin[t],
   u[t, 5] == 0,
   λ[0, x] == 1,
   WhenEvent[t > 5, λ[t, x] -> 1.25*λ[t, x]]
   },
  u,
  {t, 0, 10},
  {x, 0, 5},
  DiscreteVariables -> λ]
Plot3D[usol[t, x], {t, 0, 10}, {x, 0, 5}, PlotRange -> All]
NIntegrate[usol[1, x], {x, 0, 5}]) (*This evaluates to 0.669665*)

However, I would like to use WhenEvent to perform an operation such as StopIntegration or changing a variable when a calculation such as area under the curve represented by the dependent variable attains a certain value. For this, I tried:

Clear[usol, u, t, x, λ];
usol = NDSolveValue[
  {
   D[u[t, x], t] == λ[t, x] D[u[t, x], x, x],
   u[0, x] == 0,
   u[t, 0] == Sin[t],
   u[t, 5] == 0,
   λ[0, x] == 1,
   WhenEvent[NIntegrate[usol[t, x], {x, 0, 5}] > 0.1, end = t; 
    "StopIntegration"]
   },
  u,
  {t, 0, 10},
  {x, 0, 5},
  DiscreteVariables -> λ]

However, I am beset with a slew of error messages in this latter case. The error message starts with:

The integrand usol[0,x] has evaluated to non-numerical values for all sampling points in the region with boundaries {{0,5}}

The other question here on WhenEvent and NDSolve for partial differential equations did not have similar errors.

I am not sure what is going wrong; the operation NIntegrate[usol[t, x], {x, 0, 5}] < 1 should have "ended" the NDSolve Integration at t=1 at the least as in my functioning MWE.

$\endgroup$
6
  • 4
    $\begingroup$ You can't use usol until NDSolveValue ends. So, you certainly can't use it inside NDSolveValue $\endgroup$ Feb 6, 2016 at 4:56
  • $\begingroup$ @Dr.belisarius So, how do I proceed? I am sorry but I do not have a clue. What you say makes sense. $\endgroup$
    – dearN
    Feb 6, 2016 at 4:58
  • $\begingroup$ Probably include an auxiliary function whose derivative is u and include that equation inside NDSolve. I'm not sure exactly how this would work. I might think on it. $\endgroup$
    – march
    Feb 6, 2016 at 5:05
  • 3
    $\begingroup$ I think if you just change usol to u inside NIntegrate, it should work. $\endgroup$
    – march
    Feb 6, 2016 at 20:45
  • 2
    $\begingroup$ I'm voting to close this question as off-topic because it was solved in a comment $\endgroup$ Feb 22, 2016 at 3:20

0

Browse other questions tagged or ask your own question.