My Step Response plot from Matlab does not match the one from Simulink

13 ビュー (過去 30 日間)
Javier
Javier 2025 年 3 月 17 日
コメント済み: Paul 2025 年 3 月 18 日
Hello, i am new to matlab and simulink, my homework was to design a serial PID controller in simulink and tune it. After this I had to verify the output with a mathematical approach in matlab. There step input has a step time of 1 and a value of 2, there is also a process delay of .5 seconds.
The controller and the process functions and the simulink graph are the following:
While on Matlab i wrote this code expecting to get the same ouput:
s = zpk('s')
B=tf([1],[2.3 3.2 4.5])%Process Transfer Function
D=tf([1],[2.1 1] ) %Feedback Transfer Function
A= (2.8*(1 + .39/s + .75*s))
t= 0:0.01:50;
e = exp(-1.5*s) %Process delay plus step time
z=feedback(A*B*e, D)
z_setpoint = 2*z
step(z_setpoint, t)
The output I receive from matlab is the following:
The response from simulink peaks at 2.03, while the one from Matlab does at around 2.25. The settling times are also slightly different. Am I doing something wrong on Matlab when trying to replicate the same response?
I am sorry if any of this is confusing, as i mentioned before I am new to matlab and simulink. If any clarification is needed feel free to ask.
Thank you

採用された回答

Sam Chak
Sam Chak 2025 年 3 月 17 日
If you want the Simulink Step block to match the MATLAB step() command, please follow @Paul's guidance. Conversely, if you wish for the MATLAB step() command to match the Simulink Step block, you should use the lsim() command. Additionally, you can directly specify the time delay in the tf() command when defining the process transfer function.
My simulations of the PID-controlled systems also yield slightly different results; however, both exhibit the same transient response patterns. This discrepancy may arise from the interpretation of the algorithm in the Simulink Derivative block, which differs from how the non-causal ideal derivative is interpreted in MATLAB. To verify this, when only the PI Controller is used (without the derivative component), both MATLAB and Simulink appear to yield the same results.
Case 1: Ideal PID Controller
s = zpk('s');
% Process transfer function with output delay
Gp1 = tf(1, [2.3, 3.2, 4.5], 'OutputDelay', 1.5)
Gp1 = 1 exp(-1.5*s) * --------------------- 2.3 s^2 + 3.2 s + 4.5 Continuous-time transfer function.
Gp2 = tf(1, [2.3, 3.2, 4.5]) % without time delay
Gp2 = 1 --------------------- 2.3 s^2 + 3.2 s + 4.5 Continuous-time transfer function.
% Transfer function in feedback loop
H = tf(1, [2.1, 1]);
% Ideal PID controller
Gc = pid(2.8, 2.8*0.39, 2.8*0.75);
% Closed-loop transfer function
sys1 = feedback(Gc*Gp1, H);
sys2 = feedback(Gc*Gp2, H);
% Step response
t = 0:0.01:25;
u = zeros(length(t), 1);
u(t>=1) = 2;
figure
lsim(sys1, u, t), hold on
lsim(sys2, u, t), grid on, ylim([-0.3 2.7]), hold off
legend('Time-delayed system', 'Without time delay')
Simulink model (in MATLAB Online R2024b)
Case 2: PI Controller
% PI controller (without derivative component)
[Gc, info] = pidtune(Gp1, 'PIDF')
Gc = 1 Kp + Ki * --- s with Kp = 1.02, Ki = 1.44 Continuous-time PI controller in parallel form.
info = struct with fields:
Stable: 1 CrossoverFrequency: 0.3393 PhaseMargin: 60.0000
% Closed-loop transfer function
sys1 = feedback(Gc*Gp1, H);
sys2 = feedback(Gc*Gp2, H);
% Step response
t = 0:0.01:50;
u = zeros(length(t), 1);
u(t>=1) = 2;
figure
lsim(sys1, u, t), hold on
lsim(sys2, u, t), grid on, ylim([-0.4 3.5]), hold off
legend('Time-delayed system', 'Without time delay')
  1 件のコメント
Paul
Paul 2025 年 3 月 18 日
I agree that the Derivative block is causing a problem for this exercise. Generally speaking, the Derivative block is not recommended (I'm sure this forum has other posts on this topic), and that block will be particularly problematic when applied to a discontinuous signal as is the case in this problem. If the Derivative block is replaced with a high pass filter of the form s/(tau*s + 1) with tau small, or if the Step input is replaced with a continuous signal that approximates a step input, like clipped ramp with a steep slope, then a much better comparison is obtained between Simulink and Matlab.

サインインしてコメントする。

その他の回答 (1 件)

Paul
Paul 2025 年 3 月 17 日
Hi Javier,
You might want to investigate the step size control options in Simulink. The plot looks a bit jagged, which suggests the step size might be too large, which can (but does not neccesarily) impact the accuracy of the simulation.
I think more care is needed on the Matlab side to properly model the same delays in the Simulink model, one of which is applied inside the loop and the other applied outside the loop. The delay outside the loop applies at the application of the step command. You can either change the block parameter in the Step block to apply the step at t = 0 to match the Matlab step command, our you can use one of the alternative syntaxes of step to specify the time when the step input is applied to match the Simulink model.

カテゴリ

Help Center および File ExchangeClassical Control Design についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by