Hi @ mohammed hussein,
To implement a PID controller in MATLAB for your specific scenario, we will follow these steps:
Define the System Parameters: start by defining the practical data, the variable d, and the desired output Vdesire.
Calculate the Output: The output will be calculated based on the provided data and the variable d.
Implement the PID Controller: set up the PID controller to minimize the error between the desired output and the actual output.
Simulate the Control Process: run a simulation to observe how the PID controller adjusts the output over time.
Display the Results: Finally, visualize the results to understand the performance of the PID controller.
Here is the complete MATLAB code that accomplishes the above tasks:
% Step 1: Define the System Parameters
A = [0 3 12.5 12.8 11.8 12.3 11.9 12]; % Practical data
d = 1; % Variable
OUT = A * d; % Output
Vdesire = 12; % Desired output
% Step 2: Initialize PID Controller Parameters
Kp = 1; % Proportional gain
Ki = 0.1; % Integral gain
Kd = 0.5; % Derivative gain
% Step 3: Initialize Variables for PID Control
error = zeros(size(OUT)); % Error vector
integral = 0; % Integral term
previous_error = 0; % Previous error for derivative term
dt = 1; % Time step (assuming uniform time intervals)
% Step 4: Control Loop
for i = 1:length(OUT)
% Calculate the error
error(i) = Vdesire - OUT(i);
% Update the integral term
integral = integral + error(i) * dt; % Calculate the derivative term
derivative = (error(i) - previous_error) / dt; % Calculate the PID output
PID_output = Kp * error(i) + Ki * integral + Kd * derivative; % Update the output based on PID control
OUT(i) = OUT(i) + PID_output; % Update previous error
previous_error = error(i);
end% Step 5: Display the Results
disp('Final Output after PID Control:');
disp(OUT);
% Plotting the results for visualization
figure;
plot(1:length(A), A, 'b-o', 'DisplayName', 'Original Output');
hold on;
plot(1:length(OUT), OUT, 'r-x', 'DisplayName', 'PID Controlled Output');
yline(Vdesire, 'g--', 'Desired Output', 'DisplayName', 'Desired Output');
xlabel('Time Step');
ylabel('Output Value');
title('PID Controller Output Adjustment');
legend show;
grid on;
Please see attached.


Explanation of the Code
Initialization: The code begins by defining the practical data, the variable d, and the desired output Vdesire.
PID Parameters: The proportional, integral, and derivative gains (Kp, Ki, Kd) are initialized. These parameters can be tuned based on the specific requirements of your system.
Control Loop: The loop iterates through each output value, calculating the error, updating the integral and derivative terms, and adjusting the output based on the PID control law.
Results Display: The final output after applying the PID controller is displayed, and a plot is generated to visualize the original output, the PID-controlled output, and the desired output.
By adjusting the PID parameters, you can fine-tune the controller's performance to achieve the desired output effectively. Feel free to modify the gains and observe how they affect the system's response.