Implementing a recursive equation solver in maple

Advertisements

I want to implement a procedure for solving linear recurrence relations similar to rsolve. For my method, I need to substitute functions into the recursive relation and evaluate them at certain points. However, my code does not seem to work.
When I write:

rec := (a, n) -> a(n) - n*a(n - 1);
rec(n -> n^2, 2);

It correctly returns 2. However, when I write

repertoire:=proc(func_guess,recursive_relation)
print(recursive_relation(n -> func_guess[1], 3));
end proc:
repertoire([3^n,n!],rec);

It prints

 -2*3^n 

instead of

  (3^3-3*3^2=)0 

I also tried the command "subs", however,

subs(a(n)=n^2,a(n)=n*a(n-1))

returns n^2=n*a(n-1). I don’t care whether the recurrence is passed as a function or as an equation, I can’t seem to make either work. I’m quite new to Maple, any help is welcome.

PS: A perfect solution would allow func_guess to contain recursively defined costume functions.

>Solution :

Change

n -> func_guess[1]

to

unapply(func_guess[1], n)

As you had it, the procedure parameter n was unrelated to the global name n in the expression 3^n that was passed.

There are other ways to do this common kind of task. But unapply is easy (which is why it exists).

Leave a ReplyCancel reply