How to evaluate recursive piecewise symbolic functions?
5 views (last 30 days)
Show older comments
Is it possible to define & evaluate recursive piecewise symbolic functions. As a simple example, here's an attempt at a recursive factorial symbolic function:
syms f(n)
syms fn(n)
f(n)=n
fn(n)=piecewise(n==1,1,n>1,f(n)*fn(n-1))
However, when calling it:
fn(4)
There's no recursion in the evaluation. Although f(n) is evaluated, fn(n-1) isn't:
ans=4 fn(3)
I know in this case I could do fn(n)=factorial(n) to get the correct answer, but this is just a simpler version of the recursive function I'm trying to code. Thanks.
0 Comments
Answers (0)
See Also
Categories
Find more on Assumptions in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!