matlab常用算法大全.docx
《matlab常用算法大全.docx》由会员分享,可在线阅读,更多相关《matlab常用算法大全.docx(20页珍藏版)》请在冰豆网上搜索。
matlab常用算法大全
Matlab高级算法程序代码汇总
一、灰色预测模型matlab程序
%renkou1=renkou(:
1);%年末常住人口数
%renkou2=renkou(:
2);%户籍人口
%renkou3=renkou(:
3);%非户籍人口
%shjian=1979:
2010;
%以上数据自己给
x0=renkou2';
n=length(x0);
lamda=x0(1:
n-1)./x0(2:
n)
range=minmax(lamda)
x1=cumsum(x0)
fori=2:
n
z(i)=0.5*(x1(i)+x1(i-1));
end
B=[-z(2:
n)',ones(n-1,1)];
Y=x0(2:
n)';
u=B\Y
x=dsolve('Dx+a*x=b','x(0)=x0');
x=subs(x,{'a','b','x0'},{u
(1),u
(2),x1
(1)});
yuce1=subs(x,'t',[0:
n-1]);
digits(6),y=vpa(x)%为提高预测精度,先计算预测值,再显示微分方程的解
yuce=[x0
(1),diff(yuce1)]
epsilon=x0-yuce%计算残差
delta=abs(epsilon./x0)%计算相对误差
rho=1-(1-0.5*u
(1))/(1+0.5*u
(1))*lamda%计算级比偏差值
%以深圳人口数据得到预测模型及预测误差相关数据
lamda=
Columns1through8
0.97410.96110.94190.87490.93110.90930.93020.9254
Columns9through16
0.92450.92780.94420.93760.91270.91480.93320.9477
Columns17through24
0.95920.94450.95510.95620.95940.94610.94690.9239
Columns25through31
0.91400.90770.92430.92680.93120.94460.9618
range=
0.87490.9741
x1=
1.0e+003*
Columns1through8
0.03130.06340.09670.13220.17270.21620.26410.3155
Columns9through16
0.37110.43130.49610.56470.63800.71820.80590.8999
Columns17through24
0.99901.10241.21191.32651.44631.57121.70331.8427
Columns25through32
1.99362.15882.34072.53752.74992.97803.21943.4705
u=
-0.0665
31.3737
y=
-472.117+503.377*exp(.664533e-1*t)
yuce=
Columns1through8
31.260034.587636.964139.504042.218345.119248.219451.5326
Columns9through16
55.073458.857662.901767.223871.842876.779282.054887.6928
Columns17through24
93.7183100.1578107.0397114.3945122.2547130.6550139.6324149.2267
Columns25through32
159.4802170.4382182.1492194.6649208.0405222.3352237.6121253.9386
epsilon=
Columns1through8
0-2.4976-3.5741-4.0540-1.6983-1.5992-0.3594-0.0826
Columns9through16
0.52661.28241.91831.42621.37723.44085.63526.2772
Columns17through24
5.44173.22222.42030.2055-2.4047-5.7350-7.5924-9.7767
Columns25through32
-8.5502-5.3082-0.21922.16514.33955.73483.8379-2.9086
delta=
Columns1through8
00.07780.10700.11440.04190.03670.00750.0016
Columns9through16
0.00950.02130.02960.02080.01880.04290.06430.0668
Columns17through24
0.05490.03120.02210.00180.02010.04590.05750.0701
Columns25through32
0.05670.03210.00120.01100.02040.02510.01590.0116
rho=
Columns1through8
-0.0411-0.0271-0.00660.06500.00490.02820.00580.0110
Columns9through16
0.01190.0084-0.0091-0.00200.02450.02230.0027-0.0128
Columns17through24
-0.0251-0.0094-0.0208-0.0219-0.0254-0.0111-0.01190.0126
Columns25through31
0.02320.03000.01220.00950.0048-0.0095-0.0280
二、遗传算法程序代码
%Optimizingafunction usingSimpleGeneticAlgorithmwithelitistpreserved
%Maxf(x1,x2)=100*(x1*x1-x2).^2+(1-x1).^2;-2.0480<=x1,x2<=2.0480
%Author:
WangYonglin(wylin77@)
clc;clearall;
formatlong;%设定数据显示格式
%初始化参数
T=100;%仿真代数
N=80;%群体规模
pm=0.05;pc=0.8;%交叉变异概率
umax=2.048;umin=-2.048;%参数取值范围
L=10;%单个参数字串长度,总编码长度2L
bval=round(rand(N,2*L));%初始种群
bestv=-inf;%最优适应度初值
%迭代开始
forii=1:
T
%解码,计算适应度
fori=1:
N
y1=0;y2=0;
forj=1:
1:
L
y1=y1+bval(i,L-j+1)*2^(j-1);
end
x1=(umax-umin)*y1/(2^L-1)+umin;
forj=1:
1:
L
y2=y2+bval(i,2*L-j+1)*2^(j-1);
end
x2=(umax-umin)*y2/(2^L-1)+umin;
obj(i)=100*(x1*x1-x2).^2+(1-x1).^2;%目标函数
xx(i,:
)=[x1,x2];
end
func=obj;%目标函数转换为适应度函数
p=func./sum(func);
q=cumsum(p);%累加
[fmax,indmax]=max(func);%求当代最佳个体
iffmax>=bestv
bestv=fmax;%到目前为止最优适应度值
bvalxx=bval(indmax,:
);%到目前为止最佳位串
optxx=xx(indmax,:
);%到目前为止最优参数
end
Bfit1(ii)=bestv;%存储每代的最优适应度
%%%%遗传操作开始
%轮盘赌选择
fori=1:
(N-1)
r=rand;
tmp=find(r<=q);
newbval(i,:
)=bval(tmp
(1),:
);
end
newbval(N,:
)=bvalxx;%最优保留
bval=newbval;
%单点交叉
fori=1:
2:
(N-1)
cc=rand;
ifccpoint=ceil(rand*(2*L-1));%取得一个1到2L-1的整数
ch=bval(i,:
);
bval(i,point+1:
2*L)=bval(i+1,point+1:
2*L);
bval(i+1,point+1:
2*L)=ch(1,point+1:
2*L);
end
end
bval(N,:
)=bvalxx;%最优保留
%位点变异
mm=rand(N,2*L)mm(N,:
)=zeros(1,2*L);%最后一行不变异,强制赋0
bval(mm)=1-bval(mm);
end
%输出
plot(Bfit1);%绘制最优适应度进化曲线
bestv %输出最优适应度值
optxx %输出最优参数
三、种子群算法程序代码
%declaretheparametersoftheoptimization
max_iterations=1000;
no_of_particles=50;
dimensions=1;
delta_min=-0.003;
delta_max=0.003;
c1=1.3;
c2=1.3;
%initialisetheparticlesandteirvelocitycomponents
forcount_x=1:
no_of_particles
forcount_y=1:
dimensions
particle_position(count_x,count_y)=rand*10;
particle_velocity(count_x,count_y)=rand;
p_best(count_x,count_y)=particle_position(count_x,count_y);
end
end
%initializethep_best_fitnessarray
forcount=1:
no_of_particles
p_best_fitness(count)=-1000;
end
%particle_position
%particle_velocity
%mainparticleswrmroutine
forcount=1:
max_iterations
%findthefitnessofeachparticle
%changefitnessfunctionasperequationrequiresdanddimensions
forcount_x=1:
no_of_particles
%x=particle_position(count_x,1);
%y=particle_position(count_x,2);
%z=particle_position(count_x,3);
%soln=x^2-3*y*x+z;
%x=particle_position(count_x);
%soln=x^2-2*x+1;
x=particle_position(count_x);
soln=x-7;
ifsoln~=0
current_fitness(count_x)=1/abs(soln);
else
current_fitness=1000;
end
end
%decideonp_bestetcforeachparticle
forcount_x=1:
no_of_particles
ifcurrent_fitness(count_x)>p_best_fitness(count_x)
p_best_fitness(count_x)=current_fitness(count_x);
forcount_y=1:
dimensions
p_best(count_x,count_y)=particle_position(count_x,count_y);
end
end
end
%decideontheglobalbestamongalltheparticles
[g_best_val,g_best_index]=max(current_fitness);
%g_bestcontainsthepositionoftehglobalbest
forcount_y=1:
dimensions
g_best(count_y)=particle_position(g_best_index,count_y);
end
%updatethepositionandvelocitycompponents
forcount_x=1:
no_of_particles
forcount_y=1:
dimensions
p_current(count_y)=particle_position(count_x,count_y);
end
forcount_y=1:
dimensions
particle_velocity(count_y)=particle_velocity(count_y)+c1*rand*(p_best(count_y)-p_current(count_y))+c2*rand*(g_best(count_y)-p_current(count_y));
particle_positon(count_x,count_y)=p_current(count_y)+particle_velocity(count_y);
end
end
end
g_best
current_fitness(g_best_index)
clearall,clc%psoexample
iter=1000;%numberofalgorithmiterations
np=2;%numberofmodelparameters
ns=10;%numberofsetsofmodelparameters
Wmax=0.9;%maximuminertialweight
Wmin=0.4;%minimuminertialweight
c1=2.0;%parameterinPSOmethodology
c2=2.0;%parameterinPSOmethodology
Pmax=[1010];%maximummodelparametervalue
Pmin=[-10-10];%minimummodelparametervalue
Vmax=[11];%maximumchangeinmodelparameter
Vmin=[-1-1];%minimumchangeinmodelparameter
modelparameters(1:
np,1:
ns)=0;%setallmodelparameterestimatesforallmodelparametersetstozero
modelparameterchanges(1:
np,1:
ns)=0;%setallchangeinmodelparameterestimatesforallmodelparametersetstozero
bestmodelparameters(1:
np,1:
ns)=0;%setbestmodelparameterestimatesforallmodelparametersetstozero
setbestcostfunction(1:
ns)=1e6;%setbestcostfunctionofeachmodelparametersettoalargenumber
globalbestparameters(1:
np)=0;%setbestmodelparametervaluesforallmodelparametersetstozero
bestparameters=globalbestparameters';%bestmodelparametervaluesforallmodelparametersets(toplot)
globalbestcostfunction=1e6;%setbestcostfunctionforallmodelparametersetstoalargenumber
i=0;%indicatesithalgorithmiteration
j=0;%indicatesjthsetofmodelparameters
k=0;%indicateskthmodelparameter
fork=1:
np%initialization
forj=1:
ns
modelparameters(k,j)=(Pmax(k)-Pmin(k))*rand
(1)+Pmin(k);%randomlydistributemodelparameters
modelparameterchanges(k,j)=(Vmax(k)-Vmin(k))*rand
(1)+Vmin(k);%randomlydistributechangeinmodelparameters
end
end
fori=2:
iter
forj=1:
ns
x=modelparameters(:
j);
%calculatecostfunction
costfunction=105*(x
(2)-x
(1)^2)^2+(1-x
(1))^2;
ifcostfunction bestmodelparameters(:
j)=modelparameters(:
j);
setbestcostfunction(j)=costfunction;
end
四、模拟退火算法
%ford=1:
50%循环10次发现最小路径为4.115,循环50次有3次出现4.115
T_max=80;%input('pleaseinputthestarttemprature');
T_min=0.001;%input('pleaseinputtheendtemprature');
iter_max=100;%input('pleaseinputthemostinterpstepsonthefittemp');
s_max=100;%input('pleaseinputthemoststeadystepsontthefittemp');
T=T_max;
load.\address.txt;
order1=randperm(size(address,1))';%生成初始解。
figure
(1);
plot(address(order1,1),address(order1,2),'*r-')
title('随机产生的路径');
totaldis1=distance(address,order1);
forn=1:
size(address,1)
text(address(n,1)+0.01,address(n,2),num2str(n))%标号
end
text(0.9,0.9,num2str(totaldis1))
figure
(2);
whileT>=T_min
iter_num=1;
s_num=1;
plot(T,totaldis1,'r.')
holdon
whileiter_numorder2=exhgpath(order1);%随机交换两个城市位置
totaldis2=distance(address,order2);
R=rand;
DeltaDis=totaldis2-totaldis1;%新的距离-原来的距离
ifDeltaDis<0;
order1=order2;
totaldis1=totaldis2;%新距离小,无条件接受
elseif(exp((totaldis1-totaldis2)/(T))>R)%%%%本算法最核心的思想:
以一定概率接受坏的结果,防止局部最优
order1=order2;
totaldis1=totaldis2;
elses_num=s_num+1;
end
iter_num=iter_num+1;
end
T=T*0.99;
end
set(gca,'xscale','log');%或者使用semilogx,有相同效果
xlabel('退火温度');ylabel('总距离');
order1
totaldis1
figure(3)
plot(address(order1,1)