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)
for i=2:n
z(i)=*(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-*u(1))/(1+*u(1))*lamda %计算级比偏差值
%以深圳人口数据得到预测模型及预测误差相关数据
lamda =
Columns 1 through 8
Columns 9 through 16
Columns 17 through 24
Columns 25 through 31
range =
x1 =
+003 *
Columns 1 through 8
Columns 9 through 16
Columns 17 through 24
Columns 25 through 32
u =
y =
+*exp(.664533e-1*t)
yuce =
Columns 1 through 8
Columns 9 through 16
Columns 17 through 24
Columns 25 through 32
epsilon =
Columns 1 through 8
0 Columns 9 through 16
Columns 17 through 24
Columns 25 through 32
delta =
Columns 1 through 8
Columns 9 through 16
Columns 17 through 24
Columns 25 through 32
rho =
Columns 1 through 8
Columns 9 through 16
Columns 17 through 24
Columns 25 through 31
二、遗传算法程序代码
% Optimizing a function using Simple Genetic Algorithm with elitist preserved
%Max f(x1,x2)=100*(x1*x1-x2).^2+(1-x1).^2; <=x1,x2<=
% Author: Wang Yonglin ()
clc;clear all;
format long;%设定数据显示格式
%初始化参数
T=100;%仿真代数
N=80;% 群体规模
pm=;pc=;%交叉变异概率
umax=;umin=;%参数取值范围
L=10;%单个参数字串长度,总编码长度2L bval=round(rand(N,2*L));%初始种群bestv=-inf;%最优适应度初值
%迭代开始
for ii=1:T
%解码,计算适应度
for i=1:N
y1=0;y2=0;
for j=1:1:L
y1=y1+bval(i,L-j+1)*2^(j-1);
end
x1=(umax-umin)*y1/(2^L-1)+umin;
for j=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);%求当代最佳个体
if fmax>=bestv
bestv=fmax;%到目前为止最优适应度值
bvalxx=bval(indmax,:);%到目前为止最佳位串optxx=xx(indmax,:);%到目前为止最优参数
end
Bfit1(ii)=bestv; % 存储每代的最优适应度%%%%遗传操作开始
%轮盘赌选择
for i=1:(N-1)
r=rand;
tmp=find(r<=q);
newbval(i,:)=bval(tmp(1),:);
end
newbval(N,:)=bvalxx;%最优保留
bval=newbval;
%单点交叉
for i=1:2:(N-1)
cc=rand;
if cc 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: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 %输出最优参数 三、种子群算法程序代码 %declare the parameters of the optimization max_iterations = 1000; no_of_particles = 50; dimensions = 1; delta_min = ; delta_max = ; c1 = ; c2 = ; %initialise the particles and teir velocity components for count_x = 1:no_of_particles for count_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 %initialize the p_best_fitness array for count = 1:no_of_particles p_best_fitness(count) = -1000; end %particle_position %particle_velocity %main particle swrm routine for count = 1:max_iterations %find the fitness of each particle %change fitness function as per equation requiresd and dimensions for count_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; if soln~=0 current_fitness(count_x) = 1/abs(soln); else current_fitness =1000; end end %decide on p_best etc for each particle for count_x = 1:no_of_particles if current_fitness(count_x) >p_best_fitness(count_x) p_best_fitness(count_x) = current_fitness(count_x); for count_y = 1:dimensions p_best(count_x,count_y) = particle_position(count_x,count_y); end end end %decide on the global best among all the particles [g_best_val,g_best_index] = max(current_fitness); %g_best contains the position of teh global best for count_y = 1:dimensions g_best(count_y) = particle_position(g_best_index,count_y); end %update the position and velocity compponents for count_x = 1:no_of_particles for count_y = 1:dimensions p_current(count_y) = particle_position(count_x,count_y); end for count_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) clear all, clc % pso example iter = 1000; % number of algorithm iterations np = 2; % number of model parameters ns = 10; % number of sets of model parameters Wmax = ; % maximum inertial weight Wmin = ; % minimum inertial weight c1 = ; % parameter in PSO methodology c2 = ; % parameter in PSO methodology Pmax = [10 10]; % maximum model parameter value Pmin = [-10 -10]; % minimum model parameter value Vmax = [1 1]; % maximum change in model parameter Vmin = [-1 -1]; % minimum change in model parameter modelparameters(1:np,1:ns) = 0; % set all model parameter estimates for all model parameter sets to zero modelparameterchanges(1:np,1:ns) = 0; % set all change in model parameter estimates for all model parameter sets to zero bestmodelparameters(1:np,1:ns) = 0; % set best model parameter estimates for all model parameter sets to zero setbestcostfunction(1:ns) = 1e6; % set best cost function of each model parameter set to a large number globalbestparameters(1:np) = 0; % set best model parameter values for all model parameter sets to zero bestparameters = globalbestparameters'; % best model parameter values for all model parameter sets (to plot) globalbestcostfunction = 1e6; % set best cost function for all model parameter sets to a large number i = 0; % indicates ith algorithm iteration j = 0; % indicates jth set of model parameters k = 0; % indicates kth model parameter for k = 1:np % initialization for j = 1:ns modelparameters(k,j) = (Pmax(k)-Pmin(k))*rand(1) + Pmin(k); % randomly distribute model parameters modelparameterchanges(k,j) = (Vmax(k)-Vmin(k))*rand(1) + Vmin(k); % randomly distribute change in model parameters end end for i = 2:iter for j = 1:ns x = modelparameters(:,j); % calculate cost function costfunction = 105*(x(2)-x(1)^2)^2 + (1-x(1))^2; if costfunction bestmodelparameters(:,j) = modelparameters(:,j); setbestcostfunction(j) = costfunction; end 四、模拟退火算法 % for d=1:50 %循环10次发现最小路径为,循环50次有3次出现 T_max=80; %input('please input the start temprature'); T_min=; %input('please input the end temprature'); iter_max=100;%input('please input the most interp steps on the fit temp'); s_max=100; %input('please input the most steady steps ont the fit temp'); T=T_max; load .\; order1=randperm(size(address,1))';%生成初始解。 figure(1); plot(address(order1,1),address(order1,2),'*r-') title('随机产生的路径'); totaldis1=distance(address,order1); for n=1:size(address,1) text(address(n,1)+,address(n,2),num2str(n))%标号 end text,,num2str(totaldis1)) figure(2); while T>=T_min iter_num=1; s_num=1; plot(T,totaldis1,'r.') hold on while iter_num order2=exhgpath(order1); %随机交换两个城市位置 totaldis2=distance(address,order2); R=rand; DeltaDis=totaldis2-totaldis1; %新的距离-原来的距离 if DeltaDis<0; order1=order2; totaldis1=totaldis2; %新距离小,无条件接受 elseif (exp((totaldis1-totaldis2)/(T))>R)%%%%本算法最核心的思想:以一定概率接受坏的结果,防止局部最优 order1=order2; totaldis1=totaldis2; else s_num=s_num+1; end iter_num=iter_num+1; end T=T*; end set(gca,'xscale','log');%或者使用semilogx,有相同效果 xlabel('退火温度');ylabel('总距离'); order1 totaldis1 figure(3) plot(address(order1,1),address(order1,2),'*b-') title('最终路径'); for n=1:size(address,1) text(address(n,1)+,address(n,2),num2str(n))%标号 end text,,num2str(totaldis1)) dstc(d)=totaldis1; % end ——————————————————————————————function y=exhgpath(order) while 1 b=size(order,1); r=unidrnd(b,1,2); if r(1)-r(2)~=0 break end end b=order(r(2)); order(r(2))=order(r(1)); order(r(1))=b; y=order; ------------------------------------------------------------ function y=distance(address,order) nmb=size(address,1); y=0; for i=1:nmb-1 y=y+sqrt((address(order(i+1),1)-address(order(i),1))^2+(address(order (i+1),2)-address(order(i),2))^2); end y=y+sqrt((address(order(i+1),1)-address(order(1),1))^2+(address(order (i+1),2)-address(order(1),2))^2); 人工神经网络程序代码 %产生指定类别的样本点,并在图中绘出 X = [0 1; 0 1]; % 限制类中心的范围 clusters = 5; % 指定类别数目 points = 10; % 指定每一类的点的数目 std_dev = ; % 每一类的标准差 P = nngenc(X,clusters,points,std_dev); plot(P(1,:),P(2,:),'+r'); title('输入样本向量');