% Quasi-Newton least-squares method in MATLAB
% Define the objective function function f = objective_function(x) f = 4x(1)^2 + 3x(2)^2 + 2x(1)x(2) - 8x(1) - 9x(2) + 5; end
% Define the gradients of the objective function function g = gradient(x) g = [8x(1) + 2x(2) - 8; 6x(2) + 2x(1) - 9]; end
% Define the initial guess x0 = [0; 0];
% Define the tolerance for convergence tolerance = 1e-6;
% Initialize the Hessian as Identity matrix H = eye(length(x0));
x = x0; while true g = gradient(x); if norm(g) < tolerance break; end
main.m345 chars15 lines
end
disp('Optimal solution:'); disp(x);
disp('Optimal function value:'); disp(objective_function(x));
% Function for backtracking line search function alpha = backtracking_line_search(f, grad, x, p) alpha = 1; rho = 0.5; c = 0.1;
main.m86 chars4 lines
end
main.m0 chars1 lines
gistlibby LogSnag