Difference between matlab ss function and Simulink State-Space block
Show older comments
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
Simulink Block and Solver setting:
The simulink block configurations are the same in matlab

Simulink solver setting:

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)
7 Comments
Fangjun Jiang
on 5 Jul 2022
Solver settings
Raymond Wong
on 5 Jul 2022
Sam Chak
on 5 Jul 2022
Is the system inherently stable?
Could it be the input excites one of the virbration mode?
Raymond Wong
on 6 Jul 2022
Sam Chak
on 6 Jul 2022
@Raymond Wong, Thanks for the clarification. This is indeed a little strange. In thoery, if the system is inherently stable, the output response should not diverge no matter what input is fed into the system, so long as the input is bounded and does not excite the vibration mode of the system.
Sam Chak
on 6 Jul 2022
Got this error mesage:
error: 'out' undefined
Raymond Wong
on 6 Jul 2022
Accepted Answer
More Answers (2)
Fangjun Jiang
on 5 Jul 2022
Edited: Fangjun Jiang
on 5 Jul 2022
1 vote
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.
1 Comment
Raymond Wong
on 6 Jul 2022
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.
1 Comment
Raymond Wong
on 6 Jul 2022
Categories
Find more on State-Space Control Design in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!