matlabstatespacelinearization

Is the MATLAB command lsim good for linearized state space models?


A state space model is on the form: dx = Ax + Bu y = Cx + Du

Nonlinear state space models which is linearized is on this form:

dΔx = AΔx + BΔu
Δy =  CΔx + DΔu

Where:

Δx = [x1 - x10; x2 - x20; x3 - x30; .... ; xn - xn0]
Δu = [u1 - u10; u2 - u20; u3 - u30; .... ; um - um0]

The x10, x20, x30, xn0, u10, u20, u30, um0 is constants/initial values for the linearization.

So! The question is about the MATLAB command "lsim":

lsim (sys, u, t, x0)

In this case, sys is the A, B, C, D matrices from the lineraized state space model. u is the insignal vector, t is the time vector. But x0....can i say that x0 is x10, x20, x30, .... ,xn0?

Can I also say that u = u - u0 , and u0 is , u10 u10, u20, u30,... , um0 ?

Example:

u = linspace(5, 5, 100); % insignal 5
t = linspace(0, 100, 100); % 100 seconds
u0 = [0.2; -1.2; -3];
u = u - u0; % 
x0 = [-2; 2; -1]
lsim (sys, u, t, x0)

This will results:

Δx = [x1 - 2; x2 + 2; x3 - 1]
Δu = [u1 + 0.2; u2 - 1.2; u3 - 3]

Am I right?


Solution

  • There is a readily available Simulink S-Function dsfunc.m which is the simulation of a State Space model, and which can be easily adapted for included linearized State Space matrices, changing at every time step.

    Just build a simulink file dsfunc.slx and run it with the sim command sim('dsfunc') command.

    The good part is the S-Function deals properly with the state, and you just adapt the State Space model each time, keeping the state from one iteration to another.

    Also you can do it equally easily with a single for loop. Linearizing constant are included as shown.

    function [t,u,x,y]=example2
    % Start
    dt=0.001;
    N=1000;
    n=2;
    r=1;
    t=(0:N-1)'*dt;
    u=rand(N,r);
    % First
    [A,B,C,D,x0,y0]=ABCD(t(1),zeros(n,1),u(1,:)');
    x(1,:)=x0';
    y(1,:)=(C*x(1,:)'+D*u(1,:)'+y0)';
    % Cycle
    for i=2:N
        [A,B,C,D,x0,y0]=ABCD(t(i),x(i-1,:)',u(i-1,:)');
        x(i,:)=(A*x(i-1,:)'+B*u(i-1,:)'+x0)';
        y(i,:)=(C*x(i,:)'+D*u(i,:)'+y0)';
    end
    plot(t,[u x y]);legend({'u' 'x1' 'x2' 'y'});
    
    
    function [A,B,C,D,x0,y0]=ABCD(t,x,u)
    A=[0.1 -0.9;0 0.1];
    B=0.5*ones(2,1);
    C=[1 1];
    x0=[0];y0=[0];
    

    enter image description here