필터 지우기
필터 지우기

How to find the factorial of fractional numbers using matlab code?

조회 수: 24 (최근 30일)
I have an array s1. I want to evaluate the factorial of s1.
M=3;
m1 = 0:M;
s1 = m1./2
s1 = 1×4
0 0.5000 1.0000 1.5000
factorial(s1)
Error using factorial
N must be an array of real non-negative integers.

채택된 답변

John D'Errico
John D'Errico 2022년 7월 16일
You CANNOT compute the factorial of a fractional number. Factorials are defined only for integers.
HOWEVER...
It is true that
factorial(N) == gammma(N+1)
for integer N. You might think of the gamma function as an extension of the factorial function onto the real line. For example:
N = 0:10;
factorial(N)
ans = 1×11
1 1 2 6 24 120 720 5040 40320 362880 3628800
gamma(N + 1)
ans = 1×11
1 1 2 6 24 120 720 5040 40320 362880 3628800
And the gamma function is defined on the real line. So...
gamma(1.5)
ans = 0.8862
is thus what you might think of when you write (0.5)!.
fplot(@gamma,[.1,5])
In fact, the gamma function follows the smiple rules that work for factorial. Thus we would see that if
factorial(N) == N*factorial(N-1)
then we might hope it would be true that
gamma(N+1) = N*gamma(N)
For example, we can test that as:
format long g
[gamma(3.25)*3.25,gamma(4.25)]
ans = 1×2
8.28508514183522 8.28508514183522
So the gamma function follows a similar recursive rule like the factorial function. The only differnce is you don't have any easy way to start the recursion.
  댓글 수: 2
Rik
Rik 2022년 7월 18일
It would be nice if Matlab would already suggest using the gamma function in the error message.
Steven Lord
Steven Lord 2022년 7월 18일
This sounds like a reasonable suggestion. I've added it to the enhancement database.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by