MATLAB Answers

how to tune pid controller using genetic algorithm?

115 views (last 30 days)
SURESH PANDIYAN on 14 Mar 2016
Commented: SURBHI GOEL on 25 Feb 2021
Hi friends, I had designed longitudinal flight control system in simulink. In that model i want to tune PID controller by using Genetic algorithm.How can i get Kp,Ki,Kd values ?
anybody knows how to use GA in simulink, Pls help me...

Answers (1)

Amirali Najafi
Amirali Najafi on 20 May 2020
Code credit to Steve Brunton
close all; clear all; clc
dt = 0.001;
PopSize = 25;
MaxGenerations = 10;
s = tf('s');
G = 1/(s*(s*s+s+1));
options = optimoptions(@ga,'PopulationSize',PopSize,'MaxGenerations',MaxGenerations);
[x,fval] = ga(@(K)pidtest(G,dt,K),3,-eye(3),zeros(3,1))
function J = pidtest(G,dt,parms)
s = tf('s');
K = parms(1) + parms(2)/s + parms(3)*s/(1+.001*s);
Loop = series(K,G);
ClosedLoop = feedback(Loop,1);
t = 0:dt:20;
[y,t] = step(ClosedLoop,t);
CTRLtf = K/(1+K*G);
u = lsim(CTRLtf,1-y,t);
Q = 1;
R = .001;
J = dt*sum(Q*(1-y(:)).^2+R*u(:).^2)
[y,t] = step(ClosedLoop,t);
SURBHI GOEL on 25 Feb 2021
@James Keen If Y is the controller output and X is the controller input, are you defining controller action as Y/X?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!