MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Problems with FindRoot and recursive functions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg89742] Re: Problems with FindRoot and recursive functions
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Thu, 19 Jun 2008 05:45:17 -0400 (EDT)
  • Organization: The Open University, Milton Keynes, UK
  • References: <g35g0m$9bv$1@smc.vnet.net> <48565C59.1040200@gmail.com> <g3agqp$plh$1@smc.vnet.net>

Daniel Kuang Chen Liu 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 process
> at {t,1}.

Daniel,

As far as I can tell, the recursion is fine. However, interactions 
between FindRoot and your function might not be what you expect. (I 
would say that having a continuous function is much better.) When fed 
with one starting value, FindRoot uses Newton method, and with two 
starting values, it uses a Secant method. We can follow the recursive 
calls thanks to Trace[] and the option EvaluationMonitor allows us to 
see what values of t are used by FindRoot[].

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

Out[2]= {t -> 0.}

In[3]:= Trace[x1[0.5]]

Out[3]= {x1[0.5], {NumericQ[0.5], True},

   If[0.5 < 0, {0.5, 1}, 0.5 + x1[0.5 - 1]], {0.5 < 0, False},

   If[False, {0.5, 1}, 0.5 + x1[0.5 - 1]], 0.5 + x1[0.5 - 1],

   {{0.5 - 1, -0.5}, x1[-0.5], {NumericQ[-0.5], True},

    If[-0.5 < 0, {-0.5, 1}, 0.5 + x1[-0.5 - 1]], {-0.5 < 0, True},

    If[True, {-0.5, 1}, 0.5 + x1[-0.5 - 1]], {-0.5, 1}}, 0.5 + {-0.5, 1},

   {0.5 - 0.5, 0.5 + 1}, {0.5 - 0.5, 0.}, {0.5 + 1, 1.5}, {0., 1.5}}

In[4]:= FindRoot[x1[t][[1]] == 0, {t, 0.5},
  EvaluationMonitor :> Print["t = ", t]]

During evaluation of In[4]:= t = 0.5

During evaluation of In[4]:= t = 0.

Out[4]= {t -> 0.}

In[5]:= FindRoot[x1[t][[1]] == 0, {t, 0.3, 0.7},
  EvaluationMonitor :> Print["t = ", t]]

During evaluation of In[5]:= t = 0.3

During evaluation of In[5]:= t = 0.7

During evaluation of In[5]:= t = 0.7

During evaluation of In[5]:= t = 0.3

During evaluation of In[5]:= t = 0.3

During evaluation of In[5]:= t = 0.

Out[5]= {t -> 0.}

Regards,
-- Jean-Marc

> Thank you for the help.
> 
> Daniel Liu
> 
> On Mon, Jun 16, 2008 at 10:28 PM, Jean-Marc Gulliet <
> jeanmarc.gulliet 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 values
>> 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 is a
>> number?"
>> http://support.wolfram.com/mathematica/kernel/features/evalwhennumber.html
>>
>>
> 



  • Prev by Date: Re: Workbench editing snag
  • Next by Date: Re: Combining pure functions to produce a new pure function
  • Previous by thread: Re: Problems with FindRoot and recursive functions
  • Next by thread: Re: Problems with FindRoot and recursive functions