Re: Problems with FindRoot and recursive functions

• To: mathgroup at smc.vnet.net
• Subject: [mg89816] Re: Problems with FindRoot and recursive functions
• From: m.r at inbox.ru
• Date: Sat, 21 Jun 2008 05:31:28 -0400 (EDT)
• References: <g35g0m\$9bv\$1@smc.vnet.net> <48565C59.1040200@gmail.com>

```Don't forget that certain iterative functions first evaluate their
arguments symbolically (see tutorial/
UnconstrainedOptimizationSymbolicEvaluation for details). If you have
your function defined as x[t_?NumericQ] := ... and then evaluate
FindRoot[x[t][[1]], {t, t0}], then x[t][[1]] is first evaluated in a
context (as in Block) where t doesn't have a value. Therefore x[t] is
unevaluated and x[t][[1]] evaluates to t. So the function to be
minimized will be t.

You need to be very careful about listability too; suppose your
function is defined as f[x_] := Norm[x - {1, 2}] and you want to
evaluate it for vector-valued x. Then you have to make sure that it's
not prematurely evaluated to Norm[{x - 1, x - 2}].

Here are some ways to set up your problem:

x1[t_] := If[t < 0, {t, 1}, 0.5 + x1[t - 1]]
xf[t_?NumericQ] := x1[t][[1]]

FindRoot[xf[t], {t, 1.5}]
FindRoot[x1[t].{1, 0}, {t, 1.5}]
FindRoot[x1[t][[1]], {t, 1.5}, Evaluated -> False]

Maxim Rytin
m.r at inbox.ru

On Jun 18, 3:26 am, "Daniel Kuang Chen Liu"
<dkl... at student.monash.edu.au> wrote:
> To Jean-Marc and dh,
>
> Forcing the use of numeric arguments still doesn't seem to work.
> The correct answer is {t -> 0.5} but we get {t -> 0} when we make the
> x1[t_?NumericQ] modification. NumericQ appears to stop the recursion proc=
ess
> at {t,1}.
>
> Thank you for the help.
>
> Daniel Liu
>
> On Mon, Jun 16, 2008 at 10:28 PM, Jean-Marc Gulliet <
>
> jeanmarc.gull... at gmail.com> wrote:
> > Daniel Kuang Chen Liu wrote:
>
> >  I have a recursive function of the form
>
> >>  x1[t_] := If[t < 0, {t, 1}, 0.5 + x1[t - 1]]
>
> >> which returns a list of length 2, and the first element has a root at
> >> t=0.5
>
> >>> In[3]:= x1[0.5]
> >>>> Out[3]= {0., 1.5}
>
> >> I want to use FindRoot to determine t0 such that x1[t0][[1]] == 0,=
so I
> >> use
> >> the following code
>
> >>  FindRoot[x1[t][[1]] == 0, {t, 0.5}]
>
> >> to which Mathematica complains
>
> >> During evaluation of In[6]:= FindRoot::nlnum: The function value
> >> {False} is not a list of numbers with dimensions {1} at {t} = {0.5}.
>
> >> It would much appreciated if someone could tell me what is wrong with =
the
> >> code.
>
> > Hi Daniel,
>
> > You just have to ensure that the function x1 is called for numeric valu=
es
> > only [1]. For instance,
>
> >    x1[t_?NumericQ] := If[t < 0, {t, 1}, 0.5 + x1[t - 1]]
> >    FindRoot[x1[t][[1]] == 0, {t, 0.5}]
>
> >    {t -> 0.}
>
> > Regards,
> > - Jean-Marc
>
> > [1] "How do I write a function which evaluates only when the argument i=
s a
> > number?"
> >http://support.wolfram.com/mathematica/kernel/features/evalwhennumber...

```

• Prev by Date: Re: Combining pure functions to produce a new pure
• Next by Date: Re: Combining pure functions to produce a new pure
• Previous by thread: Re: Problems with FindRoot and recursive functions
• Next by thread: minor progress with 6.0 Documentation