How to solve (integrate) a differential equation in Xcos

Simulating dynamic systems means solving differential equations. The most efficient way to solve a differential equation is by integrating it numerically. Xcos comes with a variety of numerical solvers which can be used to integrate the differential equations describing a dynamic system.

In this article we are going to solve an ordinary differential equation (ODE) in two ways:

  • analytic (classic)
  • numeric (in Xcos)

The analytic method is taught in college and the main purpose is to understand how differential equations work. The main advantage of the analytic method is that it shows the link between differential equation, integral and function (solution).

The disadvantage is that the analytic methods can be applied only to a limited type of differential equations. In reality (physics) most of the differential equations describing a real dynamic system are high order and nonlinear, which are very difficult to solve with classic (analytic) methods.

The equation we are going to solve is:


with the initial condition y(0) = 0.

Analytic solution

The equation is a separable differential equation, which can be solved as:

\[ \begin{equation*} \begin{split}
\frac{dy}{dx} &= x^2-3\\
dy &= (x^2-3)dx\\
\int dy &= \int (x^2-3)dx\\
\int dy &= \int x^2 dx -3 \int dx\\
y + C_1 &= \frac{x^3}{3} + C_2 -3x+C_3\\
y &= \frac{x^3}{3}-3x+C
\end{split} \end{equation*} \]


\[C = C_2 + C_3 – C_1\]

The general solution of our differential equation is:

\[y(x) = \frac{x^3}{3}-3x+C\]

To find out the value of C, we need to replace x with 0 and solve for C:

\[ \begin{equation*} \begin{split}
\frac{0^3}{3}-3 \cdot 0 +C = 0\\
C = 0
\end{split} \end{equation*} \]

We now have the particular solution for our differential equation:

\[\bbox[#FFFF9D]{y(x) = \frac{x^3}{3}-3x}\]

Integration in Xcos

The Xcos model will contain two parts, both created in the same block diagram. The first part will be the integration of the differential equation and the second part will be the model of analytic solution found above. The purpose is to check if we get the same solution for both methods, which will give confidence in our results.

The results for both methods are going to be saved in the Scilab workspace. With a Scilab script we are going to plot the results, side by side, to check if there are any deviations or mismatches.

Ordinary Differential Equation (ODE) integrated in Xcos explained

Image: Ordinary Differential Equation (ODE) integrated in Xcos explained

To integrate a differential equation in Xcos is straight forward. First step is to write the differential equation in a form that has the differential on the left side of the equal sign and the rest of the equation on the right side, like this:

\[\frac{dy}{dx} = x^2-3\]

Second, we need to model the right side of the equation with Xcos blocks. Notice that x comes from a TIME_f source, which means that will increase linearly in time.

Third, the block diagram of the mathematical expression of the right side is feed to a integrator block. The only setting of the integrator block is the initial condition y(0) = 0. To set it up, double-click on the integrator block and enter the initial condition (in our case is 0).

What comes out from the integrator block is the solution of the differential equation, the value of y function of x. If we run the simulation for 5 s, we’ll get the value of y for the same time (5 s).

The analytic solution is also simulated with Xcos blocks, having the same input x. Both results, analytic and numerical integration, are fed to a multiplexation block and stored in Scilab workspace under the variable y_x. The block which stores the data in the Scilab workspace is time triggered, initialized at 0 s and with an update at 0.1 s. To change the settings double-click on the CLOCK_c block (red foreground).

In the image below you can see the same Xcos block diagram with added annotation for each signal. This way it will be easier for the reader to track the implementation of the equations in the block diagram.

Ordinary Differential Equation (ODE) integrated in Xcos

Image: Ordinary Differential Equation (ODE) integrated in Xcos

Before running the simulation, we need to setup the simulation parameters. Click on Simulation -> Setup to open the Set Parameters dialog. For this particular example we are going to simulate for 5 s. The integration method is Runge-Kutta 4(5). Even if we choose another numerical solver, the result should be the same.

Xcos simulation parameters setup

Image: Xcos simulation parameters setup

After the simulation is complete, in the Scilab workspace we’ll find the variable y_x, which contains the results of our simulation. y_x is a variable type structure, with the following content:

  • y_x.time – is a vector variable containing the simulation time, from 0 to 5 s with steps of 0.1 s
  • y_x.values – is a matrix variable containing both the integration result and the analytic function result

To plot the result we are going to use a post-processing Scilab script, containing the following instructions:

// Left plot
plot(y_x.time, y_x.values(:,1),'Linewidth',3)
title('$\text{Xcos integration of }y\prime=x^2-2$','fontsize',4)

// Right plot
plot(y_x.time, y_x.values(:,2),'Linewidth',3)
title('$\text{Analytic solution }y(x)=\frac{x^3}{3}-3 \cdot x$','fontsize',4)

By running the script we get the following plot:

Plot of Ordinary Differential Equation (ODE) integrated in Xcos

Image: Plot of Ordinary Differential Equation (ODE) integrated in Xcos

As you can see, both methods output the same result. This confirms that both the analytic solution and the numerical integration are correct.

To get a better understanding of the method, first change the initial condition of the equation (e.g. y(0) = 3), calculate the constant C, modify the block diagram and rerun the simulation. Analyse the results and check if they match.

If you have the differential equations for a particular dynamic system, you can create the block diagram in Xcos and solve it by numerical integration. You can also use this method to check the analytic solution of any differential equation.

For any questions, observations and queries regarding this article, use the comment form below.

Don’t forget to Like, Share and Subscribe!

Leave a Reply

Ad Blocker Detected

Dear user, Our website provides free and high quality content by displaying ads to our visitors. Please support us by disabling your Ad blocker for our site. Thank you!