对之前的版本进行修改:温度是否高于设定温度都可以自行调节。
% 参数定义
setpoint = 60; % 目标温度
initial_temp = 75; % 初始温度
dt = 1; % 时间步长(秒)
t_end = 300; % 总时间(秒)
time = 0:dt:t_end; % 时间向量
n = length(time); % 时间步数
% 模型参数
A = 0.001; % 系统的动态模型参数
B = 0.1; % 输入增益
K = 1; % 参考模型增益
adaptive_gain = 0.1; % 自适应增益
% 初始状态
current_temp = initial_temp;
adaptive_param = 1; % 初始自适应参数
% 存储温度和控制信号
temp = zeros(1, n);
control_signal = zeros(1, n);
for i = 1:n
% 计算误差
error = setpoint - current_temp;
% 参考模型输出
ref_model_output = K * error;
% 控制信号计算
control_signal(i) = adaptive_param * ref_model_output;
if current_temp>setpoint
% 系统状态更新
current_temp = current_temp - (B * control_signal(i) + A * current_temp) * dt;
% 当条件为真时执行的代码
else
% 系统状态更新
current_temp = current_temp + (B * control_signal(i) + A * current_temp) * dt;
% 当条件为假时执行的代码
end
% 系统状态更新
% current_temp = current_temp - (B * control_signal(i) + A * current_temp) * dt;
% 存储温度
temp(i) = current_temp;
% 自适应参数更新
% 这里使用简化的自适应算法,实际应用中可能需要更复杂的算法
adaptive_param = adaptive_param + adaptive_gain * (error - (B * control_signal(i)));
end
% 绘制结果图
figure;
subplot(2,1,1);
plot(time, temp, 'b-', 'LineWidth', 2);
hold on;
plot(time, setpoint * ones(1, n), 'r--', 'LineWidth', 2);
xlabel('Time (s)');
ylabel('Temperature (°C)');
title('Temperature with Adaptive Control');
legend('Temperature', 'Setpoint');
grid on;
subplot(2,1,2);
plot(time, control_signal, 'g-', 'LineWidth', 2);
xlabel('Time (s)');
ylabel('Control Signal');
title('Control Signal Over Time');
grid on;
初始温度25℃:
初始温度75℃: