%% 该代码为基于PSO和BP网络的预测
%
% <html>
% <table border="0" width="600px" id="table1"> <tr> <td><b><font size="2">该案例作者申明:</font></b></td> </tr> <tr><td><span class="comment"><font size="2">1:本人长期驻扎在此<a target="_blank" href="http://www.ilovematlab.cn/forum-158-1.html"><font color="#0000FF">板块</font></a>里,对该案例提问,做到有问必答。本套书籍官方网站为:<a href="http://video.ourmatlab.com">video.ourmatlab.com</a></font></span></td></tr><tr> <td><font size="2">2:点此<a href="http://union.dangdang.com/transfer/transfer.aspx?from=P-284318&backurl=http://www.dangdang.com/">从当当预定本书</a>:<a href="http://union.dangdang.com/transfer/transfer.aspx?from=P-284318&backurl=http://www.dangdang.com/">《Matlab神经网络30个案例分析》</a>。</td></tr><tr> <td><p class="comment"></font><font size="2">3</font><font size="2">:此案例有配套的教学视频,视频下载方式<a href="http://video.ourmatlab.com/vbuy.html">video.ourmatlab.com/vbuy.html</a></font><font size="2">。 </font></p></td> </tr> <tr> <td><span class="comment"><font size="2"> 4:此案例为原创案例,转载请注明出处(《Matlab神经网络30个案例分析》)。</font></span></td> </tr> <tr> <td><span class="comment"><font size="2"> 5:若此案例碰巧与您的研究有关联,我们欢迎您提意见,要求等,我们考虑后可以加在案例里。</font></span></td> </tr> </table>
% </html>
%% 清空环境
clc
clear
%读取数据
load data input output
%节点个数
inputnum=2;
hiddennum=5;
outputnum=1;
%训练数据和预测数据
input_train=input(1:1900,:)';
input_test=input(1901:2000,:)';
output_train=output(1:1900)';
output_test=output(1901:2000)';
%选连样本输入输出数据归一化
[inputn,inputps]=mapminmax(input_train);
[outputn,outputps]=mapminmax(output_train);
%构建网络
net=newff(inputn,outputn,hiddennum);
% 参数初始化
%粒子群算法中的两个参数
c1 = 1.49445;
c2 = 1.49445;
maxgen=100; % 进化次数
sizepop=30; %种群规模
Vmax=1;
Vmin=-1;
popmax=5;
popmin=-5;
for i=1:sizepop
pop(i,:)=5*rands(1,21);
V(i,:)=rands(1,21);
fitness(i)=fun(pop(i,:),inputnum,hiddennum,outputnum,net,inputn,outputn);
end
% 个体极值和群体极值
[bestfitness bestindex]=min(fitness);
zbest=pop(bestindex,:); %全局最佳
gbest=pop; %个体最佳
fitnessgbest=fitness; %个体最佳适应度值
fitnesszbest=bestfitness; %全局最佳适应度值
%% 迭代寻优
for i=1:maxgen
i;
for j=1:sizepop
%速度更新
V(j,:) = V(j,:) + c1*rand*(gbest(j,:) - pop(j,:)) + c2*rand*(zbest - pop(j,:));
V(j,find(V(j,:)>Vmax))=Vmax;
V(j,find(V(j,:)<Vmin))=Vmin;
%种群更新
pop(j,:)=pop(j,:)+0.2*V(j,:);
pop(j,find(pop(j,:)>popmax))=popmax;
pop(j,find(pop(j,:)<popmin))=popmin;
%自适应变异
pos=unidrnd(21);
if rand>0.95
pop(j,pos)=5*rands(1,1);
end
%适应度值
fitness(j)=fun(pop(j,:),inputnum,hiddennum,outputnum,net,inputn,outputn);
end
for j=1:sizepop
%个体最优更新
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
yy(i)=fitnesszbest;
end
%% 结果分析
plot(yy)
title(['适应度曲线 ' '终止代数=' num2str(maxgen)]);
xlabel('进化代数');ylabel('适应度');
x=zbest;
%% 把最优初始阀值权值赋予网络预测
% %用遗传算法优化的BP网络进行值预测
w1=x(1:inputnum*hiddennum);
B1=x(inputnum*hiddennum+1:inputnum*hiddennum+hiddennum);
w2=x(inputnum*hiddennum+hiddennum+1:inputnum*hiddennum+hiddennum+hiddennum*outputnum);
B2=x(inputnum*hiddennum+hiddennum+hiddennum*outputnum+1:inputnum*hiddennum+hiddennum+hiddennum*outputnum+outputnum);
net.iw{1,1}=reshape(w1,hiddennum,inputnum);
net.lw{2,1}=reshape(w2,outputnum,hiddennum);
net.b{1}=reshape(B1,hiddennum,1);
net.b{2}=B2;
%% BP网络训练
%网络进化参数
net.trainParam.epochs=100;
net.trainParam.lr=0.1;
%net.trainParam.goal=0.00001;
%网络训练
[net,per2]=train(net,inputn,outputn);
%% BP网络预测
%数据归一化
inputn_test=mapminmax('apply',input_test,inputps);
an=sim(net,inputn_test);
test_simu=mapminmax('reverse',an,outputps);
error=test_simu-output_test;
web browser www.matlabsky.com
web browser http://www.matlabsky.com/thread-11198-1-1.html
%%
% <html>
% <table width="656" align="left" > <tr><td align="center"><p><font size="2"><a href="http://video.ourmatlab.com/">Matlab神经网络30个案例分析</a></font></p><p align="left"><font size="2">相关论坛:</font></p><p align="left"><font size="2">《Matlab神经网络30个案例分析》官方网站:<a href="http://video.ourmatlab.com">video.ourmatlab.com</a></font></p><p align="left"><font size="2">Matlab技术论坛:<a href="http://www.matlabsky.com">www.matlabsky.com</a></font></p><p align="left"><font size="2">M</font><font size="2">atlab函数百科:<a href="http://www.mfun.la">www.mfun.la</a></font></p><p align="left"><font size="2">Matlab中文论坛:<a href="http://www.ilovematlab.com">www.ilovematlab.com</a></font></p></td> </tr></table>
% </html>
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
使用matlab进行神经网络分析的实战案例,包含所有源码及数据集文件,可方便对神经网络分析流程进行学习,具有很高的参考借鉴意义。 使用matlab进行神经网络分析的实战案例,包含所有源码及数据集文件,可方便对神经网络分析流程进行学习,具有很高的参考借鉴意义。 使用matlab进行神经网络分析的实战案例,包含所有源码及数据集文件,可方便对神经网络分析流程进行学习,具有很高的参考借鉴意义。 使用matlab进行神经网络分析的实战案例,包含所有源码及数据集文件,可方便对神经网络分析流程进行学习,具有很高的参考借鉴意义。 使用matlab进行神经网络分析的实战案例,包含所有源码及数据集文件,可方便对神经网络分析流程进行学习,具有很高的参考借鉴意义。 使用matlab进行神经网络分析的实战案例,包含所有源码及数据集文件,可方便对神经网络分析流程进行学习,具有很高的参考借鉴意义。 使用matlab进行神经网络分析的实战案例,包含所有源码及数据集文件,可方便对神经网络分析流程进行学习,具有很高的参考借鉴意义。 使用matlab进行神经网络分析的实战案例,包含所有源码及数据集文件,可方便对神经网络分析流程进行学习,具有很高的参考借鉴意义。 使用matlab进行神经网络分析的实战案例,包含所有源码及数据集文件,可方便对神经网络分析流程进行学习,具有很高的参考借鉴意义。
资源推荐
资源详情
资源评论
收起资源包目录
matlab神经网络分析实战案例(附带源码及数据集) (4).zip (11个子文件)
matlab神经网络分析实战案例(附带源码及数据集) (4)
Select.m 825B
data.m 128B
Decode.m 1KB
PSO.m 5KB
Code.m 408B
Genetic.m 4KB
Cross.m 1KB
test.m 283B
fun.m 1013B
data.mat 45KB
Mutation.m 2KB
共 11 条
- 1
资源评论
白话机器学习
- 粉丝: 9349
- 资源: 7681
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功