What is Factorial MATLAB?
In mathematics, the factorial of is the product of all the positive integers less than or equal to ‘n’. It is applicable to only non-negative integers and is denoted by an exclamation mark (n!).
Table of Contents (click to navigate)
Factorial in MATLAB:
In mathematics, the factorial of is the product of all the positive integers less than or equal to ‘n’. It is applicable to only non-negative integers and is denoted by an exclamation mark (n!).
Main Concept:
We represent factorial as exclamation mark (!). It is a quantity interpret for all integers greater than or equal to zero. For an integer ‘n’ greater than or equal to 1, its factorial is equal to the product of all integers less than or equal to ‘n’ but greater than or equal to 1.
For example 5 factorial means:
6! = 1.2.3.4.5.6 = 720
Three ways to find factorial in MATLAB:
There are three ways to find factorial in MATLAB:
- Using for loops.
- Factorization method.
- Built-in function.
For loop method:
First enter the factorial to be calculated and the factorial that we want to calculate, must be greater than zero. For example:
N=7,
Initialize the product. In this case our product will be saved in variable f1.
F1=1,
Now go from 1 to your desired value and put it in ‘n’
for i = 1:n;
This command will start the iteration and will go from 1 to n digit. Just multiply all the integers in the interval by keeping the same values in the interval F1.
end;
Now end the iteration.
The result will be shown in F1 variable.
Factorization method:
In the second method we do not use for loop. It means we are not using iteration method. First we have to enter the factorial that we want to calculate and it must be greater than zero. For example:
N=7,
You can use built-in function ‘prod’ to multiply all the elements in a vector but you have to choose the elements carefully.
F2 = prod ([1:n]);
Result will be stored in f2 variable and it will show the product of all the numbers from 1 to n.
Built-in Function Method:
The best and the easiest way to find factorial is to use the built-in command for quick results. Why should we waste time on the above methods when we have a built-in command? MATLAB has a built-in function ‘factorial’. Let’s use it:
F3 = factorial (n);
Results will be stored in f3 variables.
Conclusion:
In algebra and different mathematical analysis, factorial has a great importance. It is used in formulas of mathematics which has practical applications. As we have seen three methods to find factorial in MATLAB, the best one we suggest is by using the built-in function because it is short and time saving method.
References:
MATLAB Software