# Difference between matlab ss function and Simulink State-Space block

5 views (last 30 days)
Raymond Wong on 5 Jul 2022
Commented: Paul on 7 Jul 2022
I want to do induction motor state space model simulation, so I built it with ss function and set the zero initial conditions, but after I fed it with input by lsim function, the output diverged. When I built it with simulink state-space block, fed the same input, and set the same initial conditions, it converged and got pretty good result, which made me think what's the difference between these two state space modeling methods. What aspects should I pay attention if I want to make ss in matlab converge?
Update: I attached code in matlab and SImulink block for better understanding
The simulink block configurations are the same in matlab
MATLAB code:
%% Basic Setting
f = 10000; % Sampling Frequency
Ts = 1/f;
%% Data Preparation(Not Important
out1 = out;
ab0_voltage1 = out1.voltage_ab0;
ab0_current1 = out1.current_ab0;
rotation_f1 = out1.speed;
start_time = 0;
sampling_time = 5;
start_point = start_time*f+1;
sampling_points = sampling_time*f-1;
t_ind = start_point:start_point+sampling_points;
u = ab0_voltage1(t_ind,2:3); %Input
i = ab0_current1(t_ind,2:3);
flux_init = out.flux_ab(start_point,2:3);
w = mean(out.w_e(end,2));
%% Parameterization of State Space A B C D
x = [6.14,0.037874419,0.387125581,4.987617956];
Rs = x(1);
lls_dot = x(2);
Lm_dot = x(3);
Rr_dot = x(4);
%% State Space modeling
A = [-Rr_dot/Lm_dot, -w, Rr_dot,0;
w, -Rr_dot/Lm_dot, 0,Rr_dot;
Rr_dot/(Lm_dot*lls_dot), w/lls_dot, -(Rr_dot+Rs)/lls_dot, 0;
-w/lls_dot, Rr_dot/(Lm_dot*lls_dot), 0, -(Rr_dot+Rs)/lls_dot];
B = [0, 0;
0, 0;
1/lls_dot, 0;
0, 1/lls_dot];
C = [zeros(2),eye(2)];
D =zeros(2);
sys = ss(A,B,C,D,Ts)
%% Response
t = 0:Ts:sampling_time-Ts;
x0 = [flux_init,i(1,1:2)]; % initial conditions setting
y = lsim(sys,u,t,x0)
Raymond Wong on 6 Jul 2022
@Sam Chak I forgot to upload the data file named by 'out.mat'. Sorry about that. See attached.

Fangjun Jiang on 5 Jul 2022
Edited: Fangjun Jiang on 5 Jul 2022
There are limited number of settings when calling lsim(sys,u,t,x0,method). You need to change the settings in Simulink to match the MATLAB simulation time vector. Most likely, choose discrete solver with fixed step size in Simulink. This is just to make the two simulation results match.
If the outputs in Simulink converge using the default settings, then the system is stable. Most likely, the step size in MATLAB is too large which caused it to diverge.
Raymond Wong on 6 Jul 2022
The solver setting in simulink is ode8 with fixed-step, and the fixed step size is 1e-4 which is the Ts in matlab ss function.

Sam Chak on 6 Jul 2022
I have added one line after sys = ss(A,B,C,D,Ts) to determine whether system is stable or not.
isstable(sys)
isstable(sys) returns a logical value of 1 (true) if the dynamic system model sys has stable dynamics, and a logical value of 0 (false) otherwise.
Raymond Wong on 6 Jul 2022
Thank you. I found the issue. Check out my answer as you like.

Raymond Wong on 6 Jul 2022
Edited: Raymond Wong on 6 Jul 2022
I found the issue. It turned out the model I built in MATLAB at first is the Discrete-time state-space model, which I didn't pay attention to. After I made it Continuous-time state-space model, it converged. In the code, the chage is shown below. @Sam Chak Technically they're the same model, but I'm not sure why the continuos version converge and the discrete version diverge.
% sys = ss(A,B,C,D,Ts) Discrete-time state-space model
sys = ss(A,B,C,D) % Continuous-time state-space model
Paul on 7 Jul 2022
You've got it. Use c2d() to develop the discrete-time approximation to a continous-time model. Check the c2d doc page for the available options for the conversion. Good luck with rest of your project.

### Categories

Find more on General Applications in Help Center and File Exchange

R2020b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!