Why can't Matlab do the factorial of a non-integer number?

17 visualizaciones (últimos 30 días)
HWIK
HWIK el 6 de Feb. de 2022
Respondida: DGM el 6 de Feb. de 2022
Hi,
I tried using the factorial function on a number with decimals and got the following error: N must be an array of real non-negative integers.
Is there an alternative so that it can calculate the factorial of a number with decimals like Excel does?
Thanks for your time

Respuesta aceptada

Davide Masiello
Davide Masiello el 6 de Feb. de 2022
Editada: Davide Masiello el 6 de Feb. de 2022
MatLab 'factorial' is coded so to work with integers only.
The generalization of a factorial is the Γ function, which is
and the relation with the factorial is
MatLab implements the Γ function. Therefore, to compute the factorial of 1.5 you can write
gamma(2.5)
Which yields 1.3293, the correct answer.

Más respuestas (2)

David Hill
David Hill el 6 de Feb. de 2022
Use the gamma function.
gamma(1.3);

DGM
DGM el 6 de Feb. de 2022
See the Gamma function
Or in MATLAB, gamma().
Note that if you're trying to replicate the behavior of factorial, the input is offset by 1:
factorial([2 3 4])
ans = 1×3
2 6 24
gamma([2 2.5 3 3.2 3.6 4])
ans = 1×6
1.0000 1.3293 2.0000 2.4240 3.7170 6.0000
gamma([2 2.5 3 3.2 3.6 4]+1) % offset by 1
ans = 1×6
2.0000 3.3234 6.0000 7.7567 13.3813 24.0000

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by