# Calling a function on a variable with parameters [duplicate]

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?

## marked as duplicate by m_goldberg, Mr.Wizard♦Feb 27 '15 at 8:05

• (I can also get it working by making L, T, V, and eqn explicit functions of m and g, but this looks awful, isn't easily modified, and seems overly complicated.) – Matthew Feb 27 '15 at 1:22
• Proposed duplicates: (11461), (69590). Also related: (3864) – Mr.Wizard Feb 27 '15 at 1:37
• Evaluate only works when applied to items at level 1 in an argument sequence. Your eqn appears at level 2. – m_goldberg Feb 27 '15 at 1:37
• Ah, cool. How can I fix that, then? – Matthew Feb 27 '15 at 1:40
• The following works. With[{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 1:53

The thing that made this work in the end was using dummy variables as the function parameters. This then allowed me to use /. to replace variables.
solution[mm_, gg_] :=

• Did you try blockSet? It does something much like this but in a more generalized way. – Mr.Wizard Feb 27 '15 at 2:16