How to evaluate recursive piecewise symbolic functions?

13 visualizaciones (últimos 30 días)
Jon
Jon el 10 de Oct. de 2019
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.

Respuestas (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by