Processing math: 100%

First time here? Check out the FAQ!

Ask Your Question
0

How can you get the n'th function in a sequence defined by a recurrence relation?

asked 5 years ago

Let a_0(x) and b_0(x) be given functions. Then define a_n and b_n by the following relations (or any)

a_n(x) = a_{n-1}(x+1) - a_{n-1}(x) + a_0(x+1)a_{n-1}(x) + b_{n-1}(x) b_n(x) = b_{n-1}(x+1) - b_{n-1}(x) + a_{n-1}(x)b_0(x+1)

How can I write a program to print the n'th pair of functions in the sequence?

I'm new to sagemath and I've been trying to work through this problem on my own, but the bugs I've been running across make me think the way I'm writing my methods is wrong. Here's what I came up with using a different relation

var('x,a,b')

#the first functions in the recurrence relation. Making these more complicated will give you wrong outputs.
def lam_0(x):
    return a*x
def s_0(x):
    return b

#The lam_n based on the starting function lam_0. 
#Example: lam_2 would be lam_forward(lam_0, s_0, z, 2)
def lam_forward(lam_func, s_func, z, n = 1):
    if n <= 0:
        return lam_0(z)
    else:
        return lam_forward(lam_func, s_func, z + 1, n - 1) - lam_forward(lam_func, s_func, z, n - 1) + lam_forward(lam_func, s_func, z + 1, n - 1)*lam_0(z) + s_forward(lam_func, s_func, z+1, n-1)

#The s_n based on the starting function s_0.
def s_forward(lam_func, s_func, z, n = 1):
    if n <= 0:
        return s_0(z)
    else:
        return s_forward(lam_func, s_func, z+1, n-1) - s_forward(lam_func, s_func, z+1, n-1) + lam_forward(lam_func, s_func, z + 1, n - 1)*s_0(z)

#Print the n'th pair of functions
def printNthFunctions(n=0):
    print('Lam_', n, ' =', lam_forward(lam_0(x), s_0(x), x, n).full_simplify())
    print('S_', n, ' = ', s_forward(lam_0(x), s_0(x), x, n).full_simplify())

#BUG After n=1, this does not output the correct function, even though the individual functions are correct
def d_n(z, n):
    return  s_forward(lam_0(z), s_0(z), z, n)*lam_forward(lam_0(z), s_0(z), z, n-1) - s_forward(lam_0(z), s_0(z), z, n-1)*lam_forward(lam_0(z), s_0(z), z, n)
Preview: (hide)

1 Answer

Sort by » oldest newest most voted
2

answered 5 years ago

Juanjo gravatar image

If I am right, these are the recurrence relations you want to deal with: an(x)=an1(x+1)an1(x)+a0(x+1)an1(x)+bn1(x)bn(x)=bn1(x+1)bn1(x)+an1(x)b0(x+1) I have run the code below in a Jupyter notebook, assuming that a0(x)=Ax and b0(x)=B:

var("A,B,x")
def a(n,x):
    if n==0:
        return A*x
    else:
        return a(n-1,x+1) - a(n-1,x) + a(0,x+1)*a(n-1,x) + b(n-1,x)
def b(n,x):
    if n==0:
        return B
    else:
        return b(n-1,x+1) - b(n-1,x) + a(n-1,x)*b(0,x+1)

for n in range(5):
    show(html(fr"$a_{{{n}}}(x)={latex(a(n,x).full_simplify())}$"))
    show(html(fr"$b_{{{n}}}(x)={latex(b(n,x).full_simplify())}$"))
    show(html("<hr>"))

This is the output:

a0(x)=Ax
b0(x)=B


a1(x)=A2x2+A2x+A+B
b1(x)=ABx
a2(x)=A3x3+2A3x2+3A2+AB+(A3+3A2+2AB)x
b2(x)=A2Bx2+A2Bx+2AB+B2
a3(x)=A4x4+3A4x3+7A3+3(A4+2A3+A2B)x2+3A2+(A2+4A)B+B2+(A4+13A3+4A2B)x
b3(x)=A3Bx3+2A3Bx2+5A2B+AB2+(2AB2+(A3+5A2)B)x
a4(x)=A5x5+4A5x4+15A4+2(3A5+5A4+2A3B)x3+22A3+2AB2+(4A5+34A4+9A3B)x2+(A3+16A2)B+(A5+39A4+15A3+3AB2+3(2A3+5A2)B)x
b4(x)=A4Bx4+3A4Bx3+(A2+6A)B2+B3+3(A2B2+(A4+3A3)B)x2+(11A3+8A2)B+(4A2B2+(A4+20A3)B)x

Of course, you can increase the final value of n in the loop.

Preview: (hide)
link

Comments

Thank you so much! Sorry for my late response. This was exactly what I was looking for.

deadpan2297 gravatar imagedeadpan2297 ( 5 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 5 years ago

Seen: 243 times

Last updated: Mar 29 '20