fitype拟合多参数函数和遗传算法拟合多参数函数

多参数拟合函数最重要的是初始点。初始点的选择可以靠直觉和经验,也可以通过遗传算法从大范围逐步的逼近。

基本语法如下:

这是函数:z=a*(x^b)*(y^c)的函数的拟合。x,y是自变量,z是应变量,a,b,c是拟合的参数。

fit的语法是:fit(自变量,应变量,fittype的返回值,’Startpoint’,参数的初始值),它的返回值是三个参数的值。

ft = fittype( @(a, b, c, x, y) a*(x.^b).*(y.^c), 'independent', {'x', 'y'},...
     'dependent', 'z' );

fit([x(:),y(:)],z(:),ft,'Startpoint',[0.1,0.1,0.1])

以下是范例:

增加生产、发展经济所依靠的主要因素有增加投资、增加劳动力以及技术革新等,在研究国民经济产值与这些因素的数量关系时,由于技术水平不像资金、劳动力那样容易定量化,作为初步的模型,可认为技术水平不变,只讨论产值和资金、劳动力之间的关系。在科学技术发展不快时,如资本主义经济发展的前期,这种模型是有意义的。

Q,K,L分别表示产值、资金、劳动力,要寻求的数量关系Q(K,L)。经过简化假设与分析,在经济学中,推导出一个著名的Cobb-Douglas生产函数:

Q(K,L) = aKαLβ,  0<α,β<1           (*)

式中α,β,a要由经济统计数据确定。现有美国马萨诸塞州1900—1926年上述三个经济指数的统计数据,如下表,试用数据拟合的方法,求出式(*)中的参数α,β,a

 

表1

  t       Q         K         L

   t       Q         K         L

1900    1.05       1.04      1.05

1901    1.18       1.06      1.08

1902    1.29       1.16      1.18

1903    1.30       1.22      1.22

1904    1.30       1.27      1.17

1905    1.42       1.37      1.30

1906    1.50       1.44      1.39

1907    1.52       1.53      1.47

1908    1.46       1.57      1.31

1909    1.60       2.05      1.43

1910    1.69       2.51      1.58

1911    1.81       2.63      1.59

1912    1.93       2.74      1.66

1913    1.95       2.82      1.68

 

 

 1914    2.01      3.24      1.65

 1915    2.00      3.24      1.62

 1916    2.09      3.61      1.86

 1917    1.96      4.10      1.93

 1918    2.20      4.36      1.96

 1919    2.12      4.77      1.95

 1920    2.16      4.75      1.90

 1921    2.08      4.54      1.58

 1922    2.24      4.54      1.67

 1923    2.56      4.58      1.82

 1924    2.34      4.58      1.60

 1925    2.45      4.58      1.61

 1926    2.58      4.54      1.64

拟合代码如下:

%lab2由4列数据组成,年份,K,L和Q4列,对应上诉表格数据。

x=lab2(:,2)’;

y=lab2(:,3)’;

z=lab2(:,4)’;

ft = fittype( @(a, b, c, x, y) a*(x.^b).*(y.^c), ‘independent’, {‘x’, ‘y’},…

     ‘dependent’, ‘z’ );

fit([x(:),y(:)],z(:),ft,’Startpoint’,[0.1,0.1,0.1])

 

二、遗传算法拟合

《fitype拟合多参数函数和遗传算法拟合多参数函数》

%主函数%

M = 20;          %种群个数
C = 20000;          %迭代次数
m = 2;           %适应值归一淘汰加速指数
Pmutation = 0.4; %变异概率
Pc = 0.4;        %交叉概率
pop=round(rand(30,45)); %种群

%%%%初始化种群及其适应函数%%%%

[dePop]=decode(pop,lab3,lab2);
fitness=fit(dePop);
maxfitness=min(fitness);
rr=find(fitness==maxfitness);
R=pop(rr(1,1),:);
fprintf('当前种群最优个体:%.12f\n',fitness(rr));

