How do I perform PID controller from error by using matlab script?
조회 수: 8 (최근 30일)
이전 댓글 표시
I want to change the PID that is shown in this figure to matlab script
I am tring to use
PID=Kp*ev + Ki * integral(ev)+ Kd * diff(ev);
But the term "Ki * integral(ev)" gives an error
댓글 수: 1
Sam Chak
2024년 3월 15일
I asked the MATLAB Assistant the following question: "How can I simulate a system with the equation x'' + x' + x = u, where the error-based PID controller u is defined as u = Kp*e + Ki*∫ e dt + Kd*de/dt, and the values of Kp, Ki, and Kd are 24.75, 25, and 24.7525 respectively? I would like to set the setpoint to 1 and run the simulation for 1 second using ode45."
Unfortunately, the simulation is giving multiple error messages. Additionally, I have presented the expected output responses from the pid() and feedback() commands.
%% ----- Expected result ------
Gp = tf(1, [1 1 1]); % Plant
kp = 24.75;
ki = 25;
kd = 24.7525;
Gc = pid(kp, ki, kd) % PID controller
Gcl = feedback(Gc*Gp, 1); % closed-loop
step(Gcl, 1), grid on
Code generated by MATLAB Assistant in AI Chat Playground
% Define the PID controller gains
kp = 24.75;
ki = 25;
kd = 24.7525;
% Define the desired setpoint
setpoint = 1;
% Define the error signal
error = @(t, x) setpoint - x(1);
% Create a function that calculates the control signal
controlSignal = @(t, x) kp*error(t, x) + ki*trapz(t, error(t, x)) + kd*gradient(error(t, x))/gradient(t);
% Define the system dynamics
systemDynamics = @(t, x) [x(2); - 1*x(2) - 1*x(1) + controlSignal(t, x)];
% Define the simulation time span
tspan = [0 1];
% Define the initial conditions
x0 = [0; 0];
% Simulate the system using ode45
[t, x] = ode45(systemDynamics, tspan, x0);
% Plot the response
plot(t, x(:, 1)), grid on
xlabel('Time');
ylabel('Output');
title('Response');
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 PID Controller Tuning에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!