Re: Simple recursive assignment

• To: mathgroup at smc.vnet.net
• Subject: [mg16021] Re: Simple recursive assignment
• From: dreissBLOOP at bloop.earthlink.net (David Reiss)
• Date: Sat, 20 Feb 1999 02:52:08 -0500
• Sender: owner-wri-mathgroup at wolfram.com

In article <7ajadc\$imu at smc.vnet.net>, "Peltio" <pelt.ioNOS at PAMiol.it> wrote:

> Can someone spare a little time for a trivial question?
>
> The assignement
>     a[0]=1.;
>     a[1]=2.;
>     a[n_]:=a[n]=a[n-1]-a[n-2]/n
> works fine, all right...
>     a[5]
>         0.291667
>
> But how can I achieve the same results when the expression a[n-1]-a[n-2]/n
> is stored in a variable expr? The following approach leads to a Recursion
> Limit error
>     expr=b[n-1]-b[n-2]/n;
>
>     b[0]=1;
>     b[1]=2;
>     b[n_]:=b[n]=expr
>
>     b[3]
>         \$RecursionLimit::reclim: etc.
>
> I tried a few combinations with Hold, Release, and so on but I can't fool
> the recursion trap.I'm sure it's something trivial I'm missing
> Any help would be greatly appreciated.
> Thanks
> Peltio,
> peltioNO at SPAMusa.net
>
> (remove NO SPAM to e-mail me)

Hi Peltio,

One way to do what you want is (as an example) to define
expr as a function:

In[1]:= ClearAll[b, expr]

In[2]:=

expr[n_] = b[n - 1] - b[n - 2]/n;

b[0] = 1;

b[1] = 2;

b[n_] := b[n] = expr[n];

In[4]:= b[2]

Out[4]= 3/2

In[5]:= b[3]

Out[5]= 5/6

Regards,

David

--

----------------------------------------

Scientific Arts:
Creative Services and Consultation
for the Applied and Pure Sciences

David Reiss
Email: dreiss at !SPAMscientificarts.com

----------------------------------------

Remove the !SPAM to send email

• Prev by Date: Re: Simple recursive assignment
• Next by Date: Re: Simple recursive assignment
• Previous by thread: Re: Simple recursive assignment
• Next by thread: 3D data graphing