Re: using FindMinimum and FindRoot with "numerically defined" functions
- To: mathgroup at smc.vnet.net
- Subject: [mg81060] Re: [mg81044] using FindMinimum and FindRoot with "numerically defined" functions
- From: DrMajorBob <drmajorbob at bigfoot.com>
- Date: Sun, 9 Sep 2007 06:21:09 -0400 (EDT)
- References: <26460367.1189255779850.JavaMail.root@m35>
- Reply-to: drmajorbob at bigfoot.com
It works error-free at this machine, in v6:
fcn[x_?NumericQ] :=
Block[{y, t},
y[1] /. First@
NDSolve[{y''[t] == -x y[t], y'[0] == 0, y[0] == 1},
y, {t, 0, 1}]]
Plot[fcn[x], {x, 0, 24}]
FindMinimum[fcn[x], {x, 10, 10.1}]
{-1., {x -> 9.86961}}
FindRoot[fcn[x] == 0., {x, 4, 4.1}]
{x -> 2.4674}
FindRoot[fcn'[x] == 0., {x, 4, 4.1}]
{x -> 9.8696}
Bobby
On Sat, 08 Sep 2007 03:02:24 -0500, <Scott.T.Milner at gmail.com> wrote:
> 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?
>
>
>
--
DrMajorBob at bigfoot.com