0

For a state space equation in which matrix A is dependent on variable t(time), how can I get the step or output response?

This is the code, which doesn't work:

A = [sin(t) 0;0 cos(t)];
B = [0.5; 0.0];
C = [1 0; 0 1];
G = ss(A,B,C,[]);
step(G,t)
x0 = [-1;0;2];
initial(G,x0)

Here are error message:

Error using horzcat Dimensions of matrices being concatenated are not consistent.

Error in Response (line 11) A = [sin(t) 0;0 cos(t)];

安维桢
  • 1
  • 4
  • How does your code not work? Error messages, unexpected behavior, crash, freeze, locusts etc.? – Haem May 23 '18 at 13:35
  • Error using horzcat Dimensions of matrices being concatenated are not consistent. Error in Response A = [sin(t) 0;0 cos(t)]; – 安维桢 May 23 '18 at 13:47
  • what is `t` ? the problem lies there probably – Ander Biguri May 23 '18 at 14:54
  • t is time, which is to say matrix A changes with time. – 安维桢 May 23 '18 at 15:03
  • 1
    The `ss` function generates an `LTI` system - that is, linear, time-**in**variant systems. The `Control Toolbox` cannot be used to create or simulate time varying systems. You should use `Simulink`, which is designed to model non-linear, time varying systems. – Phil Goddard May 23 '18 at 16:12
  • You could do this with [ode45](https://www.mathworks.com/help/matlab/ref/ode45.html). You'd need to use the technique shown in the section `ODE With Time Dependent Terms` to incorporate the input `u` to your system. And the result would be your state-vector, which you would then have to multiple by your `C` matrix to get your required output. But as per my previous comment, `Simulink` would be much easier. – Phil Goddard May 23 '18 at 17:33

1 Answers1

1

As pointed already, you can only use the ss function to generate LTI systems, but you can discretise your model analytically using methods like forward Euler, backwards Euler, Tustin etc. and simulate your model using a for loop.

For your example, you could run something like this:

  • consider a sampling period h = 0.01 (or lower if the dynamics are not captured properly);
  • select N as the number of steps for the simulation (100, 1000 etc.);
  • declare the time instants vector t = (0:N-1)*h;
  • create a for loop which computes the system states and outputs, here using the forward Euler method (see https://en.wikipedia.org/wiki/Euler_method):

    % A = [sin(t) 0;0 cos(t)]; 
    B = [0.5; 0.0];
    C = [1 0; 0 1];
    D = [0; 0];
    x0 = [-1;1];  % the initial state must have two elements as this is a second-order system
    u = 0;  % constant zero input, but can be modified
    N = 1000;
    h = 0.01;
    t = (0:N-1)*h;
    x_vec = [];
    y_vec = [];
    xk = x0;
    yk = [0;0];
    for k=1:N
        Ad = eye(2)+h*[sin(t(k)) 0; 0 cos(t(k))];
        Bd = h*B; % C and D remain the same
        yk = C*xk + D*u;
        xk = Ad*xk + Bd*u;
        x_vec = [x_vec, xk]; % keep results in memory
        y_vec = [y_vec, yk];
    end
    
    % Plot output response
    plot(t,y_vec);