Group Abstract Group Abstract

Message Boards Message Boards

0
|
4.8K Views
|
2 Replies
|
1 Total Like
View groups...
Share
Share this post:

Why doesn't NDSolve StepMonitor return values for derivative term?

Posted 10 years ago

In the evaluation below, I want both y[t] and y'[t] sown by StepMonitor, but for some reason y[t] is sown unevaluated. It appears from the output that the derivative is taken after t is replaced by its value like y'[t->value], rather than y'[t]/.t->value. Does anyone see my error?

{sol, steps} = 
  Reap[NDSolveValue[{y'[t] == -y[t], y[0] == 1}, {y[t], y'[t]}, {t, 0,
      5}, StepMonitor :> Sow[{t, y[t], y'[t]}]]];

Plot[sol, {t, 0, 5}, PlotLegends -> {"y[t]", "y'[t]"}]

enter image description here

steps[[1, 1 ;; 5]]

{{0.000190613, 0.999809, Derivative[1][y][0.000190613]}, {0.000381227,
   0.999619, Derivative[1][y][0.000381227]}, {0.00489873, 0.995113, 
  Derivative[1][y][0.00489873]}, {0.00941624, 0.990628, 
  Derivative[1][y][0.00941624]}, {0.0139337, 0.986163, 
  Derivative[1][y][0.0139337]}}
POSTED BY: David Keith
2 Replies
Posted 10 years ago

Thank you, Ilian. That makes perfect sense. Now, please pour yourself a nice beer and have a very Merry Christmas!

Warm regards, David

POSTED BY: David Keith

At the time when StepMonitor is invoked, there is no solution available to differentiate yet. There is just a discrete grid of points $t_1, t_2, \ldots$ for each time step and corresponding approximate solution values $y_1, y_2, \ldots$ . The plot works because the solution has already been built up as an interpolating function, and Derivative knows what to do with these.

Of course, if solving an equation of the form $y'(t) = f(t, y)$, then why not just use Sow[f[t, y[t]] to effectively sow the derivative values at each step?

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