MathGroup Archive 2005

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

Search the Archive

Re: defining a recursive formula?


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


  • Prev by Date: Re: Kernel Problem
  • Next by Date: Re: Re: help browser cells inactive?
  • Previous by thread: Re: defining a recursive formula?
  • Next by thread: Automating the generation of .m files.