Fix Work 2 directory and links

This commit is contained in:
Christos Choutouridis
2025-03-05 10:53:15 +02:00
parent feea941d95
commit c39b5382f6
39 changed files with 9 additions and 2 deletions
+33
View File
@@ -0,0 +1,33 @@
% Given environment
clear;
% Setup the function under test
syms x y;
fexpr = x^5 * exp(-x^2 - y^2);
title_fun = "$f(x,y) = x^5 \cdot e^{-x^2 - y^2}$";
% Calculate the gradient and Hessian
grad_fexpr = gradient(fexpr, [x, y]); % Gradient of f
hessian_fexpr = hessian(fexpr, [x, y]); % Hessian of f
% Convert symbolic expressions to MATLAB functions
fun = matlabFunction(fexpr, 'Vars', [x, y]); % Function
grad_fun = matlabFunction(grad_fexpr, 'Vars', [x, y]); % Gradient
hessian_fun = matlabFunction(hessian_fexpr, 'Vars', [x, y]); % Hessian
% Minimum reference
Freference = @(x) x(1).^5 .* exp(-x(1).^2 - x(2).^2);
[Xmin, Fmin] = fminsearch(Freference, [-1, -1]);
% Amijo globals
global amijo_beta; % Step reduction factor in [0.1, 0.5] (typical range: [0.1, 0.8])
global amijo_sigma; % Sufficient decrease constant in [1e-5, 0.1] (typical range: [0.01, 0.3])
%fixed step size globals
global gamma_fixed_step
global image_width,
global image_height;
image_width = 960;
image_height = 640;
+14
View File
@@ -0,0 +1,14 @@
% Define environment (functions, gradients etc...)
GivenEnv
%
% We plot the function in the domain of x,y in [-3, 3].
% We also plot the contour in order to get a sense of the min and maximum
% points in the x-y plane
%
% 3d plot the function
plot3dFun(fun, [-3, 3], [-3, 3], 100, title_fun, "figures/Plot_Function.png");
% Plot isobaric lines
plotContour(fun, [-3, 3], [-3, 3], 100, title_fun, "figures/Plot_Contour.png");
+116
View File
@@ -0,0 +1,116 @@
% Define environment (functions, gradients etc...)
GivenEnv
% Define parameters
max_iter = 300; % Maximum iterations
tol = 1e-4; % Tolerance
% Point x0 = (0, 0)
% =========================================================================
point = 1;
x0 = [0, 0];
f = fun(x0(1), x0(2));
gf = grad_fun(x0(1), x0(2));
hf = hessian_fun(x0(1), x0(2));
fprintf('Initial point (%d, %d), f = %f, grad = [%f;%f], hessian = [%f %f ; %f %f]. Can NOT use method\n', x0, f, gf, hf);
disp(' ');
% Point x0 = (-1, 1)
% =========================================================================
point = 2;
x0 = [-1, 1];
point_str = "[" + x0(1) + ", " + x0(2) + "]";
f = fun(-1, 1);
gf = grad_fun(x0(1), x0(2));
hf = hessian_fun(x0(1), x0(2));
fprintf('Initial point (%d, %d), f = %f, grad = [%f;%f], hessian = [%f %f ; %f %f]. Can use method\n', x0, f, gf, hf);
% Find the best fixed gamma
k = zeros(100, 1);
j = 1;
n = linspace(0.1, 1.5, 100);
for g = n
gamma_fixed_step = g;
[~, ~, k(j)] = method_steepest_descent(fun, grad_fun, x0, tol, max_iter, 'fixed');
j = j + 1;
end
plotItersOverGamma(n, k, "Iteration for different $\gamma$ values", "figures/StDes_Iter_o_gamma_" + point + ".png");
[~, j] = min(k);
gamma_fixed_step = n(j);
[x_fixed, f_fixed, kk] = method_steepest_descent(fun, grad_fun, x0, tol, max_iter, 'fixed');
fprintf('Fixed step: Initial point (%f, %f), steps:%d, Final (x,y)=(%f, %f), f(x,y)=%f\n', x0, kk, x_fixed(end, :), f_fixed(end));
plotPointsOverContour(x_fixed, fun, [-2, 0], [-2, 2], 100, point_str + ": Steepest descent $\gamma$ = " + gamma_fixed_step, "figures/StDes_fixed_" + point + ".png");
% Minimized f
[x_minimized, f_minimized, kk] = method_steepest_descent(fun, grad_fun, x0, tol, max_iter, 'minimized');
fprintf('Minimized f(g): Initial point (%f, %f), steps:%d, Final (x,y)=(%f, %f), f(x,y)=%f\n', x0, kk, x_minimized(end, :), f_minimized(end));
plotPointsOverContour(x_minimized, fun, [-2, 0], [-2, 2], 100, point_str + ": Steepest descent minimized $f(x_k + \gamma_kd_k)$", "figures/StDes_minimized_" + point + ".png");
% Armijo Rule
% Methods tuning
amijo_beta = 0.4; % typical range: [0.1, 0.8]
amijo_sigma = 0.1; % typical range: [0.01, 0.3]
[x_armijo, f_armijo, kk] = method_steepest_descent(fun, grad_fun, x0, tol, max_iter, 'armijo');
fprintf('Armijo step: Initial point (%f, %f), steps:%d, Final (x,y)=(%f, %f), f(x,y)=%f\n', x0, kk, x_armijo(end, :), f_armijo(end));
plotPointsOverContour(x_armijo, fun, [-2, 0], [-2, 2], 100, point_str + ": Steepest descent Armijo method", "figures/StDes_armijo_" + point + ".png");
disp(' ');
% Compare methods
plotConvCompare(x_fixed, "Fixed", x_minimized, "Minimized", x_armijo, "Armijo", Xmin, "Convergence compare", "figures/StDes_compare_" + point + ".png");
% Point x0 = (1, -1)
% =========================================================================
point = 3;
x0 = [1, -1];
point_str = "[" + x0(1) + ", " + x0(2) + "]";
f = fun(-1, 1);
gf = grad_fun(x0(1), x0(2));
hf = hessian_fun(x0(1), x0(2));
fprintf('Initial point (%d, %d), f = %f, grad = [%f;%f], hessian = [%f %f ; %f %f]. Can use method\n', x0, f, gf, hf);
% Find the best fixed gamma
k = zeros(100, 1);
j = 1;
n = linspace(0.1, 1, 100);
for g = n
gamma_fixed_step = g;
[~, ~, k(j)] = method_steepest_descent(fun, grad_fun, x0, tol, max_iter, 'fixed');
j = j + 1;
end
%if min(k) == max_iter
% fprintf('Fixed step: Initial point (%d, %d). Can NOT use method\n', x0);
%end
%plotItersOverGamma(n, k, "Iteration for different $\gamma$ values", "figures/StDes_Iter_o_gamma_" + point + ".png");
%gamma_fixed_step = 1;
[x_fixed, f_fixed, kk] = method_steepest_descent(fun, grad_fun, x0, tol, max_iter, 'fixed');
fprintf('Fixed step: Initial point (%f, %f), steps:%d, Final (x,y)=(%f, %f), f(x,y)=%f\n', x0, kk, x_fixed(end, :), f_fixed(end));
plotPointsOverContour(x_fixed, fun, [-1, 2], [-2, 2], 100, point_str + ": Steepest descent $\gamma$ = " + gamma_fixed_step, "figures/StDes_fixed_" + point + ".png");
% Minimized f
[x_minimized, f_minimized, kk] = method_steepest_descent(fun, grad_fun, x0, tol, max_iter, 'minimized');
fprintf('Minimized f(g): Initial point (%f, %f), steps:%d, Final (x,y)=(%f, %f), f(x,y)=%f\n', x0, kk, x_minimized(end, :), f_minimized(end));
plotPointsOverContour(x_minimized, fun, [-2, 2], [-3, 2], 100, point_str + ": Steepest descent minimized $f(x_k + \gamma_kd_k)$", "figures/StDes_minimized_" + point + ".png");
% Armijo Rule
% Methods tuning
amijo_beta = 0.4; % typical range: [0.1, 0.8]
amijo_sigma = 0.1; % typical range: [0.01, 0.3]
[x_armijo, f_armijo, kk] = method_steepest_descent(fun, grad_fun, x0, tol, max_iter, 'armijo');
fprintf('Armijo step: Initial point (%f, %f), steps:%d, Final (x,y)=(%f, %f), f(x,y)=%f\n', x0, kk, x_armijo(end, :), f_armijo(end));
plotPointsOverContour(x_armijo, fun, [-1, 2], [-2, 2], 100, point_str + ": Steepest descent Armijo method", "figures/StDes_armijo_" + point + ".png");
+42
View File
@@ -0,0 +1,42 @@
% Define environment (functions, gradients etc...)
GivenEnv
% Point x0 = (0, 0)
% =========================================================================
point = 1;
x0 = [0, 0];
f = fun(x0(1), x0(2));
gf = grad_fun(x0(1), x0(2));
hf = hessian_fun(x0(1), x0(2));
ev = eig(hf);
fprintf('Initial point (%d, %d), f = %f, grad = [%f;%f], hessian = [%f %f ; %f %f]. Eigenvalues= [%f, %f], Can NOT use method\n', x0, f, gf, hf, ev);
disp(' ');
% Point x0 = (-1, 1)
% =========================================================================
point = 2;
x0 = [-1, 1];
point_str = "[" + x0(1) + ", " + x0(2) + "]";
f = fun(-1, 1);
gf = grad_fun(x0(1), x0(2));
hf = hessian_fun(x0(1), x0(2));
ev = eig(hf);
fprintf('Initial point (%d, %d), f = %f, grad = [%f;%f], hessian = [%f %f ; %f %f]. Eigenvalues= [%f, %f], Can NOT use method\n', x0, f, gf, hf, ev);
disp(' ');
% Point x0 = (1, -1)
% =========================================================================
point = 3;
x0 = [1, -1];
point_str = "[" + x0(1) + ", " + x0(2) + "]";
f = fun(-1, 1);
gf = grad_fun(x0(1), x0(2));
hf = hessian_fun(x0(1), x0(2));
ev = eig(hf);
fprintf('Initial point (%d, %d), f = %f, grad = [%f;%f], hessian = [%f %f ; %f %f]. Eigenvalues= [%f, %f], Can NOT use method\n', x0, f, gf, hf, ev);
+119
View File
@@ -0,0 +1,119 @@
% Define environment (functions, gradients etc...)
GivenEnv
% Define parameters
max_iter = 300; % Maximum iterations
tol = 1e-4; % Tolerance
% Point x0 = (0, 0)
% =========================================================================
point = 1;
x0 = [0, 0];
f = fun(x0(1), x0(2));
gf = grad_fun(x0(1), x0(2));
hf = hessian_fun(x0(1), x0(2));
ev = eig(hf);
fprintf('Initial point (%d, %d), f = %f, grad = [%f;%f], hessian = [%f %f ; %f %f]. Eigenvalues= [%f, %f], Can NOT use method\n', x0, f, gf, hf, ev);
disp(' ');
% Point x0 = (-1, 1)
% =========================================================================
point = 2;
x0 = [-1, 1];
point_str = "[" + x0(1) + ", " + x0(2) + "]";
f = fun(-1, 1);
gf = grad_fun(x0(1), x0(2));
hf = hessian_fun(x0(1), x0(2));
ev = eig(hf);
fprintf('Initial point (%d, %d), f = %f, grad = [%f;%f], hessian = [%f %f ; %f %f]. Eigenvalues= [%f, %f], Can use method\n', x0, f, gf, hf, ev);
% Find the best fixed gamma
k = zeros(100, 1);
j = 1;
n = linspace(0.1, 1.5, 100);
for g = n
gamma_fixed_step = g;
[x, f, k(j)] = method_lev_mar(fun, grad_fun, hessian_fun, 0.3, x0, tol, max_iter, 'fixed');
if ~(x(end, 1) < -1.57 && x(end, 1) > -1.59 && x(end, 2) < 0.01 && x(end,2) > -0.01 && f(end) < -0.8 && f(end) > -0.82)
k(j) = 300;
end
j = j + 1;
end
plotItersOverGamma(n, k, "Iteration for different $\gamma$ values", "figures/LevMar_Iter_o_gamma_" + point + ".png");
[~, j] = min(k);
gamma_fixed_step = n(j);
[x_fixed, f_fixed, kk] = method_lev_mar(fun, grad_fun, hessian_fun, 0.3, x0, tol, max_iter, 'fixed');
fprintf('Fixed step: Initial point (%f, %f), steps:%d, Final (x,y)=(%f, %f), f(x,y)=%f\n', x0, kk, x_fixed(end, :), f_fixed(end));
plotPointsOverContour(x_fixed, fun, [-3, 0], [-2, 2], 100, point_str + ": Levenberg-Marquardt $\gamma$ = " + gamma_fixed_step, "figures/LevMar_fixed_" + point + ".png");
[x_minimized, f_minimized, kk] = method_lev_mar(fun, grad_fun, hessian_fun, 0.3, x0, tol, max_iter, 'minimized');
fprintf('Minimized f(g): Initial point (%f, %f), steps:%d, Final (x,y)=(%f, %f), f(x,y)=%f\n', x0, kk, x_minimized(end, :), f_minimized(end));
plotPointsOverContour(x_minimized, fun, [-3, 0], [-2, 2], 100, point_str + ": Levenberg-Marquardt minimized $f(x_k + \gamma_kd_k)$", "figures/LevMar_minimized_" + point + ".png");
% Armijo Rule
% Methods tuning
amijo_beta = 0.4; % typical range: [0.1, 0.8]
amijo_sigma = 0.1; % typical range: [0.01, 0.3]
[x_armijo, f_armijo, kk] = method_lev_mar(fun, grad_fun, hessian_fun, 0.3, x0, tol, max_iter, 'armijo');
fprintf('Armijo step: Initial point (%f, %f), steps:%d, Final (x,y)=(%f, %f), f(x,y)=%f\n', x0, kk, x_armijo(end, :), f_armijo(end));
plotPointsOverContour(x_armijo, fun, [-3, 0], [-2, 2], 100, point_str + ": Levenberg-Marquardt Armijo method", "figures/LevMar_armijo_" + point + ".png");
disp(' ');
% Compare methods
plotConvCompare(x_fixed, "Fixed", x_minimized, "Minimized", x_armijo, "Armijo", Xmin, "Convergence compare", "figures/LevMar_compare_" + point + ".png");
% Point x0 = (1, -1)
% =========================================================================
point = 3;
x0 = [1, -1];
point_str = "[" + x0(1) + ", " + x0(2) + "]";
f = fun(-1, 1);
gf = grad_fun(x0(1), x0(2));
hf = hessian_fun(x0(1), x0(2));
ev = eig(hf);
fprintf('Initial point (%d, %d), f = %f, grad = [%f;%f], hessian = [%f %f ; %f %f]. Eigenvalues= [%f, %f], Can use method\n', x0, f, gf, hf, ev);
% Find the best fixed gamma
k = zeros(100, 1);
j = 1;
n = linspace(0.1, 1.5, 100);
for g = n
gamma_fixed_step = g;
[x, f, k(j)] = method_lev_mar(fun, grad_fun, hessian_fun, 0.3, x0, tol, max_iter, 'fixed');
if ~(x(end, 1) < -1.57 && x(end, 1) > -1.59 && x(end, 2) < 0.01 && x(end,2) > -0.01 && f(end) < -0.8 && f(end) > -0.82)
k(j) = 300;
end
j = j + 1;
end
[~, j] = min(k);
gamma_fixed_step = n(j);
[x_fixed, f_fixed, kk] = method_lev_mar(fun, grad_fun, hessian_fun, 0.3, x0, tol, max_iter, 'fixed');
fprintf('Fixed step: Initial point (%f, %f), steps:%d, Final (x,y)=(%f, %f), f(x,y)=%f\n', x0, kk, x_fixed(end, :), f_fixed(end));
plotPointsOverContour(x_fixed, fun, [-3, 2], [-2, 2], 100, point_str + ": Levenberg-Marquardt $\gamma$ = " + gamma_fixed_step, "figures/LevMar_fixed_" + point + ".png");
[x_fixed, f_fixed, kk] = method_lev_mar(fun, grad_fun, hessian_fun, 0.3, x0, tol, max_iter, 'minimized');
fprintf('Minimized f(g): Initial point (%f, %f), steps:%d, Final (x,y)=(%f, %f), f(x,y)=%f\n', x0, kk, x_fixed(end, :), f_fixed(end));
plotPointsOverContour(x_fixed, fun, [-3, 2], [-2, 2], 100, point_str + ": Levenberg-Marquardt minimized $f(x_k + \gamma_kd_k)$", "figures/LevMar_minimized_" + point + ".png");
% Armijo Rule
% Methods tuning
amijo_beta = 0.4; % typical range: [0.1, 0.8]
amijo_sigma = 0.1; % typical range: [0.01, 0.3]
[x_armijo, f_armijo, kk] = method_lev_mar(fun, grad_fun, hessian_fun, 0.3, x0, tol, max_iter, 'armijo');
fprintf('Armijo step: Initial point (%f, %f), steps:%d, Final (x,y)=(%f, %f), f(x,y)=%f\n', x0, kk, x_armijo(end, :), f_armijo(end));
plotPointsOverContour(x_armijo, fun, [-3, 2], [-2, 2], 100, point_str + ": Levenberg-Marquardt Armijo method", "figures/LevMar_armijo_" + point + ".png");
disp(' ');
Binary file not shown.

