Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Implementing a recursive equation solver in maple

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 -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).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading