I need cost function for tuning PID controller help me

I have to apply some algorithms for tuning PID controller parameters (Kp,Ki,Kd) so for any algorithm i have to get separate cost function file or called objective function so if anyone has p;ease send me the file thanks

Risposte (1)

Not sure what your system is, but I think you can try the one of the simplest cost function probably looks like this:
Tuning the gains is probably simpler by manual calculations (follow formulas in textbooks) or with the pidtune() command.
function J = costfun(Kp, Ki, Kd)
[t, x] = ode45(@(t, x) myode(t, y), tspan, ic, opts);
error = x - x_ref;
J = trapz(t, error.^2); % ISE
% J = trapz(t, t.*abs(error)); % ITAE
end
function dxdt = myode(t, x)
% Insert your system and the PID controller
dxdt = A.*x + B*u;
end
Edit: An example is included.
Example: Consider the system described by
.
The cost function J is given by
where
and .
A full state-feedback controller is used:
The optimal feedback gain matrix is found using fmincon().
costfun = @objfun;
nvars = 2;
A = -eye(nvars);
b = zeros(nvars, 1);
Aeq = [];
beq = [];
lb = [0 0]; % lower bound
ub = [2 2]; % upper bound
k0 = [0.5, 1.5]; % initial guess
[k, fval, exitflag, output] = fmincon(costfun, k0, A, b, Aeq, beq, lb, ub)
Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
k = 1×2
1.0002 1.0006
fval = 2.0000
exitflag = 1
output = struct with fields:
iterations: 14 funcCount: 45 constrviolation: 0 stepsize: 8.2993e-05 algorithm: 'interior-point' firstorderopt: 8.0557e-08 cgiterations: 0 message: 'Local minimum found that satisfies the constraints.↵↵Optimization completed because the objective function is non-decreasing in ↵feasible directions, to within the value of the optimality tolerance,↵and constraints are satisfied to within the value of the constraint tolerance.↵↵<stopping criteria details>↵↵Optimization completed: The relative first-order optimality measure, 8.055695e-08,↵is less than options.OptimalityTolerance = 1.000000e-06, and the relative maximum constraint↵violation, 0.000000e+00, is less than options.ConstraintTolerance = 1.000000e-06.' bestfeasible: [1×1 struct]
function J = objfun(k)
tspan = linspace(0, 30, 3001);
ic = [1 0];
[t, x] = ode45(@(t, x) myode(t, x, k), tspan, ic);
Q = eye(2);
R = 1;
ifcn = Q(1,1)*x(:,1).^2 + Q(2,2)*x(:,2).^2 + R*(- k(1)*x(:,1) - k(2)*x(:,2)).^2;
J = trapz(t, ifcn); % ISE
end
function dxdt = myode(t, x, k)
dxdt = zeros(2, 1);
% my controller
u = - k(1)*x(1) - k(2)*x(2);
% my system
dxdt(1) = x(2);
dxdt(2) = - x(2) + u;
end

9 Commenti

thanks for response
can you give me ITAE or ISE cost function for tuning PID ?
You are welcome, @Yazeed Ali. I searched your given keywords on Google and used the formulas for ISE and ITAE. The Answer is edited to include that. If you find the cost functions helpful, please consider accepting ✔ and voting 👍 the Answer.
unable to perform this assignment because of the left side is 1-by-1 and the size of the right side is 1-by-201
i get this error when i insert ITAE cost function
i have same error here can you please share your facebook account so we can keep in contact
The original question only asks for the cost function candidates.
What optimization algorithm did both of you use?
I'm using wolf grey optimization
I'm unfamiliar with the Gray Wolf Optimizer. However, I have edited my Answer to include an example using MATLAB optimizer fmincon(). Basically, I think the idea of optimization should be more or less the same.
You can try optimizing my test cost function for a 2nd-order system using your Gray Wolf Optimizer. If you get
and ,
then you can be sure that your algorithm works (at least for a 2nd-order system).
If it works, be sure to accept ✔ and voting 👍 the Answer. Thanks a bunch! 🙏.
I want to send you my matlab files can you share me your mail id please
Sam Chak
Sam Chak il 27 Dic 2022
Modificato: Sam Chak il 27 Dic 2022
I'm familiar with Grey Wolf. Why not consider posting the GWO code and the error message in a new Question? GWO was published by Seyedali Mirjalili in 2014. Optimization experts in MATLAB would be able to advise you.
I'm not sure if the proposed Cost function is helpful or not. Most likely it is because you cannot test with the GWO. However, it works with other optimizers such GA, PSO, ABC, etc., then we can safely assure that the proposed "Cost Function template" maybe effective.
I only verified it using fmincon. It does not guarantee to work with other optimizers. Let me know it doesn't work because I may consider removing it as I don't want other users to be misguided by the 'flawed' Cost Function. Thanks!

Accedi per commentare.

Richiesto:

il 22 Nov 2022

Modificato:

il 27 Dic 2022

Community Treasure Hunt

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

Start Hunting!

Translated by