Why can't Matlab do the factorial of a non-integer number?
8 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
Davide Masiello
2022 年 2 月 6 日
編集済み: Davide Masiello
2022 年 2 月 6 日
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.
3 件のコメント
Dyuman Joshi
2024 年 1 月 23 日
There is no function in MATLAB, in-built or a part of the toolbox, that offers that functionality directly.
(Possibly because) There is no known explicit relation or definition which can be utilized to obtain that.
However, you can try this -
%value of which the inverse is to be calculated
y = gamma(2.5)
f = @(x, val) gamma(x) - val;
out = fzero(@(x) f(x, y), 2)
But note that the inverse gamma function is not one-on-one (reference - Graph of the function provided in its wikipedia article - https://en.wikipedia.org/wiki/Inverse_gamma_function), so multiple solutions exist for each input.
The value obtained as the output depends on the initial guess -
%Finding the inverse for the same, but with initial guess as 1
fzero(@(x) f(x, y), 1)
John D'Errico
2024 年 12 月 2 日
gaminv is NOT the inverse of the gamma function. gaminv is the inverse of the gamma CDF. It is provided with the stats toolbox. It does not compute the inverse of the gamma function. Yes, it may be a bit confusing due to the name.
As well, gammaincinv is not the tool you want either. That evaluates the inverse of the INCOMPLETE gamma integral. Again, not what you want. A different animal.
I'm sorry, but it seems to be not a terribly common need to compute the inverse of the complete gamma function, so effectively the inverse of the extension of the factorial function into the real line. It is not something I've ever had a need to do in practice, nor have I seen requests for it.
その他の回答 (4 件)
Walter Roberson
2024 年 12 月 2 日
編集済み: Walter Roberson
2024 年 12 月 10 日
MatLab 'factorial' is coded so to work with integers only.
More generally: factorial is only defined for non-negative integers.
factorial does not work for numbers with decimals because the very definition of factorial only applies to integers.
If factorial() were to "work" for numbers with decimals, it would have to be defined as either factorial(fix(X)) or factorial(floor(X)) or factorial(round(X))
0 件のコメント
DGM
2022 年 2 月 6 日
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])
gamma([2 2.5 3 3.2 3.6 4])
gamma([2 2.5 3 3.2 3.6 4]+1) % offset by 1
0 件のコメント
Fryderyk Kukowski
2024 年 12 月 2 日
As other answers have provided the explanation of the problem but no ready implementation, I'll fill in this gap.
Here (and the same is in the attachment) is the function that will do just what you want.
function y = realFactorial(x)
y = gamma(x + 1);
end
5 件のコメント
Fryderyk Kukowski
2024 年 12 月 6 日
Yes, you can do it but making it a function makes the code look cleaner and more readable. In your example I would add a comment "Factorial for real numbers" next to the call to gamma function. If I used a function realFactorial instead then it is pretty much self describing and no comment is needed. Also in the future I might forget that adding 1 to argument of gamma function makes it a factorial function for real numbers. realFactorial is easier to remember.
Dyuman Joshi
2024 年 12 月 10 日
"In your example I would add a comment "Factorial for real numbers" next to the call to gamma function."
That'd be redundant, because that is the mathematical definition of the Gamma function - https://en.wikipedia.org/wiki/Gamma_function.
I highlighted the description because it is technically incorrect.
参考
カテゴリ
Help Center および File Exchange で Gamma Functions についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!