       Re: Recursive function

• To: mathgroup at smc.vnet.net
• Subject: [mg115937] Re: Recursive function
• From: Dana DeLouis <dana.del at gmail.com>
• Date: Wed, 26 Jan 2011 05:06:52 -0500 (EST)

```On Jan 25, 4: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

<snip>

Hi.  I believe Table could be another option

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[Table[x[t,a,b],{t,0,10}],{a,-1,1,1/2},{b,-1,1,1/2}]

Perhaps if you wanted to change the number of returned values:

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[Table[x[t,a,b],{t,0,terms}],{terms,0,20,1},{a,-1,1,1/2},{b,-1,1,1/2}]

As a side note, what you started with looked like you were going the RSolve route:

Not as elegant a solution as you have, but...

equ={
x[t]=a*x[t-1]+b*x[t-2],
x=1,
x=1
};

x[t]/.RSolve[equ,x[t],t]//FullSimplify//First

<< output here >>

Replace Sqrt[a^2+4 b]  with 'k since Sqrt[a^2+4 b] occurs often.

<< a little better >>

Since k is in the denominator,  combinations of a & b could make k = 0 (Divide by zero error).  Hence...

Limit[ %, k->0]
2^-t a^(-1+t) (a+2 t-a t)

Therefore...

f[t_,a_,b_]:=Module[{k=Sqrt[a^2+4 b]},
If[Or[t=0,t=1],1,
If[k=0,
2^-t a^(-1+t) (a+2 t-a t),
(1/k)2^(-1-t) ((a-k)^t (-2+a+k)+(2-a+k) (a+k)^t)]//N//Chop//Rationalize]]

Manipulate[Table[f[t,a,b],{t,0,terms}],{terms,0,20,1},{a,-1,1,1/2},{b,-1,1,1/2}]

= = = = = = = = = =
HTH  : >)
Dana DeLouis

To understand recursion, one must first understand recursion.   :>o

```

• Prev by Date: Re: CUDADot[] does not work with rectangular matrices
• Next by Date: Re: Recursive function
• Previous by thread: Re: Recursive function
• Next by thread: Re: Recursive function