Using manipulate with a defined funtion to solve a ODE

As many in this community I'm new in Mathematica and while exploring the numerical solver for differential equations, I tried:

clc;
denshi[g_, k_, h_, vo_, t_] :=
NDSolve[{w''[t] - g - k*Exp[-w[t]/h] w'[t] == 0, w[0] == 0,
w'[0] == vo}, w, t];
Manipulate
[Plot[denshi[g, k, h, vo, t], {t, 0, 34}, PlotRange -> 10], {g, 0,
20}, {k, 0, 10}, {h, 0, 10}, {vo, 0, 10}]


But the program says more input is required. I would appreciate a suggestion for the code.

"...is incomplete; more input is needed" is just one of your problems. "More input required" appears because you put the [ on a different line than Manipulate. For example

Sin
[f[x]]


will generate the same error message. Another problem is that NDSolve doesn't return what you think it should return. You didn't mention this, but you could have just tried for example

denshi[1, 1, 1, 1, 1]


and you would see that it returns "1 cannot be used as a variable." This is because the third argument in NDSolve is supposed to be of the format {t, tmin, tmax}. This tells NDSolve to return an interpolating function between tmin and tmax, instead you just put a number as the third argument. If you thought that would give you the solution at that time you thought wrong, NDSolve cannot be used like that. The correct way to get the value at time tt is this:

denshi[g_, k_, h_, vo_, tt_] := First[w /. NDSolve[{
w''[t] - g - k*Exp[-w[t]/h] w'[t] == 0,
w[0] == 0,
w'[0] == vo
}, w, {t, 0, tt}]][tt];


I recommend that you pick this expression apart and try to understand why this code works.

Even after this you get an error, or lots of errors. They are all caused by the fact because

Plot[denshi[g, k, h, vo, t], {t, 1, 34}]


redefines t. It gives t a value even inside the function denshi. I think it's above your current level to understand why this syntax affects t inside denshi but if you want to look it up you can read about the Block function, Plot gives its iteration variable this type of scope. So we have to change the name of that as well. The solution is to use another symbol than t. We end up with

denshi[g_, k_, h_, vo_, tt_] := First[w /. NDSolve[{
w''[t] - g - k*Exp[-w[t]/h] w'[t] == 0,
w[0] == 0,
w'[0] == vo
}, w, {t, 0, tt}]][tt];
Manipulate[
Plot[denshi[g, k, h, vo, ttt], {ttt, 1, 34}],
{g, 1, 20}, {k, 1, 10}, {h, 1, 10}, {vo, 1, 10}
]


where I've also changed a few starting values because there are singularities at zero.

As you posed this as if it were a question about "the numerical solver" and then had a lot of other problems, problems related to Manipulate and Plot mostly, as well as problems with NDSolve, I recommend that you practice with more basic problems. Start by using NDSolve on its own, then put it inside Plot and then finally when you have that working put it inside Manipulate.

• The only possible singularity is at h=0. Anyway it was an extensive explanation of my mistakes and what to focus on, I appreciate it and will practice. – transistorNPN Mar 16 '15 at 0:16

Something like:

 Manipulate[
Block[{s, tmax = 10},
s = NDSolve[{w''[t] - g - k*Exp[-w[t]/h] w'[t] == 0, w[0] == 0,
w'[0] == vo}, w, {t, 0, tmax}];
Plot[w[t] /. s, {t, 0, tmax}]
]
, {g, 1, 10}, {k, 1, 10}, {h, 1, 10}, {vo, 1, 10}
]


returns: