# Matlab: System simulation with dynamic state matrix / input matrix [closed]

I have the following system: $$\dot{x} = A(t)x+B(t)u$$ $$y = x$$

$A(t)$ and $B(t)$ are actually scalar, but time-dependent. If they would be constant, I could simulate the system in Matlab using:

sys = ss(A,B,C,0); lsim(sys,u,t,x0);

However, it would be nice to simulate the system with dynamic state and input matrix. The matrices are based on measurement data, this means I would have for each discrete time step $t_i$ another matrix $A(t_i)$. Any suggestions how to do that?

## closed as off-topic by Chuck♦, Mark Booth♦Jun 15 '15 at 11:28

If this question can be reworded to fit the rules in the help center, please edit the question.

• I'm voting to close this question as off-topic because it belongs on Stack Overflow (yes, despite the fact that I answered it). – Chuck Jun 12 '15 at 15:09
• What is wrong with using an ode solver such as ode45 and interpolating A and B? – fibonatic Jun 12 '15 at 15:52
• @Chuck I do not think this question would really be suited for stackoverflow, the computation science stackexchange would be a better choice. – fibonatic Jun 12 '15 at 15:55
• @fibonatic, you might be correct, but I've always gotten Matlab answers from Stack Overflow. To this point, consider there are 321 questions tagged with "matlab" on the computation science board, but 48,752 on the Stack Overflow board. – Chuck Jun 12 '15 at 17:44
• @Chuck But stackoverflow is also an older/bigger stackexchange. – fibonatic Jun 12 '15 at 19:20

When you say the matrices are based on measurement data, do you mean that $A(t)$ varies with your state space output $y$, which should be configured to provide your outputs (measurements), or that you want to actually provide a hard-coded series of parameters (field measurements) to the $A$ matrix?

If you are trying to do the former, have essentially $A=f(y)$, where $y=f(t)$ (because actually $y=f(x)$, but $x=f(t)$ by definition), then you can use ode45. In the function you want to use you can define y = x, then use that to update the parameters in $A$.

If you want the latter, or the former really, you can always brute force it. Setting up some standards real quick, let's say you have:

nSamples, which would be the number of time points in your simulation.

Your time step is dT.

You have nStates, so $size(A) = [nStates,nStates]$. Now assume that your A matrix evolves over time based of field samples (testing, etc.), you should have a bunch of $A$ matrices, such that you actually have $size(A) = [nStates,nStates,nSamples]$.

Now, try this:

x = Initial Conditions;

for i = 1:nSamples

currentA = A[: , : , i];

xDot = currentA * x + B * u;

x = x + xDot * dT;

y = x;

end

As before, if you just want to swap out parameters in $A$ based on $y$ then that's easy to implement too.

x = Initial Conditions;

y = f(x);

for i = 1:nSamples

A = f(y);

xDot = Ax + Bu;

x = x + xDot*dT;

y = f(x);

end

Again, this is just a brute-force method to implement what lsim does for you. It's just numeric integration.

Hope this helps!