0

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?

Matthew
  • 221
  • 1
  • 8

1 Answers1

0

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_] := 
 NDSolve[{eqn /. {m -> mm, g -> gg}, z[0] == 0, z'[0] == 0}, 
  z[t], {t, 0, 10}]

I think this is a pretty unintrusive solution, since it only affects one line of code and I can write the rest as usual.

Matthew
  • 221
  • 1
  • 8
  • Did you try blockSet? It does something much like this but in a more generalized way. – Mr.Wizard Feb 27 '15 at 02:16
  • No, I didn't, but I'll try it for a future problem - I can see why it's useful, but the way I've got my problem set up, I think this works better. – Matthew Feb 27 '15 at 10:53