I have defined a function which takes a coefficient for a differential equation as an argument, solves the differential equation with NDSolve, and returns the value of the solution at time t=10. I can plot this, but I can't seem to find the minimum value.
This code demonstrates the problem i'm having:
f[k_] := y[10] /. NDSolve[
{y''[t] == -k*y[t], y[1] == 2 , y'[2] == 1},
y,
{t, 0, 10}
];
Plot[f[k], {k, 0, 2}] (* works great *)
NMinimize[{f[k], k > 0, k < .75}, k] (* fails *)
The NMinimize fails with the error NDSolve::ndnum: Encountered non-numerical value for a derivative at t == 1.'.
So how do I find the minimum of my function f?
f[k_?NumericQ] := y[10] /. First @ NDSolve[{y''[t] == -k*y[t], y[1] == 2, y'[2] == 1}, y, {t, 0, 10}]. See the pitfalls question. You also might want to considerFindMinimuminstead ofNMinimize. – Michael E2 May 08 '14 at 23:36