MathGroup Archive 2011

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

Search the Archive

Re: Recursive function

  • To: mathgroup at smc.vnet.net
  • Subject: [mg115925] Re: Recursive function
  • From: DrMajorBob <btreat1 at austin.rr.com>
  • Date: Wed, 26 Jan 2011 05:04:37 -0500 (EST)

Manipulate needs to "see" that its argument depends on a and b, so:

Clear[x]
x[a_, b_][t_Integer] :=
  x[a, b][t] = a x[a, b][t - 1] + b x[a, b][t - 2]
x[_, _][0] = 1;
x[_, _][1] = 1;
Manipulate[x[a, b] /@ Range[0, 3], {a, -1, 1, 0.5}, {b, -1, 1, .5}]

That's a good policy when writing functions, anyway.

Bobby

On Tue, 25 Jan 2011 03:20:19 -0600, StatsMath <stats.math8 at gmail.com>  
wrote:

> I am trying to compute the following function:
>
> x[t] = a * x[t-1] + b * x[t-2]
> x[0] = 1
> x[1] = 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[0] = 1;
> x[1] = 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[0] = 1;
> x[1] = 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!
>
>


-- 
DrMajorBob at yahoo.com


  • Prev by Date: Re: Recursive function
  • Next by Date: Re: Recursive function
  • Previous by thread: Re: Recursive function
  • Next by thread: Re: Recursive function