开发者

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...


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


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]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