November 25, 2024

Simulate in Simulink Ordinary Differential Equations – Step-by-Step Tutorial


In this tutorial, we explain how to simulate ordinary differential equations in Simulink. The YouTube tutorial accompanying this webpage is given below.

Example and Preparation Steps

We explain the simulation process by using a particular example.

Problem: Simulate ordinary differential equation

(1)   \begin{align*}k_{1}\dot{x}=-k_{2}x^{2}+u\end{align*}

where x is a dependent variable (that we want to solve for), k_{1} and k_{2} are real constants, and u is an external real input. In (1), the “dot” symbol above the dependent variable denotes the first derivative. To make the problem numerically tractable, we assume the following values k_{1}=2, k_{2}=2, and u=1.

The Simulink model is shown in the figure below.

Figure 1: Final Simulink block model of the ordinary differential equation (1).

The simulated solution x is shown in the figure below.

Figure 2: Simulated solution x(t).

In the sequel, we explain how to create this model.

Before we start with the Simulink modeling, we first need to transform our equation into the appropriate form. The transformation process consists of the following steps.

STEP 1: In this step, we want to express the first derivative of x. That is, on the left-hand side of the equation (1), we only need to have \dot{x}:

(2)   \begin{align*}\dot{x}=\frac{1}{k_{1}}\cdot \Big( -k_{2}x^{2}+u \Big)\end{align*}

STEP 2: In this step, we eliminate the first derivative by using the fact that the first derivative of x(t) is equivalent to the product of s and X(s) in the Laplace complex domain, where s is the complex variable. That is, \dot{x} \Leftrightarrow s X(s). Here, not to make the notation too complex, we will simply denote the Laplace transformation X(s) of the time domain variable x(t), by x. The product of s and x is interpreted as the derivative of x. Later on, we will interpret the product of 1/s and x as an integral of x. We obtain

(3)   \begin{align*}s\cdot x = \frac{1}{k_{1}}\cdot \Big( -k_{2}x^{2}+u \Big)\end{align*}

STEP 3: Similarly to STEP 1, we move the variable s from the left to the right-hand side of the last equation. That is, on the left-hand side of the equation we need only to have x:

(4)   \begin{align*} x =\frac{1}{s} \cdot \frac{1}{k_{1}}\cdot \Big( -k_{2}x^{2}+u \Big)\end{align*}

The term

(5)   \begin{align*}\frac{1}{s}\end{align*}

is an integrator. It is modeled as an integrator block in Simulink.

Now, we are ready to construct the Simulink block diagram.

Simulink Implementation

First, we create a new MATLAB script and define the variables

(6)   \begin{align*}k_{1}=2, k_{2}=2\end{align*}

We load these variables in the MATLAB workspace. The Simulink workspace is able to directly read these variables from the MATLAB workspace.

Next, we construct the expression in the brackets

(7)   \begin{align*} -k_{2}x^{2}+u \end{align*}

The Simulink block diagram is shown below.

Figure 3: Block diagram representation of \Big( -k_{2}x^{2}+u \Big).

Next, we include the constant 1/(k_{1}) and the integrator 1/s. The Block diagram is shown below

Figure 4: Block diagram including the constant 1/(k_{1}) and the integrator 1/s.

Finally, we add the feedback and the scope. This is shown in the figure below.

Figure 5: Final block diagram.