using FindMinimum and FindRoot with "numerically defined" functions
- To: mathgroup at smc.vnet.net
 - Subject: [mg81044] using FindMinimum and FindRoot with "numerically defined" functions
 - From: Scott.T.Milner at gmail.com
 - Date: Sun, 9 Sep 2007 06:12:49 -0400 (EDT)
 
Many times I have wanted to use Mathematica to find the minimum or a
root of a function that was defined in terms of a numerical solution
to a differential equation.  (For example, solving a 1-dimensional ODE
boundary value problem by the "shooting method" can be expressed in
this way.)
Here is a simple example (in which we pretend that we do not know how
to solve analytically the ODE presented).
fcn[x_] := Block[{},
    ans = NDSolve[{y''[t] == -x y[t], y'[0] == 0, y[0] == 1}, y, {t,
0, 1}];
    y[1] /. ans[[1]]]
If you plot this function with Plot[fcn[x], {x, 0, 24}], you will find
a minimum at about x=10.  So for instance, I would like to find the
minimum of fcn[x] using FindMinimum[ ], but this does not work:
I attempt to use FindMinimum[ ] (supplying two initial values to avoid
using derivatives):
FindMinimum[fcn[x], {x, 10, 10.1}]
which generates a long string of error messages (NDSolve::ndnum and
several ReplaceAll::reps).
Similar errors are generated if I attempt to use this function with
FindRoot[ ], as in:
FindRoot[fcn[x] == 0., {x, 4, 4.1}]
Is there some way to successfully use FindMinimum[ ] and FindRoot[ ]
with a function defined in terms of the numerical solution to an ODE
using NDSolve, or other such "numerically defined" functions?