这段MATLAB代码用于绘制一个**平滑比例控制器(smoothed proportional controller)**的操作示例图。以下是代码的详细解读:
代码功能
- 目的:展示比例控制器在不同过程变量(如温度)下的控制行为。
- 背景:基于论文 Katzin, D., Marcelis, L. F. M., & van Mourik, S. (2021) 中的研究,用于生成论文中的图4。
- 核心:通过绘制比例控制器的输出,展示其在不同温度下的控制行为。
代码解析
- 注释说明:
% PLOTEXAMPLEPROPCONTROL Plot an example of the operation of a smoothed proportional controller % Used to create Figure 4 in: % Katzin, D., Marcelis, L. F. M., & van Mourik, S. (2021). % Energy savings in greenhouses by transition from high-pressure sodium % to LED lighting. Applied Energy, 281, 116019. % https://doi.org/10.1016/j.apenergy.2020.116019
这些注释提供了代码的功能描述、背景信息(用于生成论文中的图4),以及相关论文的引用。
- 作者信息:
% David Katzin, Wageningen University % david.katzin@wur.nl % david.katzin1@gmail.com
这部分注释提供了作者的姓名、单位和联系方式。
- 定义变量:
x.tAir = 0:0.001:40; a.heatSetPoint = 18.5; p.tHeatBand = -1;
x.tAir
:过程变量(如温度)的范围,从 0 到 40,步长为 0.001。a.heatSetPoint
:加热设定点(set point),值为 18.5。p.tHeatBand
:比例控制器的带宽(bandwidth),值为 -1。
- 绘制比例控制器输出:
plot(x.tAir, proportionalControl(x.tAir, a.heatSetPoint, -p.tHeatBand, 0, 1));
- 调用
proportionalControl
函数计算比例控制器的输出,并绘制曲线。 proportionalControl
函数的输入参数:x.tAir
:过程变量(温度)。a.heatSetPoint
:设定点。-p.tHeatBand
:带宽的负值。0
和1
:控制器输出的最小值和最大值。
- 调用
- 设置图形属性:
grid axis([18 20 -0.1 1.1]) xticks(18:0.25:20) xticklabels({'','','x = setPoint','','','','x = setPoint+Pband','',''})
grid
:在图形中添加网格线。axis([18 20 -0.1 1.1])
:设置图形的横轴范围为 18 到 20,纵轴范围为 -0.1 到 1.1。xticks
和xticklabels
:设置横轴刻度和标签,突出显示设定点和带宽。
- 绘制设定点和带宽的参考线:
hold on plot([19.5 19.5], [-1 2], 'LineWidth',0.3,'LineStyle','--',... 'Color',[0.15 0.15 0.15]); plot([18.5 18.5], [-1 2], 'LineWidth',0.3,'LineStyle','--',... 'Color',[0.15 0.15 0.15]);
- 在设定点(18.5)和设定点加带宽(19.5)处绘制垂直虚线,用于标记关键点。
- 设置纵轴刻度和标签:
yticks(-0.2:0.2:1.2) yticklabels({'','No action', '0.2', '0.4', '0.6','0.8','Full action',''})
- 设置纵轴刻度和标签,表示控制器输出的不同状态(从“无动作”到“全动作”)。
- 绘制控制器输出的上下限参考线:
plot([10 30], [0 0], 'LineWidth',0.3,'LineStyle','--',... 'Color',[0.15 0.15 0.15]); plot([10 30], [1 1], 'LineWidth',0.3,'LineStyle','--',... 'Color',[0.15 0.15 0.15]);
- 在控制器输出的下限(0)和上限(1)处绘制水平虚线。
- 设置坐标轴标签:
xlabel('Process variable x','LineWidth',0.3); ylabel('Controller action','LineWidth',0.3);
- 设置横轴标签为“过程变量 x”。
- 设置纵轴标签为“控制器动作”。
比例控制器公式
假设 proportionalControl
函数的实现如下:
function output = proportionalControl(x, setPoint, band, minOutput, maxOutput) % 计算比例控制器的输出 output = max(minOutput, min(maxOutput, (x - setPoint) / band)); end
- 公式解释:
clamp
表示将输出限制在minOutput
和maxOutput
之间。- 当
x
接近setPoint
时,输出接近 0。 - 当
x
超出setPoint + band
时,输出达到最大值。
示例
假设:
- 设定点
setPoint = 18.5
。 - 带宽
band = -1
。 - 控制器输出范围
[0, 1]
。
- 当
x = 18.5
(设定点)时: - 当
x = 19.5
(设定点加带宽)时: - 当
x = 17.5
(设定点减带宽)时:
总结
这段代码通过绘制比例控制器的输出曲线,展示了其在不同过程变量(如温度)下的控制行为。适用于温室控制系统的分析和优化。