×

遗传算法matlab matlab 遗传算法

matlab 遗传算法?matlab画出的曲线怎么拟合函数

admin admin 发表于2022-04-27 21:59:41 浏览161 评论0

抢沙发发表评论

matlab 遗传算法

function m_main()clearclcMax_gen=100;% 运行代数pop_size=100;%种群大小chromsome=10;%染色体的长度pc=0.9;%交叉概率pm=0.25;%变异概率gen=0;%统计代数%初始化init=40*rand(pop_size,chromsome)-20;pop=init; fit=obj_fitness(pop); [max_fit,index_max]=max(fit);maxfit=max_fit; [min_fit,index_min]=min(fit);best_indiv=pop(index_max,:);%迭代操作while gen《Max_gen gen=gen+1; bt(gen)=max_fit; if maxfit《max_fit;maxfit=max_fit;pop(index_min,:)=pop(index_max,:);best_indiv=pop(index_max,:);end best_indiv_tmp(gen)=pop(index_max); newpop=ga(pop,pc,pm,chromsome,fit); fit=obj_fitness(newpop); [max_fit,index_max]=max(fit); [min_fit,index_min]=min(fit); pop=newpop; trace(1,gen)=max_fit; trace(2,gen)=sum(fit)./length(fit);end %运行结果[f_max gen_ct]=max(bt)%求的最大值以及代数maxfitbest_indiv%画图% bthold on plot(trace(1,:),’.g:’); plot( trace(2,:),’.r-’);title(’实验结果图’)xlabel(’迭代次数/代’),ylabel(’最佳适应度(最大值)’);%坐标标注plot(gen_ct-1,0:0.1:f_max+1,’c-’);%画出最大值text(gen_ct,f_max+1, ’最大值’)hold offfunction [fitness]=obj_fitness(pop)%适应度计算函数[r c]=size(pop);x=pop;fitness=zeros(r,1);for i=1:r for j=1:c fitness(i,1)=fitness(i,1)+sin(sqrt(abs(40*x(i))))+1-abs(x(i))/20.0; endendfunction newpop=ga(pop,pc,pm,chromsome,fit);pop_size=size(pop,1);%轮盘赌选择 ps=fit/sum(fit); pscum=cumsum(ps);%size(pscum) r=rand(1,pop_size);qw=pscum*ones(1,pop_size); selected=sum(pscum*ones(1,pop_size)《ones(pop_size,1)*r)+1; newpop=pop(selected,:);%交叉if pop_size/2~=0 pop_size=pop_size-1;end for i=1:2:pop_size-1 while pc》rand c_pt=round(8*rand+1); pop_tp1=newpop(i,:);pop_tp2=newpop(i+1,:); newpop(i+1,1:c_pt)=pop_tp1(1,1:c_pt); newpop(i,c_pt+1:chromsome)=pop_tp2(1,c_pt+1:chromsome); end end% 变异 for i=1:pop_size if pm》rand m_pt=1+round(9*rand); newpop(i,m_pt)=40*rand-20; end end

matlab画出的曲线怎么拟合函数

拟合步骤:1、求(获)得一系列x,y对应值x=[...]y=[...]2、根据画出的曲线,,设定拟合函数fun=inline(’a(1)+a(2)*exp(a(3)*x’,’a’,’x’)3、初定x0的初值x0=[0 0 0]4、用拟合函数求出拟合系数a=lsqcurvefit(fun,x0,x,y) 或 a= nlinfit(x,y,fun,x0)用cftool的结果与实际是有较大的误差。你不仿用二种获得的拟合函数,将已知值x代人,得到的yi,那个更接近已知值y。一般用cftool工具箱,来判断拟合函数可能的形式。

Matlab中的axis是什么意思

坐标轴的控制函数axis,调用格式如下: axis([xmin,xmax,ymin,ymax,zmin,zmax]) 用此命令可以控制坐标轴的范围. 与axis相关的几条常用命令还有: axis auto 自动模式,使得图形的坐标范围满足图中一切图元素 axis equal 严格控制各坐标的分度使其相等 axis square 使绘图区为正方形 axis on 恢复对坐标轴的一切设置 axis off 取消对坐标轴的一切设置 axis manual 以当前的坐标限制图形的绘制希望对你有帮助