简体   繁体   中英

MATLAB function LSIM in C++

I'm developing a simulation software in Qt (C++) which has to take an input (u) for a SISO dynamic system defined by matrices A, B, C, D and get the output (y) based on a initial state (x0):

dx = Ax + Bu
y  = Cx + Du

That kind of stuff can be done by using Matlab's function LSIM... I know that I can define the differential equations and solve them numerically, however defining them and solve them in a proper order is a little bit tricky, so, I'm wondering if anyone knows if there is an existing C++ library or C++ example which can do that...

Edit from here...

I'm developing my Qt application as a standalone app, without Matlab... So I thought that there is a way in which I have something like this if I have a 2-state system for one state of the system:

double y(double A[2][2], double B[2], double C[2], double D, double XAct[2], double XNext[2], double u)

But nevermind, I found that I can do something like this:

X[k+1] = A*X[k] + B*u[k]
y[k] = C*X[k] + D*u[k]

However I'm worried about timestamps so... I don't know if this is correctly for systems with an array as an input... I suppose that yes but...

You can use that equation set in C(++) or whichever language however, A,B,C,D must be from the discrete state-space model: Ad, Bd, Cd, Dd.

X[k+1] = A*X[k] + B*u[k]
y[k] = C*X[k] + D*u[k]

Sampling time (Ts) of the input data will be used to discretize the continuous state space model. MATLAB has the c2d function to do that.

sys = ss(A,B,C,D);
sysd = c2d(sys,Ts);
Ad = sysd.A;
Bd = sysd.B;
Cd = sysd.C;
Cd = sysd.D;

Once you have Ad, Bd, Cd, Dd, you can use them in your C(++) code with the following equations:

X[k+1] = Ad*X[k] + Bd*u[k]
y[k] = Cd*X[k] + Dd*u[k]

have a look at the Gnu Scientific Library (GSL) which can solve the diff ex for you. I had a similar problem and that's what I used

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM