matlab常用算法大全Word格式文档下载.docx
- 文档编号:17574395
- 上传时间:2022-12-07
- 格式:DOCX
- 页数:19
- 大小:22.47KB
matlab常用算法大全Word格式文档下载.docx
《matlab常用算法大全Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《matlab常用算法大全Word格式文档下载.docx(19页珍藏版)》请在冰豆网上搜索。
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*
0.03130.06340.09670.13220.17270.21620.26410.3155
0.37110.43130.49610.56470.63800.71820.80590.8999
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=
31.260034.587636.964139.504042.218345.119248.219451.5326
55.073458.857662.901767.223871.842876.779282.054887.6928
93.7183100.1578107.0397114.3945122.2547130.6550139.6324149.2267
159.4802170.4382182.1492194.6649208.0405222.3352237.6121253.9386
epsilon=
0-2.4976-3.5741-4.0540-1.6983-1.5992-0.3594-0.0826
0.52661.28241.91831.42621.37723.44085.63526.2772
5.44173.22222.42030.2055-2.4047-5.7350-7.5924-9.7767
-8.5502-5.3082-0.21922.16514.33955.73483.8379-2.9086
delta=
00.07780.10700.11440.04190.03670.00750.0016
0.00950.02130.02960.02080.01880.04290.06430.0668
0.05490.03120.02210.00180.02010.04590.05750.0701
0.05670.03210.00120.01100.02040.02510.01590.0116
rho=
-0.0411-0.0271-0.00660.06500.00490.02820.00580.0110
0.01190.0084-0.0091-0.00200.02450.02230.0027-0.0128
-0.0251-0.0094-0.0208-0.0219-0.0254-0.0111-0.01190.0126
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(wylin77126.)
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);
x1=(umax-umin)*y1/(2^L-1)+umin;
y2=y2+bval(i,2*L-j+1)*2^(j-1);
x2=(umax-umin)*y2/(2^L-1)+umin;
obj(i)=100*(x1*x1-x2).^2+(1-x1).^2;
%目标函数
xx(i,:
)=[x1,x2];
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;
%存储每代的最优适应度
%%%%遗传操作开场
%轮盘赌选择
(N-1)
r=rand;
tmp=find(r<
=q);
newbval(i,:
)=bval(tmp
(1),:
end
newbval(N,:
)=bvalxx;
%最优保存
bval=newbval;
%单点穿插
2:
cc=rand;
ifcc<
pc
point=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:
bval(N,:
%位点变异
mm=rand(N,2*L)<
pm;
%N行
mm(N,:
)=zeros(1,2*L);
%最后一行不变异,强制赋0
bval(mm)=1-bval(mm);
%输出
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
%initializethep_best_fitnessarray
forcount=1:
p_best_fitness(count)=-1000;
%particle_position
%particle_velocity
%mainparticleswrmroutine
max_iterations
%findthefitnessofeachparticle
%changefitnessfunctionasperequationrequiresdanddimensions
%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;
%decideonp_bestetcforeachparticle
ifcurrent_fitness(count_x)>
p_best_fitness(count_x)
p_best_fitness(count_x)=current_fitness(count_x);
%decideontheglobalbestamongalltheparticles
[g_best_val,g_best_index]=max(current_fitness);
%g_bestcontainsthepositionoftehglobalbest
g_best(count_y)=particle_position(g_best_index,count_y);
%updatethepositionandvelocitycompponents
p_current(count_y)=particle_position(count_x,count_y);
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);
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;
Pmax=[1010];
%maximummodelparametervalue
Pmin=[-10-10];
%minimummodelparametervalue
Vmax=[11];
%maximumchangeinmodelparameter
Vmin=[-1-1];
%minimumchangeinmodelparameter
modelparameters(1:
np,1:
ns)=0;
%setallmodelparameterestimatesforallmodelparametersetstozero
modelparameterchanges(1:
%setallchangeinmodelparameterestimatesforallmodelparametersetstozero
bestmodelparameters(1:
%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
fori=2:
iter
x=modelparameters(:
j);
%calculatecostfunction
costfunction=105*(x
(2)-x
(1)^2)^2+(1-x
(1))^2;
ifcostfunction<
setbestcostfunction(j)%bestcostfunctionforjthsetofmodelparameters
bestmodelparameters(:
j)=modelparameters(:
setbestcostfunction(j)=costfunction;
四、模拟退火算法
%ford=1:
50%循环10次发现最小路径为4.115,循环50次有3次出现4.115
T_max=80;
%input('
pleaseinputthestarttemprature'
T_min=0.001;
pleaseinputtheendtemprature'
iter_max=100;
%input('
pleaseinputthemostinterpstepsonthefittemp'
s_max=100;
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_num<
iter_max&
s_num<
s_max;
order2=exhgpath(order1);
%随机交换两个城市位置
totaldis2=distance(address,order2);
R=rand;
DeltaDis=totaldis2-totaldis1;
%新的距离-原来的距离
ifDeltaDis<
0;
order1=order2;
totaldis1=totaldis2;
%新距离小,无条件承受
elseif(exp((totaldis1-totaldis2)/(T))>
R)%%%%本算法最核心的思想:
以一定概率承受坏的结果,防止局部最优
elses_num=s_num+1;
iter_num=iter_num+1;
T=T*0.99;
set(gca,'
xscale'
log'
%或者使用semilogx,有一样效果
xlabel('
退火温度'
ylabel('
总距离'
order1
totaldis1
figure(3)
plot(address(order1,1),addr
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- matlab 常用 算法 大全
![提示](https://static.bdocx.com/images/bang_tan.gif)