4
$\begingroup$

I would like to have a variable start point for my initial conditions in ParametricNDSolve. I was hoping ideally this would look like the following (with a random DE used for the example).

soln = ParametricNDSolve[{v''[x] == 10, v'[a] == 1, v[a] == 1}, 
  v, {x, a, 10}, {a}]

vsoln = v /. soln


vsoln[1]

However, this gives the following error:

enter image description here

My expectation is that this is caused by Mathematica not evaluating the given value of a first and so the initial values for the variable are not defined in a way that it can access. Any suggestions here would be greatly appreciated. I'm not nessasarily tied to using ParametricNDSolve but I would like to avoid having to do a change of variable to set v[a] $\to$ v*[0], where v* is some shifted function.

$\endgroup$
3
$\begingroup$

Original Solution

One possible solution is to change ParametricNDSolve to NDSolve but hold the evaluation of the equation. Then later when you're ready and know the desired value of a make the appropriate substitution for a and release the evaluation:

soln = Hold[
  NDSolve[{v''[x] == 10, v'[a] == 1, v[a] == 1}, v, {x, a, 10}]]

vsoln[aa_] :=  v /. First[ReleaseHold[ReplaceAll[soln,  a -> aa]]]

vsoln[1]

Plot[vsoln[1][t], {t, 1., 10.}]

enter image description here enter image description here

A Second Method

A very similar alternative method that I think is more intuitive is to instead of holding include a condition to not evaluate unless a numerical value is given:

soln[a_?NumericQ] := 
 NDSolve[{v''[x] == 10, v'[a] == 1, v[a] == 1}, v, {x, a, 10}]


vsoln[a_] := v /. First[soln[a]]

vsoln[1]
Plot[vsoln[1][t], {t, 1, 10}]

enter image description here enter image description here

$\endgroup$
3
  • $\begingroup$ Now, I'm actually quite surprised that this isn't the default behavior of ParametricNDSolve. If anyone has more knowledge is there an intuitive reason it doesn't behave in the above manner? $\endgroup$
    – akozi
    Dec 17 '21 at 16:28
  • 1
    $\begingroup$ That ParametricNDSolve(Value) is unable to do this looks reportable to me; I see no obvious reason why it should fail. $\endgroup$ Dec 17 '21 at 16:31
  • $\begingroup$ @J.M. thanks! I'll fill out that document! $\endgroup$
    – akozi
    Dec 17 '21 at 16:35
0
$\begingroup$

In this particular case, you can use DSolve

eqns = {v''[x] == 10, v'[a] == 1, v[a] == 1};

soln = DSolve[eqns, v, x][[1]]

(* {v -> Function[{x}, 1 - a + 5 a^2 + x - 10 a x + 5 x^2]} *)

Verifying,

eqns /. soln

(* {True, True, True} *)

Plotting,

Plot3D[Evaluate[v[x] /. soln], {x, 0, 10}, {a, 0, 5},
 ColorFunction ->
  Function[{x, a, v}, ColorData[97][If[x >= a, 2, 1]]],
 PlotPoints -> 75,
 MaxRecursion -> 5,
 AxesLabel -> (Style[#, 14] & /@ {x, a, v})]

enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ I was assuming the OP just posted a toy example, but really had a problem with a more complicated RHS. $\endgroup$ Dec 17 '21 at 18:51

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.