while C>=0
    fprintf('迭代第%d次\n',C);
    
    %%%%选择操作%%%%
     [px,py]=size(pop);
     ms=sort(rand(px));
      fitin=1;
    nn=1;
    while nn<=px
        if (ms(nn))<fitness(fitin)
            pop_sel(nn,:)=pop(fitin,:);
            nn=nn+1;
            fitin=fitin+1;
             if fitin>px
                fitin=floor(rand*(px-1))+1;
            end
        else
            fitin=fitin+1;
            if fitin>px
                fitin=floor(rand*(px-1))+1;
            end
        end
    end
    
      %%%%交叉操作%%%%
    nnper=randperm(M);
    A=pop_sel(nnper(1),:);
    B=pop_sel(nnper(2),:);
    for i=1:M*Pc
        [A,B]=cross(A,B);
        pop_sel(nnper(1),:)=A;
        pop_sel(nnper(2),:)=B;
    end
    
        %%%%变异操作%%%%
    for i=1:M
        pick=rand;
        while pick==0
            pick=rand;
        end
        if pick<=Pmutation
            pop_sel(i,:)=Mutation(pop_sel(i,:),0.4);
        end
    end
    
  %%%%保留最优个体%%%%
    maxfitness=min(fitness);
    rr=find(fitness==maxfitness);
    pop_sel(1,:)=pop(rr(1),:);
    
    
    %%%%求适应度函数%%%%
    pop=pop_sel;
    [dePop]=decode(pop,lab3,lab2);
    fitness=fit(dePop);
    maxfitness=min(fitness);
    rr=find(fitness==maxfitness);
    R=-fitness(rr(1));
    fprintf('当前最小值=%.14f ',fitness(rr(1)));
    C=C-1;
end
plot(lab2(:,1),lab2(:,4),'b');
hold on;
result=dePop(1,2)*(lab2(:,2).^dePop(1,3)).*(lab2(:,3).^dePop(1,4));
plot(lab2(:,1),result,'k');

%这是fittype的拟合图像
a1= 1.195;b1=-0.1044;c1=0.3273;
plot(lab2(:,1),lab2(:,4),'b');
hold on;
result=a1*(lab2(:,2).^b1).*(lab2(:,3).^c1);
dePop=0;
for j=1:length(lab3(:,1))
      dePop=dePop+(lab3(j,4)-log(a1)-b1*lab3(j,2)-c1*lab3(j,3))^2;
end
fprintf('fittype的拟合结果:%.14f',dePop);
plot(lab2(:,1),result,'y');

%解码函数%
%遗传算法解码
%dePop返回值包含最小二乘法的结果,以及参数α,β,a
function [dePop]=decode(pop,lab3,lab2)
[lengthx,~]=size(pop);
dePop=zeros(lengthx,4);
for i=1:lengthx
    a=pop(i,1:15);
    b=pop(i,16:30);
    c=pop(i,31:45);
    %a1是第一个参数的值,b1是第二个参数的值,c1是第三个参数的值
    for j=1:15
        if j==1
            a1=1^a(1);
        else
            a1=a1+2^(2-j)*a(j);
        end
    end
    
    for j=1:15
        if j==1
            b1=(-1)^b(1);
        else
            b1=b1+2^(2-j)*b(j);
        end
    end
    
    for j=1:15
        if j==1
            c1=(-1)^c(1);
        else
            c1=c1+2^(2-j)*c(j);
        end
    end
    %求最小二乘法的目标的值,同时返回结果
    %lab3是lab2求对数的值
    for j=1:length(lab3(:,1))
      dePop(i)=dePop(i)+(lab3(j,4)-log(a1)-b1*lab3(j,2)-c1*lab3(j,3))^2;
    end
    dePop(i,2)=a1;
    dePop(i,3)=b1;
    dePop(i,4)=c1;
end


%适应度函数%
function fitness=fit(dePop)
   fitness=dePop(:,1);
end


%交叉函数%
%交叉操作
function [A,B]=cross(A,B)
  L=length(A);
  if L<10
      W=L;
  elseif((L/10)-floor(L/10))>=rand&&L>10
      W=ceil(L/10)+8;
  else
      W=floor(L/10)+8;
  end
  p=unidrnd(L-W+1);
  for i=1:W
      [A(1,p+i-1),B(1,p+i-1)]=exchange(A(1,p+i-1),B(1,p+i-1));
  end
end


%染色体的局部交换函数%
function [x,y]=exchange(x,y)
temp=x;
x=y;
y=temp;
end

%变异函数
function a=Mutation(A,fitness)
  nnper=randperm(size(A,2));
  if (A(1,nnper(1)))==1
      A(1,nnper(1))=0;
  elseif(A(1,nnper(1))==0)&(rand>fitness)
      A(1,nnper(1))=1;
  End
  a=A;
end

在我的遗传算法里面选择操作是欠妥的。

 

 

三、结果分析

《fitype拟合多参数函数和遗传算法拟合多参数函数》

《fitype拟合多参数函数和遗传算法拟合多参数函数》

 

四、评价与分析:

《fitype拟合多参数函数和遗传算法拟合多参数函数》

 

 

 

 

 

 

 

 

    原文作者:遗传算法
    原文地址: https://blog.csdn.net/cysisu/article/details/82960432
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