PID controller for m file

3 ビュー (過去 30 日間)
mohammed hussein
mohammed hussein 2024 年 12 月 10 日
コメント済み: mohammed hussein 2025 年 1 月 12 日
Hi
I have system (NOT transfer function) that gives vector numbers. Also, I have specific desire output. I would like to add PID controller as a control to reduce the error between the desire and output based on specific variable (d). Can you please help me with this situation
For example,
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; % desire output
  5 件のコメント
mohammed hussein
mohammed hussein 2024 年 12 月 10 日
A is a practical data changed with time .
Mathieu NOE
Mathieu NOE 2024 年 12 月 11 日
sorry, it's still unclear for me
where does this data come from , and how it has been genrated and collected , and if it's a system output what was the corresponding input ?

サインインしてコメントする。

採用された回答

Umar
Umar 2025 年 1 月 12 日

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.

  1 件のコメント
mohammed hussein
mohammed hussein 2025 年 1 月 12 日

Thank you so much for your answer

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangePID Controller Tuning についてさらに検索

タグ

製品


リリース

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by