www.gusucode.com > globaloptim 案例源码程序 matlab代码 > globaloptim/MinimizeaSimpleFunctionExample.m

    %% Minimize a Simple Function  
% Minimize a simple function of two variables.   

%% 
% Define the objective function. 
fun = @(x)x(1)*exp(-norm(x)^2);  

%% 
% Call |particleswarm| to minimize the function. 
rng default  % For reproducibility
nvars = 2;
x = particleswarm(fun,nvars)  

%% 
% This solution is far from the true minimum, as you see in a function plot. 
fsurf(@(x,y)x.*exp(-(x.^2+y.^2)))     

%% 
% Usually, it is best to set bounds. See <docid:gads.bueff_f-2>.