This is a copyable minimal example to illustrate the problem that occours when I include one parameter U
into a differential equation which I want to solve with NDSolve
. If I do not include a parameter into my differential equation, the following code works just fine to solve the equation, and evaluates the solution to the equation at some point:
ClearAll[H, v, es, eiginst, sol, hope, newhope]
H[t_] := {{0, 1}, {1, t}}
v[t_] := Table[Subscript[v, i][t], {i, 1, 2}]
es[t_] := Eigensystem[N[(H[t])]]
eiginst[t_] := Sort[Transpose[es[t]]]
sol = NDSolve[LogicalExpand[
I*v'[t] == H[t].v[t] && v[0] == eiginst[0][[1]][[2]]],
v[t], {t, 0, 10}];
hope[t_] := Evaluate[v[t] /. sol]
hope[2]
(* {-0.215432 - 0.94635 I, 0.0773099 + 0.228111 I}} *)
However, if I include one parameter U
into the exact same code I get errors, which I do not understand since it is the same code:
ClearAll[H, v, es, eiginst, sol, hope, newhope]
H[t_, U_] := U*{{0, 1}, {1, t}}
v[t_] := Table[Subscript[v, i][t], {i, 1, 2}]
es[t_, U_] := Eigensystem[N[(H[t, U])]]
eiginst[t_, U_] := Sort[Transpose[es[t, U]]]
sol[U_] := NDSolve[LogicalExpand[
I*v'[t] == H[t, U].v[t] && v[0] ==eiginst[0, U][[1]][[2]]],
v[t], {t, 0, 10}];
newhope[t_, U_] := Evaluate[v[t] /. sol[U]]
newhope[2, 1]
NDSolve::ndnum: Encountered non-numerical value for a derivative at t == 0.`. >>
ReplaceAll::reps: ... >>
NDSolve::dsvar: ... >>
ReplaceAll::reps: .. >>
ParametricNDSolve
instead ofNDSolve
$\endgroup$ParametricNDSolveValue
(a variant ofParametricNDSolve
). It is no slower than creating a function that, in effect, replicatesParametricNDSolve
and may be more reliable and convenient. $\endgroup$