       Re: Recursive function

• To: mathgroup at smc.vnet.net
• Subject: [mg115945] Re: Recursive function
• From: Achilleas Lazarides <achilleas.lazarides at gmx.com>
• Date: Thu, 27 Jan 2011 03:39:15 -0500 (EST)

```For example,
Manipulate[
Transpose[
NestList[{#[], a #[] + b #[]} &, {1, 1}, 14]
] // Part[#, 2] &,
{a, 1, 10, 1},
{b, 1, 10, 1}
]

or you could extract the relevant numbers by eg Map[#[]&,NestList[etc]] instead etc.

On Jan26, 2011, at 11:06 AM, StatsMath wrote:

> On Jan 25, 3:32 am, "Sjoerd C. de Vries" <sjoerd.c.devr... at gmail.com>
> wrote:
>> You need to make x explicitely depend on a and b as well:
>>
>> ClearAll[x];
>> x[t_, a_, b_] := a*x[t - 1, a, b] + b*x[t - 2, a, b]
>> x[0, a_, b_] = 1;
>> x[1, a_, b_] = 1;
>> Manipulate[
>> Map[x[#, a, b] &, Range[0, 3]], {a, -1, 1, 0.5}, {b, -1, 1, .5}]
>>
>> Cheers -- Sjoerd
>>
>> On Jan 25, 10:20 am, StatsMath <stats.ma... at gmail.com> wrote:
>>
>>
>>
>>> I am trying to compute the following function:
>>
>>> x[t] = a * x[t-1] + b * x[t-2]
>>> x = 1
>>> x = 1
>>
>>> For different values of a & b.
>>
>>> Would like to use Manipulate[] so that I can change the values of a &
>>> b dynamically and observe what the resulting x[t] values are.
>>
>>> So wrote the following piece of code:
>>
>>> Clear["Global`*"]
>>> x[t_Integer] := x[t] = a x[t - 1] + b x[t - 2]
>>> x = 1;
>>> x = 1;
>>> Manipulate[Map[x[#] &, Range[0, 3]], {a, -1, 1, 0.5}, {b, -1, 1, .5}]
>>
>>> Got the following results:
>>> {1, 1, a + b, b + a (a + b)}
>>
>>> The a & b values are not being propagated so tried the following:
>>
>>> Clear["Global`*"]
>>> x[t_Integer] := x[t] = a x[t - 1] + b x[t - 2] /; t > 2
>>> x = 1;
>>> x = 1;
>>> Manipulate[
>>> Map[(If[# == 0 || # == 1, 1, a x[# - 1] + b x[# - 2]]) &,
>>> Range[0, 3]], {a, -1, 1, 0.5}, {b, -1, 1, .5}]
>>
>>> Got the following result:
>>> {1, 1, -2, -1 - a - b}
>>
>>> Finally got it working with the following code:
>>
>>> Clear["Global`*"]
>>> Manipulate[
>>> Map[(If[# == 0 || # == 1, x[#] = 1,
>>>    x[#] = a x[# - 1] + b x[# - 2]]) &, Range[0, 20]], {a, -1, 1 ,
>>> 0.5}, {b, -1, 1, .5}]
>>
>>> The above code looks ugly, so wondering if there is a different way to
>>> handle recursive functions.
>>
>>> Thanks!- Hide quoted text -
>>
>> - Show quoted text -
>
> Thanks for the above soln.
>
> Is there a way to use Nest[] or NestList[] for computing the recursive
> fn and still be able to dynamically change 'a' & 'b' values with
> Manipulate[]?
>
> Want to make sure I am not missing some technique in using Nest[] or
> NestList[] to accomodate the above scenario.
>
> Thanks!
>

```

• Prev by Date: Re: disable default Dynamic Updating Enabled
• Next by Date: Re: Help with While Loop Function
• Previous by thread: Re: Recursive function
• Next by thread: Time series minima and maxima