✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,matlab项目合作可私信。
🍎个人主页:Matlab科研工作室
🍊个人信条:格物致知。
更多Matlab仿真内容点击👇
⛄ 内容介绍
为了寻找最佳的比例,积分和微分(PID)控制器增益因子,使系统输出尽可能接近参考输入信号的变化,使得控制系统的时间加权绝对误差积分最小,提出了基于灰狼优化算法(GWO)进行PID控制器增益因子优化设计的方法.仿真结果表明,基于灰狼优化算法的最优参数PID控制器比基于其他算法的性能更好,最优值更佳.
⛄ 部分代码
x0=[-pi pi 0 0 0 0];
Ts=[0 5];
% initialize alpha, beta, and delta_pos
Alpha_pos=zeros(1,dim);
Alpha_score=inf; %change this to -inf for maximization problems
Beta_pos=zeros(1,dim);
Beta_score=inf; %change this to -inf for maximization problems
Delta_pos=zeros(1,dim);
Delta_score=inf; %change this to -inf for maximization problems
%Initialize the positions of search agents
Positions=initializationr(SearchAgents_no,dim,ub,lb);
Convergence_curve=zeros(1,Max_iter);
l=0;% Loop counter
% Main loop
while l<Max_iter
l
for i=1:size(Positions,1)
% Return back the search agents that go beyond the boundaries of the search space
% Flag4ub=Positions(i,:)>ub;
% Flag4lb=Positions(i,:)<lb;
% Positions(i,:)=(Positions(i,:).*(~(Flag4ub+Flag4lb)))+ub.*Flag4ub+lb.*Flag4lb;
Kpd=Positions(i,:);
[T,X] = ode45(@(t,x) r2dof(t,x,Kpd),Ts,x0);
qd1=sin(4.17*T);
% qdot_r1=4.17*cos(4.17*T);
% qdot2_r1=-4.17*4.17*sin(4.17*T);
qd2=1.2*sin(5.11*T);
% qdot_r2=1.2*5.11*cos(5.11*T);
% qdot2_r2=-1.2*5.11*5.11*sin(5.11*T);
th1=X(:,1); %theta1 wavwform
th2=X(:,2);
fitness=Fitnessr(th1,th2,qd1,qd2);
% Calculate objective function for each search agent
% fitness=Fitnessr(Positions(i,:));
% Update Alpha, Beta, and Delta
if fitness<Alpha_score
Alpha_score=fitness; % Update alpha
Alpha_pos=Positions(i,:);
end
if fitness>Alpha_score && fitness<Beta_score
Beta_score=fitness; % Update beta
Beta_pos=Positions(i,:);
end
if fitness>Alpha_score && fitness>Beta_score && fitness<Delta_score
Delta_score=fitness; % Update delta
Delta_pos=Positions(i,:);
end
end
a=2-l*((2)/Max_iter); % a decreases linearly fron 2 to 0
% Update the Position of search agents including omegas
for i=1:size(Positions,1)
% for j=1:size(Positions,2)
r1=rand(); % r1 is a random number in [0,1]
r2=rand(); % r2 is a random number in [0,1]
A1=2*a*r1-a; % Equation (3.3)
C1=2*r2; % Equation (3.4)
D_alpha=abs(C1*Alpha_pos-Positions(i,:)); % Equation (3.5)-part 1
X1=Alpha_pos-A1*D_alpha; % Equation (3.6)-part 1
r1=rand();
r2=rand();
A2=2*a*r1-a; % Equation (3.3)
C2=2*r2; % Equation (3.4)
D_beta=abs(C2*Beta_pos-Positions(i,:)); % Equation (3.5)-part 2
X2=Beta_pos-A2*D_beta; % Equation (3.6)-part 2
r1=rand();
r2=rand();
A3=2*a*r1-a; % Equation (3.3)
C3=2*r2; % Equation (3.4)
D_delta=abs(C3*Delta_pos-Positions(i,:)); % Equation (3.5)-part 3
X3=Delta_pos-A3*D_delta; % Equation (3.5)-part 3
Positions(i,:)=(X1+X2+X3)/3;% Equation (3.7)
% end
end
l=l+1;
Convergence_curve(l)=Alpha_score;
end
⛄ 运行结果
⛄ 参考文献
[1]赵华东, 宋保业, 张建胜,等. 基于粒子群优化算法的分数阶PID控制器设计[J]. 山东科技大学学报:自然科学版, 2017, 36(4):6.
[2]蒋建辉. 基于粒子群优化算法的分数阶PID控制器设计[J]. 中小企业管理与科技, 2015(21):2.
[3]冯严冰. 基于灰狼优化算法的PID控制器设计[J]. 传感器世界, 2022, 28(8):5.