#hw17.txt; Due March 30, 2014; Frank Wagner Help:=proc(): print(` SIrec(L,Ini,f,m,n) `): print(` dIBVP(L,Ini,Fini,f,n,N) `): print(` GR(p,N) `): print(` LGR(p,N) `): end: ################### #####Problem 1##### ################### #SIrec(L,Ini,f,m,n) inputs lists L and Ini, a function #f in m, and a number n and outputs the n-th term of the #solution of the linear recurrence equation with #constant coefficients #a(n)=L[1]*a(n-1)+L[2]*a(n-2)+ ... + L[-1]*a(n-nops(L))+f(n) #and initial conditions #a(0)=Ini[1], ..., a(nops(Ini)-1)=Ini[-1] SIrec:=proc(L,Ini,f,m,n) local i: if nops(L)<>nops(Ini) then print(Ini, L, `should be lists of the SAME length `): RETURN(FAIL): fi: if n<0 then 0: elif nnops(Ini)+nops(Fini) then RETURN(FAIL): fi: var:={seq(a[i],i=0..N)}: eq:={seq(a[i]=Ini[i+1],i=0..nops(Ini)-1), seq(a[N-i]=Fini[i+1],i=0..nops(Fini)-1)}: eq:= eq union {seq(a[i]-add(L[j]*a[i-j],j=1..nops(L))-subs(n=i,f), i=nops(L)..N)}: var:=solve(eq,var): if var=NULL then RETURN(FAIL): else subs(var,[seq(a[i],i=0..N)]): fi: end: ################### #####Problem 3##### ################### #dBVP process needed for this part# #dBVP(L,Ini,Fini,N): dBVP: Discrete boundary value problem #The list of length N+1 #giving the values of a(n) for n=0 to n=N #to the solution of the linear recurrence equation #with constant coefficients #a(n)=L[1]*a(n-1)+L[2]*a(n-2)+ ... + L[-1]*a(n-nops(L)) #a(0)=Ini[1], ..., a(nops(Ini)-1)=Ini[-1]. #a(N)=Fini[1], a(N-1)=Fini[2], .. dBVP:=proc(L,Ini,Fini,N) local eq,var,a,i: if nops(L)<>nops(Ini)+nops(Fini) then RETURN(FAIL): fi: var:={seq(a[i],i=0..N)}: eq:={seq(a[i]=Ini[i+1],i=0..nops(Ini)-1), seq(a[N-i]=Fini[i+1],i=0..nops(Fini)-1)}: eq:= eq union {seq(a[i]-add(L[j]*a[i-j],j=1..nops(L)), i=nops(L)..N)}: var:=solve(eq,var): if var=NULL then RETURN(FAIL): else subs(var,[seq(a[i],i=0..N)]): fi: end: #end dBVP process# #GR(p,N) inputs a number p between 0 and 1 and a #positive integer N and outputs a list of length #N+1 so that G[i+1] gives you the probability that #you exit the casino a winner if you enter with #i dollars, repeatedly play a game where the #probability of winning 1 dollar is p and the probability #of losing 1 dollar is 1-p, and leave when you either have #N dollars or nothing. GR:=proc(p,N) local L,Ini,Fini: #Let G be the list of winning probabilities, with #G[i+1] being the probability of leaving with N dollars #given you enter with i dollars. #Clearly, G[1]=0 and G[N+1]=1. #If we start with i dollars with 0