Dynamic System Integration and Plotting with C++

Posted by Jaime Arrocha on Sunday, June 20, 2021

TOC

Intro


This is a quick demonstration on how to solve a state representation with the Euler method, collecting the values, and then plotting them.

I didn’t found a solution that wasn’t easy out there so I thought on getting the easy to integrate plotting library and a good math-algebra library to work with.

This is based on some work I did before with simulation. There’s some potential on expanding this, which I’ll do once I have more time. The complete code is here

Requirements


The only pre-installed requirement is Matplotlib from Python. This program uses Matplotlib for C++.

Run:

  • Run make
  • Execture euler1

Results


Below is the code. As you can see is quite trivial. The transition function performs the Euler integration after computing the state representation for x_dot. This program can easily be expanded to get output to csv files, which I’ll do next. And from reading matrices from a config file once it gets more robust. I have worked on a more robust version which I’ll upload soon.

 1#include <stdio.h>
 2#include "matplotlibcpp.h"
 3#include "Eigen/Core"
 4
 5namespace plt = matplotlibcpp;
 6
 7using namespace Eigen;
 8
 9// Euler based integrator
10MatrixXf transition(MatrixXf A, MatrixXf x, MatrixXf B, double u, double delta_t)
11{
12    MatrixXf x_dot = A * x + B * u;
13    MatrixXf x_new = x + x_dot*delta_t;
14    return x_new;
15}
16
17int main()
18{
19    double t = 0.0;
20    double t_final = 10.0f;
21    double delta_t = 0.01f;
22    int n = t_final/delta_t;
23    double u = 1.0;
24
25    MatrixXf A(3,3);
26    A.setZero();
27    A(0,0) = 0;
28    A(0,1) = 0;
29    A(0,2) = -1;
30    A(1,0) = 1;
31    A(1,1) = 0;
32    A(1,2) = -3;
33    A(2,0) = 0;
34    A(2,1) = 1;
35    A(2,2) = -3;
36
37    MatrixXf B(3,1);
38    B.setZero();
39    B(0,0) = 1;
40    B(1,0) = -5;
41    B(2,0) = 1;
42
43    // Single output matrix. Can be more if needed.
44    MatrixXf y(1,1);
45    y.setZero();
46
47    // State matrix
48    MatrixXf x(3,1);
49    x.setZero();
50    
51    MatrixXf C(1,3);
52    C.setZero();
53    C(2) = 1;
54
55    // for plotting
56    std::vector<double> t_array(n), output_array(n);
57
58    for (int i = 0; i < n; i++) {
59        t_array.at(i) = t;
60        
61        // Select specifically the Y row you need.
62        output_array.at(i) = y(0,0);
63
64        x = transition(A, x, B, u, delta_t);
65        y = (C*x);
66
67        // printf("x_dot(0): %f, x_dot(1)=%f, x_dot(2)=%f\n", x_dot(0,0), x_dot(1,0), x_dot(2,0));
68        // printf("x(0): %f, x(1)=%f, x(2)=%f\n", x(0,0), x(1,0), x(2,0));
69
70        t = t + delta_t; 
71    }
72
73    plt::figure();
74    plt::plot(t_array, output_array);
75    plt::show();
76
77    return 0;
78}

You will see in the Makefile that I’m using Python2.7, to use Python3.X, just point the C++ matplotlib to the corresponding Python.h location and also make the changes in the Makefile. All this is already there commented out. It works with both, but on Python 3.X there’s a segmetation fault on closing the plot due to a integration with QT and Python.

Differential equation for the demo program. Obtained from Sung, Lee et al., (2009). Process Indentification and PID Control, Section 2.1.2.

euler1_diffeq

Plotted solution

euler1