function [x_vals, f_vals, k] = steepest_descent(f, grad_f, x0, tol, max_iter, mode) % f: Objective function % grad_f: Gradient of the function % x0: Initial point [x0, y0] % tol: Tolerance for stopping criterion % max_iter: Maximum number of iterations % x_vals: Vector with the (x,y) values until minimum % f_vals: Vector with f(x,y) values until minimum % k: Number of iterations if strcmp(mode, 'armijo') == 1 gamma_f = @(f, grad_f, x0) gamma_armijo(f, grad_f, x0); elseif strcmp(mode, 'minimized') == 1 gamma_f = @(f, grad_f, x0) gamma_minimized(f, grad_f, x0); else % mode == 'fixed' gamma_f = @(f, grad_f, x0) gamma_fixed(f, grad_f, x0); end % Storage for iterations, begin with the first point x_vals = x0; f_vals = f(x0(1), x0(2)); for k = 1:max_iter grad = grad_f(x0(1), x0(2)); % Check for convergence if norm(grad) < tol break; end dk = - grad; % Calculate gamma gk = gamma_f(f, grad_f, x0); x_next = x0 + gk * dk'; % Update step f_next = f(x_next(1), x_next(2)); x0 = x_next; % Update point x_vals = [x_vals; x_next]; % Store values f_vals = [f_vals; f_next]; % Store function values end end