0

I am trying to verify my RK4 code and have a state space model to solve the same system. I have a 14 state system with initial conditions, but the conditions change with time (each iteration). I am trying to formulate A,B,C,D matrices and use sys and lsim in order to compile the results for all of my states for the entire time span. I am trying to do it similar to this:

for t=1:1:5401

    y1b=whatever
    .
    .
    y14b = whatever

    y_0 = vector of ICs

    A = (will change with time)
    B = (1,14)  with mostly zeros and 3 ones
    C = ones(14,1)
    D = 0

    Q = eye(14)
    R = eye(1)

    k = lqr(A,B,C,D)

    A_bar = A - B*k

    sys = ss(A_bar,B,C,D)

    u = zeros(14,1)

    sto(t,14) = lsim(sys,u,t,y_0)

    then solve for new y1b-y14b from outside function

end

In other words I am trying to use sto(t,14) to store each iteration of lsim and end up with a matrix of all of my states for each time step from 1 to 5401. I keep getting this error message:

Error using DynamicSystem/lsim (line 85)
In time response commands, the time vector must be real, finite, and must contain
monotonically increasing and evenly spaced time samples.

and

Error using DynamicSystem/lsim (line 85)
When simulating the response to a specific input signal, the input data U must be a
matrix with as many rows as samples in the time vector T, and as many columns as
input channels.

Any helpful input is greatly appreciated. Thank you

Amro
  • 123,847
  • 25
  • 243
  • 454

1 Answers1

1

For lsim to work, t has to contain at least 2 points.

Also, the sizes of B and C are flipped. You have 1 input and 1 output so u should be length of t in lsim by 1.

Lastly, it looks like you try to put all initials conditions at once in lsim with y_0 where you just want the part relevant to this iteration.

s = [t-1 t];
u = [0; 0];
if t==1
    y0 = y_0;
else
    y0 = sto(t-1,1:14);
end
y = lsim(sys, u, s, y0);
sto(t,1:14) = y(end,:);

I'm not sure I understood correctly your question but I hope it helps.

Simon
  • 31,675
  • 9
  • 80
  • 92
  • Thank you for the help. I tried doing it as you said but it is telling me that the initial conditions has to be the same number as states which is 14. I tried stretching s from t-1 to t with 14 steps and made u size 14 as well but it is telling me that there is a subscript mismatch. Any ideas? – user3814258 Jul 14 '14 at 23:23
  • What's the size of `y_0`? It sounds like it's `14 x 1` in which case I don't understand what you are trying to do. – Simon Jul 15 '14 at 02:22
  • that is the correct size for y_0 since it is the initial conditions for 14 states. I am trying to solve x_dot = Ax + Bu and y=Cx +Du where the states are embedded in the formulation of the A matrix but also change in time. This means I have to loop lsim for each time step and resolve for the A matrix each time. In the end I want a matrix of all the state values for each time from 1-5401 where I am only concerned with whole number times – user3814258 Jul 15 '14 at 02:47