Re: help working with functions (follow-up)
- To: mathgroup at smc.vnet.net
- Subject: [mg60007] Re: [mg59987] help working with functions (follow-up)
- From: Bob Hanlon <hanlonr at cox.net>
- Date: Sun, 28 Aug 2005 03:07:41 -0400 (EDT)
- Reply-to: hanlonr at cox.net
- Sender: owner-wri-mathgroup at wolfram.com
Amplifying on my previous response f1[i_,j_,n_]:= Module[{k=0}, Nest[ Append[#,#[[-2]]+#[[-1]]+i*j^(k++)]&, {0,1}, n-2]]; f1[0,4,10] {0,1,1,2,3,5,8,13,21,34} You can also solve for the closed form expr=FullSimplify[a[k]/. RSolve[{a[k]==a[k-1]+a[k-2]+i*j^(k-2),a[0]==0,a[1]==1}, a[k], k][[1]], Element[k, Integers]] (2^(-k - 1)*(2*Sqrt[5]*(-(1 - Sqrt[5])^k + (1 + Sqrt[5])^k)*((j - 1)*j - 1) + i*(5*2^(k + 1)*j^k + 2*Sqrt[5]*((1 - Sqrt[5])^k - (1 + Sqrt[5])^k)*j + (-5 + Sqrt[5])*(1 + Sqrt[5])^k - (1 - Sqrt[5])^k*(5 + Sqrt[5]))))/(5*((j - 1)*j - 1)) Consequently, f2[i_,j_,n_]:=Table[(2^(-k-1)*(2*Sqrt[5]*(-(1-Sqrt[5])^k+(1+ Sqrt[5])^k)*((j-1)*j-1)+i*(5*2^(k+1)*j^k+2*Sqrt[5]*(( 1-Sqrt[5])^k-(1+Sqrt[5])^k)*j+(-5+Sqrt[5])*( 1+Sqrt[5])^ k-(1-Sqrt[ 5])^k*(5+Sqrt[5]))))/(5*((j-1)*j-1)),{ k,0,n-1}]//Simplify; Checking that f1 and f2 are equivalent And@@Flatten@Table[f1[i,j,n]==f2[i,j,n],{i,5},{j,5},{n,2,10}] True For i=0 the sequence is the Fibonacci sequence FullSimplify[ Coefficient[expr, i,0]== CoefficientList[expr,i][[1]]== Collect[expr,i][[1]]== Fibonacci[k], Element[k, Integers]] True n=25; f1[0,j,n]==f2[0,j,n]==Table[Fibonacci[k],{k,0,n-1}] Clear[n]; True Bob Hanlon > > From: Bob Hanlon <hanlonr at cox.net> To: mathgroup at smc.vnet.net > Date: 2005/08/27 Sat AM 10:41:02 EDT > To: "hawkmoon269" <rson at new.rr.com>, <mathgroup at smc.vnet.net> > Subject: [mg60007] Re: [mg59987] help working with functions > > f[i_,j_,n_]:= Module[{k=0}, > Nest[ > Append[#,#[[-2]]+#[[-1]]+i*j^(k++)]&, > {0,1}, n-2]]; > > f[0,4,10] > > {0,1,1,2,3,5,8,13,21,34} > > > Bob Hanlon > > > > > From: "hawkmoon269" <rson at new.rr.com> To: mathgroup at smc.vnet.net > > Date: 2005/08/27 Sat AM 04:11:20 EDT > > Subject: [mg60007] [mg59987] help working with functions > > > > I have the following program written: > > > > Clear[a]; Clear[seq]; > > seq = Array[a,10,0]; a[0]=0; a[1]=1; > > Do[a[k] = a[k-1] + a[k-2] + 0 4^(k-2), {k,2,10}]; > > seq > > > > It works fine and correctly outputs > > > > {0, 1, 1, 2, 3, 5, 8, 13, 21, 34} > > > > My sense,though, is that this is inelegant newbie code and am > > interested in hearing alternative ways to program with the same output. > > Also, I would like to generalize this as a callable function with > > arguments i,j,n...something like: > > > > f[i_,j_,n_]:= > > Clear[a]; Clear[seq]; > > seq = Array[a,n,0]; a[0]=0; a[1]=1 > > Do[a[k] = a[k-1] + a[k-2] + i j^(k-2), {k,2,n}] > > seq > > > > So that > > > > f[0,4,10] > > > > would output > > > > {0, 1, 1, 2, 3, 5, 8, 13, 21, 34} > > > > I've tried variants of the foregoing, but can't get it to work > > correctly... > > > > Many thanks in advance for ideas and assistance etc. > > > > h > > > > >