I have an equation that contains parameters. I want to use it in a function like NDSolve, but I can't seem to figure out how to do so. Here's an example:
I define my equation like so:
L := T - V
T := 1/2 m z'[t]^2
V := m g z[t]
eqn := Dt[D[L, z'[t]], t] - D[L, z[t]] == 0
Then, I want to use it in NDSolve, so I write the following:
solution[m_, g_] := NDSolve[{Evaluate[eqn], z[0] == 0, z'[0] == 0}, z[t], {t, 0, 10}]
This doesn't work as expected, though, and solution[1, 1] fails to output a result.
I also note that
theEqn[m_, g_] := Evaluate[eqn]
theEqn[1, 1]
works fine, and outputs
1 + z''[t] == 0
Furthermore, the following code (where I have just copied and pasted the definitions into the code) does work:
solution[m_, g_] :=
NDSolve[{Dt[D[1/2 m z'[t]^2 - m g z[t], z'[t]], t] -
D[1/2 m z'[t]^2 - m g z[t], z[t]] == 0, z[0] == 0, z'[0] == 0},
z[t], {t, 0, 10}]
I feel there ought to be some way that I'm missing to do that substitution automatically. How do I do this?
L,T,V, andeqnexplicit functions ofmandg, but this looks awful, isn't easily modified, and seems overly complicated.) – Matthew Feb 27 '15 at 01:22Evaluateonly works when applied to items at level 1 in an argument sequence. Youreqnappears at level 2. – m_goldberg Feb 27 '15 at 01:37blockSetabstraction from (69590) and tell me if your function behaves as desired; I believe it will. – Mr.Wizard Feb 27 '15 at 01:42With[{eqn = eqn /. {m -> 1, g -> 1}}, NDSolve[{eqn, z[0] == 0, z'[0] == 0}, z[t], {t, 0, 10}]]. Perhaps you can adapt it to your needs. – m_goldberg Feb 27 '15 at 01:53