       Re: defining a recursive formula?

• To: mathgroup at smc.vnet.net
• Subject: [mg54913] Re: defining a recursive formula?
• From: Peter Pein <petsie at arcor.de>
• Date: Sun, 6 Mar 2005 00:56:02 -0500 (EST)
• References: <d09dnj\$d80\$1@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```lou zion wrote:

> can someone help me represent the following?
>
> i have a quantity p[R_] := i* R -a
>
> i need to compute
>
> F[N_]  := p[p[p[p[p[R]]]]]  to N levels deep.
>
> how can i do this? thanks!
>
> lou
>
>

Lou,

you can use:

pn = Nest[i*#1 - a & , #1, #2]&;
In:=pn[R, 10]
Out=-a + i*(-a + i*(-a + i*(-a + i*(-a + i*(-a + i*(-a + i*(-a +
i*(-a + i*(-a + i*R)))))))))

but for larger values of n this becomes tedious.

I suggest the use of
pn2 = Evaluate[p /.
First[RSolve[{p[n + 1] == i*p[n] - a, p == #1}, p, n]]]&;

In:=pn2[x]
Out=(a - a*i^10 - i^10*x + i^11*x)/(-1 + i)

Of course pn2 is much faster than pn:

In:= First[Timing[y1 = pn[R, 1000]; ]]
Out= 0.25 Second
In:= First[Timing[y2 = pn2[R]; ]]
Out= 0. Second

Expanding y1 lasts a while...
In:= Timing[Simplify[y1 == y2]]
Out= {5.719*Second, True}

and further work with these expressions reveals even greater differences
in calculation time:

In:= First /@ (Timing[D[#1, i]; ] & ) /@ {y1, y2}
Out= {44.563 Second, 0. Second}