Implement PID in ode45 code

So, I have a ode45 function and I have an error defined in it which changes at different iterations of the solver. I was able to define the derivative part by just differentiating the error formula but I am unable to implement the integral part. The error expression is:
, where q represents position and v represents velocity. Is it possible to get the PID part in the ode function.

 采纳的回答

If the error is defined as
then
P part is
I part is
D part is
and you can arrange then in the state-space form. For example, a Double Integrator system
can be rewritten in state-space as:
.
The PID has 3 terms, and the state-space is in differential form. So you have no issue with the P and the D part, because they are part of the state variables. The I part is in integral form, so you have to create an additional state variable. See Example below:
[t, x] = ode45(@DIsystem, [0 20], [0; 0; 0]);
plot(t, x(:,1), 'linewidth', 1.5)
grid on, xlabel('t'), ylabel('y(t)'), % ylim([-0.2 1.2])
function dxdt = DIsystem(t, x)
dxdt = zeros(3, 1);
% construction of PID
r = 1; % reference signal
e = x(1) - r; % error signal
Kp = 1 + sqrt(2); % proportional gain
Ki = 1; % integral gain
Kd = 1 + sqrt(2); % derivative gain
u = - Kp*e - Ki*x(3) - Kd*x(2); % the PID thing
% the dynamics
A = [0 1; 0 0]; % state matrix
B = [0; 1]; % input matrix
dxdt(1:2) = A*[x(1); x(2)] + B*u; % the Double Integrator system
dxdt(3) = e; % for integral action in PID
end

5 个评论

Um... what do you mean by state-space form? How do I get the integral part during the ode45 running? As at every iteration when the solver is called, the position and the velocity changes and so does the error. I want to integrate at every time step which also means that every error during all the previous iterations need to be added. The code for error is:
e_1=x(1)-( x(4)+(h_i*x(5)) );
As you can see it will change at every instance so how do I integrate it at every time step when the ode45 is running?
I have provided an example in the Answer using a Double Integrator system:
You will get the idea of how to implement that. Hope it works out for your system.
Thanks I got the idea, in my case I have the error:
e_1=x(1)-( x(4)+(h_i*x(5)) );
So, If I am right I need to introduce a new state which I will be equal to this error at the end of every iteration like you did with dxdt=x(3) and in the next iteration use this state as the integral of the error.
This is not right.
You are assuming that the derivative of the reference signal is 0 which is correct in this particular case. But what if it is not?
The main question in here is that how can we do differentiation in ode functions when we dont have any access to the data from previous iterations.
It is possible beacuse it is being done in simulink models. but I'm not sure how in MATLAB... :/
The main question in here is that how can we do differentiation in ode functions when we dont have any access to the data from previous iterations.
If your differential equation requires historical information about the solution from earlier times as part of evaluating the solution at the current time, you don't have an ordinary differential equation (ODE). You have a delay differential equation (DDE). There are three solvers in MATLAB for solving DDEs listed on this page: dde23, ddesd, and ddensd.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Programming 的更多信息

产品

版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by