Main Content


Factorial of symbolic input



f = factorial(n) returns the factorial of n. If n is an array, factorial acts element-wise on n.


collapse all

Compute the factorial for a symbolic number.

f = factorial(sym(20))
f = 2432902008176640000sym('2432902008176640000')

Compute the factorial function for a symbolic expression. factorial returns exact symbolic output as the function call.

syms n
expr = n^2 + 1;
f = factorial(expr)
f = n2+1!factorial(n^2 + 1)

Calculate the factorial for a value of n = 3. Substitute the value of n by using subs.

fVal = subs(f,n,3)
fVal = 3628800sym(3628800)

Differentiate an expression containing the factorial function (n2+n+1)!

syms n
f = factorial(n^2 + n + 1)
f = n2+n+1!factorial(n^2 + n + 1)
df = diff(f)
df = n2+n+1!ψpsi(n2+n+2)2n+1factorial((n^2 + n + 1))*psi((n^2 + n + 2))*(2*n + 1)

The derivative of the factorial function is expressed in terms of the psi function.

Expand an expression containing the factorial function.

syms n
f = factorial(n^2 + n + 1);
f1 = expand(f)
f1 = n2+n!n2+n+1factorial((n^2 + n))*(n^2 + n + 1)

Compute the limit at infinity for an expression containing the factorial function.

syms n
f = factorial(n)/exp(n);
fLim = limit(f,n,Inf)
fLim = sym(inf)

Compute factorial for array input. factorial acts element-wise on array input.

A = sym([1 3; 4 5]);
f = factorial(A)
f = 

(1624120)[sym(1), sym(6); sym(24), sym(120)]

Input Arguments

collapse all

Input, specified as a number, vector, matrix, or array, or a symbolic number, variable, array, function, or expression.

More About

collapse all

Factorial Function

The factorial of a number n is defined as follows.


The factorial of 0 is 1.


  • Calling factorial for a number that is not a symbolic object invokes the MATLAB® factorial function.

See Also

| | |

Introduced in R2012a