工程数学二
一、实验内容
(1)求解无约束优化问题:f(#)=(x+10x)+5(x;-x)+(x-2x)+10-x);
(2)终止准则职|(x^) 10-“;
(3)完成最速下降法(负梯度法)的 MATLAB 编程、调试;
(4)要求选取多个不同的初始点,并给出选代次数,最优函数值等相关信息,有能力的同学尝试画出最优值随选代次数变化的曲线图;
(5)按照模板撰写实验报告,要求规范整洁。
2. 代码
f = @(x) x(1)-x(2)+2*x(1)^2 + 2*x(2)*x(1)+x(2)^2;
grad_f = @(x) [1+4*x(1)+2*x(2); -1+2*x(1)+2*x(2)];
initial_points = [0, 0;];
tol = 0.01;
results = cell(size(initial_points, 1), 1);
for i = 1:size(initial_points, 1)
x0 = initial_points(i, :)';
[x_opt, f_opt, iter, history] = steepest_descent_method(f, grad_f, x0, tol);
results{i} = struct('x0', x0, 'x_opt', x_opt, 'f_opt', f_opt, 'iter', iter, 'history', history);
fprintf('初始点 : (%.2f, %.2f), 极小点: (%.6f, %.6f), 极小值: %.6f, 迭代次数: %d\n', ...
x0(1), x0(2), x_opt(1), x_opt(2), f_opt, iter);
end
function [x_opt, f_opt, iter, history] = steepest_descent_method(f, grad_f, x0, tol)
max_iter = 10000;
iter = 0;
x = x0;
history = [];
while norm(grad_f(x)) > tol && iter < max_iter
d = -grad_f(x);
alpha = line_search(f, grad_f, x, d);
x = x + alpha * d;
iter = iter + 1;
history = [history; f(x)];
end
x_opt = x;
f_opt = f(x);
end
function alpha = line_search(f, grad_f, x, d)
alpha = 1;
rho = 0.5;
c = 1e-4;
while f(x + alpha * d) > f(x) + c * alpha * (grad_f(x)' * d)
alpha = rho * alpha;
end
fprintf("步长因子%.6f\n",alpha);
end
figure;
hold on;
for i = 1:length(results)
plot(results{i}.history, 'DisplayName', sprintf('Initial point: (%.2f, %.2f)', results{i}.x0(1), results{i}.x0(2)));
end
hold off;
xlabel('Iteration');
ylabel('Objective Function Value');
title('Objective Function Value vs. Iteration');
legend show;