% Define range of PID gains to explore
Kp_range = 0:0.1:5;
Ki_range = 0:0.1:1;
Kd_range = 0:0.1:0.5;
% Define setpoint temperature and other constants
setpoint = 25.0; % Setpoint temperature in degrees Celsius
temperature_range = 0:0.1:50; % Range of temperature values
% Initialize arrays to store performance metrics
overshoot = zeros(length(Kp_range), length(Ki_range), length(Kd_range));
settling_time = zeros(length(Kp_range), length(Ki_range), length(Kd_range));
% Simulate PID control system for each combination of gains
for i = 1:length(Kp_range)
for j = 1:length(Ki_range)
for k = 1:length(Kd_range)
% Calculate control output using PID algorithm
output = Kp_range(i) * (setpoint - temperature_range) + ...
Ki_range(j) * trapz(temperature_range, setpoint - temperature_range) + ...
Kd_range(k) * gradient(setpoint - temperature_range);
% Calculate overshoot and settling time
[overshoot(i,j,k), settling_time(i,j,k)] = calculate_performance(output, setpoint);
end
end
end
% Reshape overshoot and settling time arrays for plotting
overshoot_2d = squeeze(max(overshoot, [], 3));
settling_time_2d = squeeze(min(settling_time, [], 3));
% Plot overshoot and settling time as a function of Kp and Ki
figure;
subplot(2,1,1);
surf(Kp_range, Ki_range, overshoot_2d.');
xlabel('Proportional Gain (Kp)');
ylabel('Integral Gain (Ki)');
zlabel('Overshoot (%)');
title('Overshoot vs. PID Gains');
subplot(2,1,2);
surf(Kp_range, Ki_range, settling_time_2d.');
xlabel('Proportional Gain (Kp)');
ylabel('Integral Gain (Ki)');
zlabel('Settling Time (seconds)');
title('Settling Time vs. PID Gains');
% Function to calculate overshoot and settling time
function [os, ts] = calculate_performance(output, setpoint)
os = max(output) - setpoint;
ts = 0;
for t = 2:length(output)
if abs(output(t) - setpoint) < 0.02 * setpoint && ts == 0
ts = t - 1;
end
end
end