code the "quasi-newton least-squares method" using basic matlab commands in matlab

% 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.m
p = -H * g;  % Computing the search direction

% Updating the solution using line search
alpha = backtracking_line_search(@objective_function, @gradient, x, p);

% Update x
x_new = x + alpha * p;

% Update H (BFGS update formula)
s = x_new - x;
y = gradient(x_new) - g;
H = H + (s * s') / (s' * y) - (H * y * y' * H) / (y' * H * y);

x = x_new;
345 chars
15 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.m
while f(x + alpha * p) > f(x) + c * alpha * grad(x)' * p
    alpha = rho * alpha;
end
86 chars
4 lines

end

main.m
0 chars
1 lines

related categories

gistlibby LogSnag