After

Width:  |  Height:  |  Size: 57 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 98 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 86 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 72 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 100 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 86 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 103 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 93 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 73 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 256 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 55 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 93 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 86 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 80 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 94 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 84 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 98 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 92 KiB

+49
View File
@@ -0,0 +1,49 @@
function [a, b, k, n] = fmin_bisection(fun, alpha, beta, epsilon, lambda)
% Bisection method for finding the local minimum of a function.
%
% fun: The objective function
% alpha: (number) The starting point of the interval in which we seek
% for minimum
% beta: (number) The ending point of the interval in which we seek
% for minimum
% epsilon: (number) The epsilon value (distance from midpoint)
% lambda: (number) The lambda value (accuracy)
%
% return:
% a: (vector) Starting points of the interval for each iteration
% b: (vector) Ending points of the interval for each iteration
% k: (number) The number of iterations
% n: (number) The calls of objective function fun_expr
%
% Error checking
if alpha > beta || 2*epsilon >= lambda || lambda <= 0
error ('Input criteria not met')
end
% Init
a = alpha;
b = beta;
n = 0;
k=1;
while b(k) - a(k) > lambda
% bisect [a,b]
mid = (a(k) + b(k)) / 2;
x_1 = mid - epsilon;
x_2 = mid + epsilon;
% set new search interval
k = k + 1;
if fun(x_1) < fun(x_2)
a(k) = a(k-1);
b(k) = x_2;
else
a(k) = x_1;
b(k) = b(k-1);
end
end
end
+33
View File
@@ -0,0 +1,33 @@
function [gamma] = gamma_armijo(f, grad_f, dk, xk)
% Calculates the best step based on amijo method
%
% f(xk+ γk*dk) f(xk) + σ * γk * dk^T * f(xk)
% γk = β*γk_0
%
% f: Objective function
% grad_fun: Gradient function of f
% dk: Current value of selected direction -f or -inv{H}*f or -inv{H + lI}*f
% xk: Current point (x,y)
% beta: beta factor in [0.1, 0.5]
% signam: sigma factor in (0, 0.1]
global amijo_beta
global amijo_sigma
gf = grad_f(xk(1), xk(2));
gamma = 1; % Start with a step size of 1
% Perform Armijo line search
while f(xk(1) + gamma * dk(1), xk(2) + gamma * dk(2)) > ...
f(xk(1), xk(2)) + amijo_sigma * gamma * dk' * gf
%while f(xk(1) + gamma * dk(1), xk(2) + gamma * dk(2)) > ...
% f(xk(1), xk(2)) + amijo_sigma * gamma * norm(dk)^2
gamma = amijo_beta * gamma; % Reduce step size
if gamma < 1e-12 % Safeguard to prevent infinite reduction
warning('Armijo step size became too small.');
break;
end
end
end
+12
View File
@@ -0,0 +1,12 @@
function [gamma] = gamma_fixed(~, ~, ~, ~)
% Return a fixed step
%
% This is for completion and code symmetry.
%
global gamma_fixed_step
% Perform line search
gamma = gamma_fixed_step;
end
+24
View File
@@ -0,0 +1,24 @@
function [gamma] = gamma_minimized(f, ~, dk, xk)
% Calculates the step based on minimizing f(xk γk*dk)
%
%
% f: Objective function
% ~: Gradient function of f - Not used
% dk: Current value of selected direction -f or -inv{H}*f or -inv{H + lI}*f
% xk: Current point (x,y)
% Define the line search function fmin(g) = f(xk - g * dk)
fmin = @(g) f(xk(1) + g*dk(1), xk(2) + g*dk(2));
% find g that minimizes fmin
e = 0.0001;
l = 0.001;
[a,b,k,~] = fmin_bisection(fmin, 0, 5, e, l);
gamma = 0.5*(a(k) + b(k));
% Define the line search function fmin(g) = f(xk - g * dk)
%fmin = @(g) f(xk(1) - gamma * dk(1), xk(2) - gamma * dk(2));
% find g that minimizes fmin
%gamma = fminbnd(g, 0, 1);
end
+61
View File
@@ -0,0 +1,61 @@
function [x_vals, f_vals, k] = method_lev_mar(f, grad_f, hessian_f, e, xk, tol, max_iter, mode)
% f: Objective function
% grad_f: Gradient of the function
% hessian_f: Hessian of the function
% e: Offset for hessian damping Hk' = Hk + mI
% - when: Hk not positive defined
% - Where: m = abs(min(eig(Hk))) + e
% xk: Initial point [xk, yk]
% 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, dk, xk) gamma_armijo(f, grad_f, dk, xk);
elseif strcmp(mode, 'minimized') == 1
gamma_f = @(f, grad_f, dk, xk) gamma_minimized(f, grad_f, dk, xk);
else % mode == 'fixed'
gamma_f = @(f, grad_f, dk, xk) gamma_fixed(f, grad_f, dk, xk);
end
x_vals = xk; % Store iterations
f_vals = f(xk(1), xk(2));
for k = 1:max_iter
grad = grad_f(xk(1), xk(2));
% Check for convergence
if norm(grad) < tol
break;
end
hess = hessian_f(xk(1), xk(2));
% Check if hessian is not positive defined
lmin = min(eig(hess));
if lmin <= 0
% Select m with offset to stear hess to positive eigenvalues
m = abs(lmin) + e;
mI = m * eye(size(hess));
if min(eig(hess + mI)) <= 0 % Fail-check
warning('Can not normalize hessian matrix.');
end
end
% Solve for search direction using Newton's step
dk = - inv(hess + mI) * grad;
% Calculate gamma
gk = gamma_f(f, grad_f, dk, xk);
x_next = xk + gk * dk'; % Update step
f_next = f(x_next(1), x_next(2));
xk = x_next; % Update point
x_vals = [x_vals; x_next]; % Store values
f_vals = [f_vals; f_next]; % Store function values
end
end
+47
View File
@@ -0,0 +1,47 @@
function [x_vals, f_vals, k] = method_newton(f, grad_f, hessian_f, xk, tol, max_iter, mode)
% f: Objective function
% grad_f: Gradient of the function
% hessian_f: Hessian 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, dk, xk) gamma_armijo(f, grad_f, dk, xk);
elseif strcmp(mode, 'minimized') == 1
gamma_f = @(f, grad_f, dk, xk) gamma_minimized(f, grad_f, dk, xk);
else % mode == 'fixed'
gamma_f = @(f, grad_f, dk, xk) gamma_fixed(f, grad_f, dk, xk);
end
x_vals = xk; % Store iterations
f_vals = f(xk(1), xk(2));
for k = 1:max_iter
grad = grad_f(xk(1), xk(2));
% Check for convergence
if norm(grad) < tol
break;
end
hess = hessian_f(xk(1), xk(2));
% Solve for search direction using Newton's step
dk = - inv(hess) * grad;
% Calculate gamma
gk = gamma_f(f, grad_f, dk, xk);
x_next = xk + gk * dk'; % Update step
f_next = f(x_next(1), x_next(2));
xk = x_next; % Update point
x_vals = [x_vals; x_next]; % Store values
f_vals = [f_vals; f_next]; % Store function values
end
end
+43
View File
@@ -0,0 +1,43 @@
function [x_vals, f_vals, k] = method_steepest_descent(f, grad_f, xk, tol, max_iter, mode)
% f: Objective function
% grad_f: Gradient of the function
% xk: 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, dk, xk) gamma_armijo(f, grad_f, dk, xk);
elseif strcmp(mode, 'minimized') == 1
gamma_f = @(f, grad_f, dk, xk) gamma_minimized(f, grad_f, dk, xk);
else % mode == 'fixed'
gamma_f = @(f, grad_f, dk, xk) gamma_fixed(f, grad_f, dk, xk);
end
% Storage for iterations, begin with the first point
x_vals = xk;
f_vals = f(xk(1), xk(2));
for k = 1:max_iter
grad = grad_f(xk(1), xk(2));
% Check for convergence
if norm(grad) < tol
break;
end
dk = - grad;
% Calculate gamma
gk = gamma_f(f, grad_f, dk, xk);
x_next = xk + gk * dk'; % Update step
f_next = f(x_next(1), x_next(2));
xk = x_next; % Update point
x_vals = [x_vals; x_next]; % Store values
f_vals = [f_vals; f_next]; % Store function values
end
end
+37
View File
@@ -0,0 +1,37 @@
function plot3dFun(fun, x_lim, y_lim, size, plot_title, filename)
% 3D plots a function
% fun: The function to plot
% x_lim: The range for x axis. ex: [-2, 2]
% y_lim: The range for y axis. ex: [0, 2]
% size: The number of points for each axis
% plot_title: The latex title for the plot
%
global image_width,
global image_height;
% Generate a grid for x and y
x_space = linspace(x_lim(1), x_lim(2), size);
y_space = linspace(y_lim(1), y_lim(2), size);
[X, Y] = meshgrid(x_space, y_space);
% Evaluate the function on the grid
Z = fun(X, Y);
% 3D plot
figure('Name', 'f(x,y)', 'NumberTitle', 'off');
set(gcf, 'Position', [100, 100, image_width, image_height]); % Set the figure size
surf(X, Y, Z);
% Customize the plot
xlabel('x'); % Label for x-axis
ylabel('y'); % Label for y-axis
zlabel('f(x, y)'); % Label for z-axis
title(plot_title, 'Interpreter', 'latex', 'FontSize', 16); % Title of the plot
colorbar;
% save the figure
if strcmp(filename, '') == 0
print(gcf, filename, '-dpng', '-r300');
end
end
+36
View File
@@ -0,0 +1,36 @@
function plotContour(fun, x_lim, y_lim, size, plot_title, filename)
% plot the contour of a function
% fun: The function to plot
% x_lim: The range for x axis. ex: [-2, 2]
% y_lim: The range for y axis. ex: [0, 2]
% size: The number of points for each axis
% plot_title: The latex title for the plot
%
global image_width,
global image_height;
% Generate a grid for x and y
x_space = linspace(x_lim(1), x_lim(2), size);
y_space = linspace(y_lim(1), y_lim(2), size);
[X, Y] = meshgrid(x_space, y_space);
% Evaluate the function on the grid
Z = fun(X, Y);
% Contour
figure('Name', 'Contours of f(x,y)', 'NumberTitle', 'off');
set(gcf, 'Position', [100, 100, image_width, image_height]); % Set the figure size
contour(X, Y, Z);
% Customize the plot
xlabel('x'); % Label for x-axis
ylabel('y'); % Label for y-axis
title(plot_title, 'Interpreter', 'latex', 'FontSize', 16); % Title of the plot
colorbar;
% save the figure
if strcmp(filename, '') == 0
print(gcf, filename, '-dpng', '-r300');
end
end
+54
View File
@@ -0,0 +1,54 @@
function plotConvCompare(points_1, title_1, points_2, title_2, points_3, title_3, Min_point, plot_title, filename)
% 3D plots a function
% points: The points to plot
% contur_fun: The function for contour plot
% x_lim: The range for x axis. ex: [-2, 2]
% y_lim: The range for y axis. ex: [0, 2]
% size: The number of points for each axis
% plot_title: The latex title for the plot
% filename: The filename to save the plot (if exists)
%
global image_width,
global image_height;
distances_1 = sqrt((points_1(:,1) - Min_point(1)).^2 + (points_1(:,2) - Min_point(2)).^2);
distances_2 = sqrt((points_2(:,1) - Min_point(1)).^2 + (points_2(:,2) - Min_point(2)).^2);
distances_3 = sqrt((points_3(:,1) - Min_point(1)).^2 + (points_3(:,2) - Min_point(2)).^2);
% 2D plot
figure('Name', 'Convergence compare', 'NumberTitle', 'off');
set(gcf, 'Position', [100, 100, image_width, image_height]); % Set the figure size
title(plot_title, 'Interpreter', 'latex', 'FontSize', 16); % Title of the plot
% One
subplot(3, 1, 1);
plot(distances_1, '-o');
% Customize the plot
ylabel(title_1, 'Interpreter', 'none');
xlabel('Step');
grid on
% One
subplot(3, 1, 2);
plot(distances_2, '-o');
% Customize the plot
ylabel(title_2, 'Interpreter', 'none');
xlabel('Step');
grid on
% One
subplot(3, 1, 3);
plot(distances_3, '-o');
% Customize the plot
ylabel(title_3, 'Interpreter', 'none');
xlabel('Step');
grid on
% save the figure
if strcmp(filename, '') == 0
print(gcf, filename, '-dpng', '-r300');
end
end
+28
View File
@@ -0,0 +1,28 @@
function plotItersOverGamma(gamma, iterations, plot_title, filename)
% 3D plots a function
% fun: The points to plot
% contur_fun: The function for contour plot
% x_lim: The range for x axis. ex: [-2, 2]
% y_lim: The range for y axis. ex: [0, 2]
% size: The number of points for each axis
% plot_title: The latex title for the plot
% filename: The filename to save the plot (if exists)
%
global image_width,
global image_height;
figure('Name', 'Iterations_over_gamma', 'NumberTitle', 'off');
set(gcf, 'Position', [100, 100, image_width, image_height]); % Set the figure size
plot(gamma, iterations, '*r', 'LineWidth', 2);
% Customize the plot
title(plot_title, 'Interpreter', 'latex', 'FontSize', 16); % Title of the plot
xlabel('\gamma') ;
ylabel('Iterations');
% save the figure
if strcmp(filename, '') == 0
print(gcf, filename, '-dpng', '-r300');
end
end
+43
View File
@@ -0,0 +1,43 @@
function plotPointsOverContour(points, contour_fun, x_lim, y_lim, size, plot_title, filename)
% 3D plots a function
% points: The points to plot
% contur_fun: The function for contour plot
% x_lim: The range for x axis. ex: [-2, 2]
% y_lim: The range for y axis. ex: [0, 2]
% size: The number of points for each axis
% plot_title: The latex title for the plot
% filename: The filename to save the plot (if exists)
%
global image_width,
global image_height;
% Generate a grid for x and y
x_space = linspace(x_lim(1), x_lim(2), size);
y_space = linspace(y_lim(1), y_lim(2), size);
[X, Y] = meshgrid(x_space, y_space);
% Evaluate the function on the grid
Z = contour_fun(X, Y);
% 2D plot
figure('Name', '(x,y) convergence', 'NumberTitle', 'off');
set(gcf, 'Position', [100, 100, image_width, image_height]); % Set the figure size
plot(points(:, 1), points(:, 2), '-or');
hold on
contour(X, Y, Z);
% Customize the plot
xlim(x_lim);
ylim(y_lim);
xlabel('x'); % Label for x-axis
ylabel('y'); % Label for y-axis
grid on
title(plot_title, 'Interpreter', 'latex', 'FontSize', 16); % Title of the plot
colorbar;
% save the figure
if strcmp(filename, '') == 0
print(gcf, filename, '-dpng', '-r300');
end
end