NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

%% 实验2 闭环控制:PID控制算法(P、PI、PID对比),图中包含稳定值
clear; clc; close all;

%% 参数设置
Ts = 0.001; % 采样时间(s)
T_end = 0.2; % 仿真时间(s)
t = 0:Ts:T_end;
N = length(t);

% 被控对象: G(s)=1/(0.02s+1) 离散化 (一阶保持器)
T_obj = 0.02;
a = exp(-Ts/T_obj); % y(k)=a*y(k-1)+b*u(k-1)
b = 1 - a;

% 设定值: 阶跃信号 (0.02s后从0跳变到1)
r = zeros(1,N);
r(round(0.02/Ts):end) = 1;
steady_value = 1; % 稳定值

%% PID参数
% 组1: 纯比例 P
Kp1 = 1; Ki1 = 0; Kd1 = 0;
% 组2: 比例积分 PI
Kp2 = 1; Ki2 = 20; Kd2 = 0;
% 组3: 比例积分微分 PID
Kp3 = 1; Ki3 = 20; Kd3 = 0.002;

%% 存储变量
y1 = zeros(1,N); u1 = zeros(1,N);
y2 = zeros(1,N); u2 = zeros(1,N);
y3 = zeros(1,N); u3 = zeros(1,N);

%% 递推变量初始化 (各组独立)
% 组1
integ1 = 0; e_prev1 = 0; y_prev1 = 0;
% 组2
integ2 = 0; e_prev2 = 0; y_prev2 = 0;
% 组3
integ3 = 0; e_prev3 = 0; y_prev3 = 0;

%% 主循环 (位置式PID)
for k = 1:N
% ----- 组1: P控制 -----
e1 = r(k) - y_prev1;
integ1 = integ1 + e1 * Ts;
der1 = (e1 - e_prev1) / Ts;
u1(k) = Kp1 * e1 + Ki1 * integ1 + Kd1 * der1;
y1(k) = a * y_prev1 + b * u1(k);
e_prev1 = e1;
y_prev1 = y1(k);

% ----- 组2: PI控制 -----
e2 = r(k) - y_prev2;
integ2 = integ2 + e2 * Ts;
der2 = (e2 - e_prev2) / Ts;
u2(k) = Kp2 * e2 + Ki2 * integ2 + Kd2 * der2;
y2(k) = a * y_prev2 + b * u2(k);
e_prev2 = e2;
y_prev2 = y2(k);

% ----- 组3: PID控制 -----
e3 = r(k) - y_prev3;
integ3 = integ3 + e3 * Ts;
der3 = (e3 - e_prev3) / Ts;
u3(k) = Kp3 * e3 + Ki3 * integ3 + Kd3 * der3;
y3(k) = a * y_prev3 + b * u3(k);
e_prev3 = e3;
y_prev3 = y3(k);
end

%% 绘图: 被控量响应,包含稳定值水平线
figure('Name','闭环控制响应');
plot(t, r, 'k--', 'LineWidth',1.2); hold on;
plot(t, y1, 'b-', 'LineWidth',1);
plot(t, y2, 'g-', 'LineWidth',1);
plot(t, y3, 'r-', 'LineWidth',1);
% 添加稳定值水平线
yline(steady_value, 'k:', 'LineWidth',1.2);
xlabel('时间 (s)'); ylabel('输出 y(t)');
title('不同PID参数下的闭环阶跃响应(稳定值 = 1)');
legend('给定值 r(t)','P (Kp=1)','PI (Kp=1, Ki=20)','PID (Kp=1, Ki=20, Kd=0.002)','稳定值','Location','best');
grid on;

%% 绘图: 控制器输出
figure('Name','控制器输出');
plot(t, u1, 'b-', t, u2, 'g-', t, u3, 'r-', 'LineWidth',1);
xlabel('时间 (s)'); ylabel('控制量 u(t)');
title('控制器输出信号');
legend('P','PI','PID');
grid on;

disp('闭环仿真完成,图中已标注稳定值。');
     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.