0
$\begingroup$

I have been attempting to generate a function that uses NDSolveValue to return the value of a solution to a system of ODE's when an event condition is triggered, as a function of the initial conditions.

When trying to evaluate this function for a couple of different inputs, I receive an error indicating that the "input value lies outside the range of data of the interpolating function". However, if I retype the function definition after every evaluation, I get no such error. This suggests something in the definition is retaining knowledge that I've used the function before, which is naturally undesirable. Is there any quick fix?

My code that replicates the error:

    testerror[a0_, b0_] := 
  NDSolveValue[{Derivative[1][\[Omega]][t] ==  -10*\[Omega][t] + 
      10*Pi*a[t] + 10, Derivative[1][a][t] == \[Omega][t]*b[t] - a[t],
     Derivative[1][b][t] == -\[Omega][t]*a[t] - b[t] + 10, \[Omega][
      0] == -Sqrt[10*Pi - 1], a[0] == a0, b[0] == b0, 
    WhenEvent[\[Omega][t] ==  -Sqrt[10*Pi - 1], end = t; 
     "StopIntegration"]}, {a[end], b[end]}, {t, \[Infinity]}];

testerror[-2.3, 2.5]; testerror[-2.35, 2.55]

If you re-evaluate the definition of the testerror function between evaluations however, you'll find no error occurs.

$\endgroup$
2
$\begingroup$

It's because you're using a global variable end.

testerror[a0_, b0_] := 
 Block[{end}, 
  NDSolveValue[{Derivative[1][\[Omega]][t] == -10*\[Omega][t] + 
   10*Pi*a[t] + 10, 
  Derivative[1][a][t] == \[Omega][t]*b[t] - a[t], 
  Derivative[1][b][t] == -\[Omega][t]*a[t] - b[t] + 10, \[Omega][
   0] == -Sqrt[10*Pi - 1], a[0] == a0, b[0] == b0, 
  WhenEvent[\[Omega][t] == -Sqrt[10*Pi - 1], end = t;
  "StopIntegration"]}, {a[end], b[end]}, {t, \[Infinity]}]];


testerror[-2.3, 2.5];testerror[-2.35, 2.55]

You can see no error is produced after using Block to localize end.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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