|
PID simulink优化仿真
适应度函数:- function BsJ = IPID_Fun_6(kp, ki, kd)
- global kp ki kd yout
- sim('pid_sim.slx');
- error = yout-1;
- B=0;
- for i=1:length(yout)
- if i>1
- erry=yout(i)-yout(i-1);
- if erry<0
- B=B+100*abs(erry);
- end
- end
- end
- BsJ = (sum(abs(error)) + B )/length(error);
复制代码 响应函数:
- function [timef, r, yout]=PID_Fun_6_response(Kpidi)
- global kp ki kd yout
- sim('pid_sim.slx');
- r = ones(size(yout));
- % error = yout-r;
- timef = [0:10/(length(yout)-1):10]';
复制代码 主函数:
- %% 清空环境
- clc,clear,close all
- warning off
- format longG
- addpath(genpath('./PID_funtion'))
- global kp ki kd yout
- %% 参数设置
- w = 0.6; % 惯性因子
- c1 = 2; % 加速常数
- c2 = 2; % 加速常数
- Dim = 3; % 维数
- sizepop = 20; % 粒子群规模
- maxiter = 30; % 最大迭代次数
- MinFit = 1e-9; % 最小适应值
- Vmax = 1; % 速度最大值
- Vmin = -1; % 速度最小值
- % P I D
- Ub = [100 500 100]; % PID取值 最大值
- Lb = [0.00 0.00 0.00]; % PID取值 最小值
- %% 适应度函数
- choosed = 6;
- if choosed==1
- fun = @(x)PID_Fun_1(x);
- elseif(choosed==2)
- fun = @(x)PID_Fun_2(x);
- elseif(choosed==3)
- fun = @(x)PID_Fun_3(x);
- elseif(choosed==4)
- fun = @(x)PID_Fun_4(x);
- elseif(choosed==5)
- fun = @(x)IPID_Fun_5(x);
- elseif(choosed==6)
- fun = @(x)IPID_Fun_6(x);
- end
- %% 粒子群初始化
- Range = ones(sizepop,1)*(Ub-Lb);
- pop = rand(sizepop,Dim).*Range + ones(sizepop,1)*Lb; % 初始化粒子群
- V = rand(sizepop,Dim)*(Vmax-Vmin) + Vmin; % 初始化速度
- fitness = zeros(sizepop,1);
- for i=1:sizepop
- kp = pop(i,1);
- ki = pop(i,2);
- kd = pop(i,3);
- fitness(i,:) = fun(pop(i,:)); % 粒子群的适应值
- end
- %% 个体极值和群体极值
- [bestf, bestindex]=min(fitness);
- zbest=pop(bestindex,:); % 全局最佳
- gbest=pop; % 个体最佳
- fitnessgbest=fitness; % 个体最佳适应值
- fitnesszbest=bestf; % 全局最佳适应值
- %% 迭代寻优
- iter = 0;
- y_fitness = zeros(1,maxiter ); % 预先产生4个空矩阵
- K_p = zeros(1,maxiter );
- K_i = zeros(1,maxiter );
- K_d = zeros(1,maxiter );
- while( (iter < maxiter ) && (fitnesszbest > MinFit) )
- disp(['当前迭代次数: ', num2str(iter) ])
- for j=1:sizepop
- % 速度更新
- V(j,:) = V(j,:) + c1*rand*(gbest(j,:) - pop(j,:)) + c2*rand*(zbest - pop(j,:));
- if V(j,:)>Vmax,
- V(j,:)=Vmax;
- end
- if V(j,:)<Vmin,
- V(j,:)=Vmin;
- end
- % 位置更新
- pop(j,:)=pop(j,:)+w*V(j,:);
- pop(j,:) = lb_ub(pop(j,:),Lb,Ub);
-
- % 适应值
- kp = pop(j,1);
- ki = pop(j,2);
- kd = pop(j,3);
- fitness(j,:) =fun(pop(j,:));
-
- % 个体最优更新
- if fitness(j) < fitnessgbest(j)
- gbest(j,:) = pop(j,:);
- fitnessgbest(j) = fitness(j);
- end
- % 群体最优更新
- if fitness(j) < fitnesszbest
- zbest = pop(j,:);
- fitnesszbest = fitness(j);
- end
- end
- iter = iter+1; % 迭代次数更新
- y_fitness(1,iter) = fitnesszbest; % 为绘图做准备
- K_p(1,iter) = zbest(1);
- K_i(1,iter) = zbest(2);
- K_d(1,iter) = zbest(3);
- end
- %% 绘图
- kp = zbest(1);
- ki = zbest(2);
- kd = zbest(3);
- disp(['PSO最优解', num2str(zbest)])
- disp(['PSO最优解对应的目标值', num2str(fitnesszbest)])
- fprintf('\n')
- figure(1) % 绘制性能指标ITAE的变化曲线
- plot(y_fitness,'LineWidth',3)
- title('最优个体适应值','fontsize',10);
- xlabel('迭代次数','fontsize',10);ylabel('适应值','fontsize',10);
- set(gca,'Fontsize',10);
- grid on
- figure(2) % 绘制PID控制器参数变化曲线
- plot(K_p,'b-','LineWidth',2); hold on
- plot(K_i,'k-.','LineWidth',2)
- plot(K_d,'r--','LineWidth',2)
- title('Kp、Ki、Kd 优化曲线','fontsize',10);
- xlabel('迭代次数','fontsize',10); ylabel('参数值','fontsize',10);
- set(gca,'Fontsize',10);
- legend('Kp','Ki','Kd',1);
- grid on;hold off;
- % PID响应
- if choosed==1
- [rint, yout]=PID_Fun_1_response(zbest);
- elseif(choosed==2)
- [rint, yout]=PID_Fun_2_response(zbest);
- elseif(choosed==3)
- [rint, yout]=PID_Fun_3_response(zbest);
- elseif(choosed==4)
- [rint, yout]=PID_Fun_4_response(zbest);
- elseif(choosed==5)
- [timef, rint, yout]=PID_Fun_5_response(zbest);
- elseif(choosed==6)
- [timef, rint, yout]=PID_Fun_6_response(zbest);
- end
- figure('color',[1,1,1])
- plot(timef, rint,'b-','linewidth',2);hold on;
- plot(timef, yout,'r.-','linewidth',2);
- axis tight;grid on;
- hold off;
- err = rint-yout;
- figure,
- plot(timef, err,'r.-','linewidth',2);
- axis tight;grid on;
- title('PSO误差值')
- xlabel('迭代次数','fontsize',10); ylabel('误差值','fontsize',10);
- hold off;
- save PSO.mat
- rmpath(genpath('./PID_funtion'))
- % rmpath(genpath('./test_function'))
复制代码 simulink模型如下:
参考:
【1】PID simulink优化仿真
【2】如何在simulink中利用优化工具箱(遗传算法)对PID参数优化
【3】百度网盘完整代码 下载链接:https://pan.baidu.com/s/18clJnM64yXIC4llYs8ljvw 提取码:dvub
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|