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[2]:=pn[R, 10]
Out[2]=-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[0] == #1}, p, n]]]&;
In[4]:=pn2[x][10]
Out[4]=(a - a*i^10 - i^10*x + i^11*x)/(-1 + i)
Of course pn2 is much faster than pn:
In[5]:= First[Timing[y1 = pn[R, 1000]; ]]
Out[5]= 0.25 Second
In[6]:= First[Timing[y2 = pn2[R][1000]; ]]
Out[6]= 0. Second
Expanding y1 lasts a while...
In[7]:= Timing[Simplify[y1 == y2]]
Out[7]= {5.719*Second, True}
and further work with these expressions reveals even greater differences
in calculation time:
In[8]:= First /@ (Timing[D[#1, i]; ] & ) /@ {y1, y2}
Out[8]= {44.563 Second, 0. Second}
It's your choice...
--
Peter Pein
Berlin