clc;clear all;close all;
%% initialization
%
swarm_size = 64; % number of the swarm particles
maxIter = 50; % maximum number of iterations
inertia = 1.0;
correction_factor = 2.0;
% set the position of the initial swarm
a = 1:8;
[X,Y] = meshgrid(a,a);
C = cat(2,X',Y');
D = reshape(C,[],2);
swarm(1:swarm_size,1,1:2) = D; % set the position of the particles in 2D
swarm(:,2,:) = 0; % set initial velocity for particles
swarm(:,4,1) = 1000; % set the best value so far
plotObjFcn = 1; % set to zero if you do not need a final plot
%% define the objective funcion here (vectorized form)
objfcn = @(x)(x(:,1) - 20).^2 + (x(:,2) - 25).^2;
tic;
%% The main loop of PSO
for iter = 1:maxIter
swarm(:, 1, 1) = swarm(:, 1, 1) + swarm(:, 2, 1)/1.3; %update x position with the velocity
swarm(:, 1, 2) = swarm(:, 1, 2) + swarm(:, 2, 2)/1.3; %update y position with the velocity
x = swarm(:, 1, 1); % get the updated position
y = swarm(:, 1, 2); % updated position
fval = objfcn([x y]); % evaluate the function using the position of the particle
% compare the function values to find the best ones
for ii = 1:swarm_size
if fval(ii,1) < swarm(ii,4,1)
swarm(ii, 3, 1) = swarm(ii, 1, 1); % update best x position,
swarm(ii, 3, 2) = swarm(ii, 1, 2); % update best y postions
swarm(ii, 4, 1) = fval(ii,1); % update the best value so far
end
end
[~, gbest] = min(swarm(:, 4, 1)); % find the best function value in total
% update the velocity of the particles
swarm(:, 2, 1) = inertia*(rand(swarm_size,1).*swarm(:, 2, 1)) + correction_factor*(rand(swarm_size,1).*(swarm(:, 3, 1) ...
- swarm(:, 1, 1))) + correction_factor*(rand(swarm_size,1).*(swarm(gbest, 3, 1) - swarm(:, 1, 1))); %x velocity component
swarm(:, 2, 2) = inertia*(rand(swarm_size,1).*swarm(:, 2, 2)) + correction_factor*(rand(swarm_size,1).*(swarm(:, 3, 2) ...
- swarm(:, 1, 2))) + correction_factor*(rand(swarm_size,1).*(swarm(gbest, 3, 2) - swarm(:, 1, 2))); %y velocity component
% plot the particles
clf;plot(swarm(:, 1, 1), swarm(:, 1, 2), 'bx'); % drawing swarm movements
axis([-2 40 -2 40]);
pause(.1); % un-comment this line to decrease the animation speed
disp(['iteration: ' num2str(iter)]);
end
toc
%% plot the function
if plotObjFcn
ub = 40;
lb = 0;
npoints = 1000;
x = (ub-lb) .* rand(npoints,2) + lb;
for ii = 1:npoints
f = objfcn([x(ii,1) x(ii,2)]);
plot3(x(ii,1),x(ii,2),f,'.r');hold on
end
plot3(swarm(1,3,1),swarm(1,3,2),swarm(1,4,1),'xb','linewidth',5,'Markersize',5);grid
end
没有合适的资源?快使用搜索试试~ 我知道了~
matlab-VPSO搜索三维曲面的最小值的matlab仿真(包含教程)
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
共3个文件
m:1个
jpg:1个
avi:1个
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 156 浏览量
2021-09-08
22:36:42
上传
评论
收藏 3MB 7Z 举报
温馨提示
matlab_VPSO搜索三维曲面的最小值的matlab仿真(包含教程)
资源推荐
资源详情
资源评论
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![thumb](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/AVI.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
共 3 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/d5fa1452106248a4a63014172db25c5d_leavemyleave.jpg!1)
mYlEaVeiSmVp
- 粉丝: 1973
- 资源: 19万+
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)