How to evaluate recursive piecewise symbolic functions?
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (0)
Voir également
Catégories
En savoir plus sur Assumptions dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!