0
votes

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

1

1 Answers

1
votes

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.