天天看点

MAT之GA:利用GA对一元函数进行优化过程,求x∈(0,10)中y的最大值

输出结果

MAT之GA:利用GA对一元函数进行优化过程,求x∈(0,10)中y的最大值

代码设计

x = 0:0.01:10;

y =  x + 10*sin(5*x)+7*cos(4*x);

figure

plot(x, y)

xlabel('independent variable')

ylabel('dependent variable')

title('GA:y = x + 10*sin(5*x) + 7*cos(4*x)利用算法求解最优解—Jason niu')

initPop = initializega(50,[0 10],'fitness');

[x endPop bpop trace] = ga([0 10],'fitness',[],initPop,[1e-6 1 1],'maxGenTerm',25,...

                          'normGeomSelect',0.08,'arithXover',2,'nonUnifMutation',[2 25 3]);

x

hold on

plot (endPop(:,1),endPop(:,2),'ro')

figure(2)

plot(trace(:,1),trace(:,3),'b:')

title('GA算法的迭代进化曲线—Jason niu')

plot(trace(:,1),trace(:,2),'r-')

xlabel('Generation'); ylabel('Fittness');

legend('Mean Fitness', 'Best Fitness')

继续阅读