# 求解非约束优化问题的拟牛顿方法（BFGS、DFP）

2018-06-04 03:28:08         来源：lusongno1的博客

## 求解非约束优化问题的拟牛顿方法（BFGS、DFP）

### 程序代码

```clc
clear

f = @(t)t(1)^2+2*t(2)^2;
x0 = [10,10]';
epsilon = 0.001;
H0 = eye(2);
method = 'BFGS';%DFP
x = quasi_Newton(f,x0,epsilon,H0,method);

function [xk,k] = quasi_Newton(f,x0,epsilon,H0,method)
%使用：quasi_Newton(f,x0,method)

if nargin < 3
help mfilename;
end
k = 0;
syms t1 t2;
t = [t1,t2]';
fs = f(t);
df = matlabFunction(dfs);
df = @(x) df(x(1),x(2));
df0 = df(x0);
normdf = sqrt(df0'*df0);
H = H0;
xk = x0;
dfk = df0;
while normdf > epsilon
p = -H*dfk;
alpha = cal_alpha(H,dfk);
xk1 = xk + alpha*p;
dfk1 = df(xk1);
sk = xk1 - xk;
yk = dfk1 - dfk;
eval(['H = ' method '(H,sk,yk);']);
%H = BFGS(H,sk,yk);
k = k + 1;
xk = xk1;
dfk = dfk1;
normdf = sqrt(dfk'*dfk);
xk
end
end
function alpha = cal_alpha(H,dfk)
%alpha = dfk'*H*dfk/(dfk'*H'*dfk);
alpha = 1;
end
function H = BFGS(H,sk,yk)
gammak = 1/(yk'*sk);
skykT = sk * yk';
skskT = sk * sk';
E = eye(2);
H = (E-gammak*skykT)*H*(E-gammak*skykT) + gammak*skskT;
end
function H = DFP(H,sk,yk)
Hyk = H*yk;
ykTsk =  yk'*sk;
skskT = sk * sk';
H = H - (Hyk*yk'*H)/(yk'*Hyk) + skskT/ykTsk;
end

```