编写matlab程序使用最速下降算法、阻尼牛顿法、BFGS方法对以 下几类不同的目标函数求解无约束优化问题
时间: 2023-12-14 17:40:15 浏览: 84
以下是使用 MATLAB 实现最速下降算法、阻尼牛顿法、BFGS 方法求解无约束优化问题的示例代码:
目标函数 1: Rosenbrock 函数
$$f(x,y) = (1-x)^2+100(y-x^2)^2$$
最速下降算法:
```matlab
syms x y
f = (1-x)^2+100*(y-x^2)^2;
grad = gradient(f, [x,y]);
x0 = [0;0];
alpha = 1e-3;
tol = 1e-6;
max_iter = 1000;
iter = 1;
while iter <= max_iter
g = double(subs(grad, [x,y], x0));
if norm(g) < tol
break;
end
d = -g;
x1 = x0 + alpha*d;
if norm(x1-x0) < tol
break;
end
x0 = x1;
iter = iter + 1;
end
disp(['Minimum point: (',num2str(x0(1)),',',num2str(x0(2)),')']);
disp(['Minimum value: ',num2str(double(subs(f, [x,y], x0)))]);
```
阻尼牛顿法:
```matlab
syms x y
f = (1-x)^2+100*(y-x^2)^2;
grad = gradient(f, [x,y]);
hess = hessian(f, [x,y]);
x0 = [0;0];
alpha = 1;
tol = 1e-6;
max_iter = 1000;
iter = 1;
while iter <= max_iter
g = double(subs(grad, [x,y], x0));
H = double(subs(hess, [x,y], x0));
if norm(g) < tol
break;
end
d = -inv(H+alpha*eye(2))*g;
x1 = x0 + d;
if norm(x1-x0) < tol
break;
end
if double(subs(f, [x,y], x1)) < double(subs(f, [x,y], x0))
alpha = alpha/10;
x0 = x1;
else
alpha = alpha*10;
end
iter = iter + 1;
end
disp(['Minimum point: (',num2str(x0(1)),',',num2str(x0(2)),')']);
disp(['Minimum value: ',num2str(double(subs(f, [x,y], x0)))]);
```
BFGS 方法:
```matlab
syms x y
f = (1-x)^2+100*(y-x^2)^2;
grad = gradient(f, [x,y]);
H = eye(2);
x0 = [0;0];
tol = 1e-6;
max_iter = 1000;
iter = 1;
while iter <= max_iter
g = double(subs(grad, [x,y], x0));
d = -H*g;
if norm(g) < tol
break;
end
alpha = 1;
while double(subs(f, [x,y], x0+alpha*d)) > double(subs(f, [x,y], x0))+0.5*alpha*transpose(g)*d
alpha = alpha/2;
end
s = alpha*d;
x1 = x0 + s;
y = double(subs(grad, [x,y], x1)) - g;
rho = 1 / (transpose(s)*y);
H = (eye(2)-rho*s*y')*H*(eye(2)-rho*y*s')+rho*s*s';
x0 = x1;
iter = iter + 1;
end
disp(['Minimum point: (',num2str(x0(1)),',',num2str(x0(2)),')']);
disp(['Minimum value: ',num2str(double(subs(f, [x,y], x0)))]);
```
目标函数 2: Booth 函数
$$f(x,y) = (x+2y-7)^2+(2x+y-5)^2$$
最速下降算法:
```matlab
syms x y
f = (x+2*y-7)^2+(2*x+y-5)^2;
grad = gradient(f, [x,y]);
x0 = [0;0];
alpha = 1e-3;
tol = 1e-6;
max_iter = 1000;
iter = 1;
while iter <= max_iter
g = double(subs(grad, [x,y], x0));
if norm(g) < tol
break;
end
d = -g;
x1 = x0 + alpha*d;
if norm(x1-x0) < tol
break;
end
x0 = x1;
iter = iter + 1;
end
disp(['Minimum point: (',num2str(x0(1)),',',num2str(x0(2)),')']);
disp(['Minimum value: ',num2str(double(subs(f, [x,y], x0)))]);
```
阻尼牛顿法:
```matlab
syms x y
f = (x+2*y-7)^2+(2*x+y-5)^2;
grad = gradient(f, [x,y]);
hess = hessian(f, [x,y]);
x0 = [0;0];
alpha = 1;
tol = 1e-6;
max_iter = 1000;
iter = 1;
while iter <= max_iter
g = double(subs(grad, [x,y], x0));
H = double(subs(hess, [x,y], x0));
if norm(g) < tol
break;
end
d = -inv(H+alpha*eye(2))*g;
x1 = x0 + d;
if norm(x1-x0) < tol
break;
end
if double(subs(f, [x,y], x1)) < double(subs(f, [x,y], x0))
alpha = alpha/10;
x0 = x1;
else
alpha = alpha*10;
end
iter = iter + 1;
end
disp(['Minimum point: (',num2str(x0(1)),',',num2str(x0(2)),')']);
disp(['Minimum value: ',num2str(double(subs(f, [x,y], x0)))]);
```
BFGS 方法:
```matlab
syms x y
f = (x+2*y-7)^2+(2*x+y-5)^2;
grad = gradient(f, [x,y]);
H = eye(2);
x0 = [0;0];
tol = 1e-6;
max_iter = 1000;
iter = 1;
while iter <= max_iter
g = double(subs(grad, [x,y], x0));
d = -H*g;
if norm(g) < tol
break;
end
alpha = 1;
while double(subs(f, [x,y], x0+alpha*d)) > double(subs(f, [x,y], x0))+0.5*alpha*transpose(g)*d
alpha = alpha/2;
end
s = alpha*d;
x1 = x0 + s;
y = double(subs(grad, [x,y], x1)) - g;
rho = 1 / (transpose(s)*y);
H = (eye(2)-rho*s*y')*H*(eye(2)-rho*y*s')+rho*s*s';
x0 = x1;
iter = iter + 1;
end
disp(['Minimum point: (',num2str(x0(1)),',',num2str(x0(2)),')']);
disp(['Minimum value: ',num2str(double(subs(f, [x,y], x0)))]);
```
目标函数 3: Himmelblau 函数
$$f(x,y) = (x^2+y-11)^2+(x+y^2-7)^2$$
最速下降算法:
```matlab
syms x y
f = (x^2+y-11)^2+(x+y^2-7)^2;
grad = gradient(f, [x,y]);
x0 = [0;0];
alpha = 1e-3;
tol = 1e-6;
max_iter = 1000;
iter = 1;
while iter <= max_iter
g = double(subs(grad, [x,y], x0));
if norm(g) < tol
break;
end
d = -g;
x1 = x0 + alpha*d;
if norm(x1-x0) < tol
break;
end
x0 = x1;
iter = iter + 1;
end
disp(['Minimum point: (',num2str(x0(1)),',',num2str(x0(2)),')']);
disp(['Minimum value: ',num2str(double(subs(f, [x,y], x0)))]);
```
阻尼牛顿法:
```matlab
syms x y
f = (x^2+y-11)^2+(x+y^2-7)^2;
grad = gradient(f, [x,y]);
hess = hessian(f, [x,y]);
x0 = [0;0];
alpha = 1;
tol = 1e-6;
max_iter = 1000;
iter = 1;
while iter <= max_iter
g = double(subs(grad, [x,y], x0));
H = double(subs(hess, [x,y], x0));
if norm(g) < tol
break;
end
d = -inv(H+alpha*eye(2))*g;
x1 = x0 + d;
if norm(x1-x0) < tol
break;
end
if double(subs(f, [x,y], x1)) < double(subs(f, [x,y], x0))
alpha = alpha/10;
x0 = x1;
else
alpha = alpha*10;
end
iter = iter + 1;
end
disp(['Minimum point: (',num2str(x0(1)),',',num2str(x0(2)),')']);
disp(['Minimum value: ',num2str(double(subs(f, [x,y], x0)))]);
```
BFGS 方法:
```matlab
syms x y
f = (x^2+y-11)^2+(x+y^2-7)^2;
grad = gradient(f, [x,y]);
H = eye(2);
x0 = [0;0];
tol = 1e-6;
max_iter = 1000;
iter = 1;
while iter <= max_iter
g = double(subs(grad, [x,y], x0));
d = -H*g;
if norm(g) < tol
break;
end
alpha = 1;
while double(subs(f, [x,y], x0+alpha*d)) > double(subs(f, [x,y], x0))+0.5*alpha*transpose(g)*d
alpha = alpha/2;
end
s = alpha*d;
x1 = x0 + s;
y = double(subs(grad, [x,y], x1)) - g;
rho = 1 / (transpose(s)*y);
H = (eye(2)-rho*s*y')*H*(eye(2)-rho*y*s')+rho*s*s';
x0 = x1;
iter = iter + 1;
end
disp(['Minimum point: (',num2str(x0(1)),',',num2str(x0(2)),')']);
disp(['Minimum value: ',num2str(double(subs(f, [x,y], x0)))]);
```
阅读全文