3
$\begingroup$

Consider the following code:

Sol = ParametricNDSolveValue[{e1'[t] == s e2[t], e2'[t] == -s e1[t],e1[0] == 1, e2[0] == 0}, {e1, e2}, {t, 0, 1}, s]
fun[t_, s_] := Through[Sol[s][t]][[1]];
Plot[fun[t, 1], {t, 0, 10}]
dfun[t_, s_] = Derivative[1, 0][fun][t, s]
Plot[dfun[t, 10], {t, 0, 1}]

The aim is to find and plot the derivative in t of fun, but I get the output:

s'[t]

and a line along the x axis as a plot, which is the wrong solution.

How do I find the derivative of the solution of ParametricNDSolveValue?

EDIT To be clear: I need to keep s as a parameter, I know that if I write dfun[t_] = D[Through[Sol[1][t]][[1]], t] I can plot the deirvative, but I do not want to fix the value of s

$\endgroup$
3
  • $\begingroup$ Inspect the sub expressions: Through[Sol[s][t]] is different from Through[Sol[1][t]], so fun[t, s] behaves differently when s is a symbol and when it’s numeric. $\endgroup$
    – Michael E2
    Jul 3 at 13:08
  • $\begingroup$ @MichaelE2 I see the problem, but how do I solve it? The real aim is to use this derivative inside another ParametricNDSolveValue $\endgroup$
    – mattiav27
    Jul 3 at 13:14
  • 1
    $\begingroup$ You could use s_?NumericQ in the definition of fun, so that Sol[s] would evaluate to a list before part 1 is taken. Or you could use Indexed[…, 1] instead of …[[1]]. You could also add e1’ to the list of expressions to return {e1, e2, e1’} and take part 3 with NumericQ or Indexed as mentioned. $\endgroup$
    – Michael E2
    Jul 3 at 13:38
4
$\begingroup$

We can include derivative in solution as follows

sol = ParametricNDSolveValue[{e1'[t] == s e2[t], e2'[t] == -s e1[t], 
   e1[0] == 1, e2[0] == 0}, {e1, e2, e1', e2'}, {t, 0, 1}, s]
fun[t_, s_] := Through[sol[s][t]][[1]];
Plot[fun[t, 1], {t, 0, 10}]

dfun[t_, s_] := Through[sol[s][t]][[3]];
Plot[dfun[t, 10], {t, 0, 1}]

Figure 1

$\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.