Simulink Tutorial Series – 6

Simulink Tutorial Series - 6; Calculate factorial in SImulink

In this article, I am going to demonstrate a practical example of for loop in Simulink.

I will build the Simulink model step-by-step.

Matlab/Simulink is the leading software for model based development in aerospace, automotive industry etc.

There are several other online resources, where you can get theoretical knowledge about Matlab/Simulink. In fact, Matlab has very good documentation for each of their products. But, there are not many resources to explain the theory along with good working examples. This is going to be a series of articles. This is the 6th article in this series.

In every article in this Simulink Tutorial Series, I will add real-life working examples and show how to build models for a particular problem.

Problem Statement

Here is the problem statement –

Calculate the factorial of a given number (N) in Simulink.

Assumption

I am assuming, you have already gone through my previous articles –

Simulink Tutorial Series – 1
Simulink Tutorial Series – 2
Simulink Tutorial Series – 3
Simulink Tutorial Series – 4
Simulink Tutorial Series – 5

One of these articles, I have explained how to implement for loop in Simulink.

What is Factorial?

I am getting into the formal mathematical definition of factorial. Let me give you a couple of example for your understanding:

Factorial of 5 is = 5! = 5 x 4 x 3 x 2 x 1 = 120
Factorial of 6 is = 6! = 6 x 5 x 4 x 3 x 2 x 1 = 720

So, we can establish the factorial formula as:

Factorial of n is = n! = n x (n-1) x (n-2) x (n-3) x (n-4) x …….. x 4 x 3 x 3 x 2 x 1


Calculate Factorial using Simulink

I am going to solve this problem with the help of for loop library block available in Simulink Library.

Step-1

At first, let us find out the blocks that are required to solve the problem –

  1. For loop block required to iterate
  2. A delay block for feeding the previous output
  3. A display block to show the output
  4. Constants block for providing the input

Now, I will open the Simulink Library Browser and drag and drop all the required Simulink library block to solve the problem.

Simulink Tutorial Series - 6; Step-1

Step-2

Now, I will jump into the For iterator block and configure the for loop. Then, I will place the additional required blocks and join them up.

The For loop here is configured to execute from 1 to N. The input for the for loop is connected to the input port 1.

Every time the for loop runs, the output would be feed to the multiplication block. The multiplication block calculates as (Output of For Iterator x Previous Output of the multiplication block).

One thing, you have to be careful here is, you need to set the

Simulink Tutorial Series - 6; Step-2
For Iterator setting

Step-3

Now, I need to configure the delay block to ensure that the initial value for the delay block is 1. Please note that I have chosen the “Initial Condition” as Input port (x0).

Delay Block setting

Step-4

Now, our final solution model is ready.

Simulink Tutorial Series - 6; Step-4

Simulink Model Output

Now, let us execute the model with a input value 5.

Simulink Tutorial Series - 6; Model Output

Now, I will change the input as 6 and then execute the model. Here, the input is 6. So, factorial of 6 is = 6 x 5 x 4 x 3 x 2 x 1 = 720.
As you can see below, the output of the model is displayed as 720, as we expected.

Simulink Tutorial Series - 6; Step-1; Model Output-2

There you go! Our model output is exactly what we expected.

Download Sample Model

You can download the Sample Model by Clicking HERE.

Summary

Matlab/Simulink is a well known and very popular tool used for Model Based Software Development in the aerospace and automotive industry.

Today, in this article (Simulink Tutorial Series – 6), I have explained step-by-step, how to calculate the factorial of a given number in the Simulink model using Simulink library blocks from scratch.

If you have any questions, please feel free to comment in the comment box below. 👇👇👇

I will keep sharing useful real-life Simulink model example here – Simulink Tutorial Series.

Happy learning!

Simulink Tutorial Series – 6
Scroll to top
error: Content is protected !!