📄 pid.m
字号:
% ***************************************************************************\
% PID Function
%
% This program has been written by the Technical Support Staff at Z-World in
% response to several customer requests. As such, it has NOT had the testing and
% validation procedures which our "standard" software products have. It is being
% made available as a sample. There is no warranty, implied or otherwise.
%
% The PID (Proportional Integral Derivative) function is used in mainly
% control applications. PIDCalc performs one iteration of the PID
% algorithm.
%
% While the PID function works, main is just a dummy program showing
% a typical usage.
%***************************************************************************/
% function ISE = PID(Proportion,Integral,Derivative)
clc;
clear;
Proportion = 4.8269;%2.591; % Proportional Const
Integral = 4.0806;%1.4079; % Integral Const
Derivative = 3.899;%1.1921; % Derivative Const
dt = 0.01; % 每一次迭代时间
Epock = 2000; % 所用时间20秒
PlantOut(1) = 0; % 初始输出 0
for i =1:Epock
% 参考输入
input(i) = 1;
% 传感器输出
if i==1
output(1) = 0;
else
output(i) = PlantOut(i);
end
% 计算偏差
Error(i) = input(i) - output(i);
% 指标计算误差累积
CumulateError(i) = Error(i)^2*dt;
% PID计算 用面积法近似积分 用变化量近似微分
ErrorArea(i) = Error(i)*dt;
if i == 1
dError = Error(i);
else
dError = Error(i) - Error(i-1);
end
PIDout(i) = Proportion * Error(i) + Integral * sum(ErrorArea) + Derivative * dError;
% 装置输出计算
gs(i) = 2/5*exp(-1/5*i*dt)*dt;
PlantOut(i+1) = PIDout*fliplr(gs)';
end
ISE = sum(CumulateError);
% 输出响应曲线
plot((0:dt:20),PlantOut);
hold on
plot((0:dt:20),1,':')
xlabel('t/s')
axis([0 20 0 1.4])
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -