ex0406.m

来自「基于MATLAB的微粒群工具箱」· M 代码 · 共 29 行

M
29
字号
function [optx,fval] = simplex2
bounds = ones(10,1) * [-5.12 5.12];
numvars = size(bounds,1);
rng = (bounds(:,2)-bounds(:,1))';
options = optimset('LargeScale','off');
options = optimset(options,'MaxFunEvals',500);
for i = 1 : 100
    x0 = rng.*(rand(1,numvars)) + bounds(:,1)';
    [x,lbest,exitflag] = fminsearch(@f,x0,options);
    if(i == 1)
        fval = lbest;
        optx = x;
    else
        fval = min(fval,lbest);
        if(fval == lbest)
            optx = x;
        end
    end
end
return

function y = f(x)
len=length(x);A=8;
y=0;
for i=1:len
    y=y+x(i)^2-A*cos(2*pi*x(i));
end
y=y+len*A;
return

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?