version 2

This commit is contained in:
Christos Choutouridis
2024-11-27 16:09:37 +02:00
parent 3b40c20ec0
commit 2b2df13940
35 changed files with 212 additions and 73 deletions
+4
View File
@@ -15,6 +15,10 @@ 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])
+2 -2
View File
@@ -8,7 +8,7 @@ GivenEnv
%
% 3d plot the function
plot3dFun(fun, [-3, 3], [-3, 3], 100, title_fun, "figures/FunctionPlot.png");
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/FunctionContour.png");
plotContour(fun, [-3, 3], [-3, 3], 100, title_fun, "figures/Plot_Contour.png");
+3 -1
View File
@@ -63,6 +63,9 @@ fprintf('Armijo step: Initial point (%f, %f), steps:%d, Final (x,y)=(%f, %f),
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;
@@ -111,4 +114,3 @@ fprintf('Armijo step: Initial point (%f, %f), steps:%d, Final (x,y)=(%f, %f),
plotPointsOverContour(x_armijo, fun, [-1, 2], [-2, 2], 100, point_str + ": Steepest descent Armijo method", "figures/StDes_armijo_" + point + ".png");
-1
View File
@@ -40,4 +40,3 @@ 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);
+9 -6
View File
@@ -43,6 +43,7 @@ for g = n
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);
@@ -51,9 +52,9 @@ gamma_fixed_step = n(j);
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_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, 0], [-2, 2], 100, point_str + ": Levenberg-Marquardt minimized $f(x_k + \gamma_kd_k)$", "figures/LevMar_minimized_" + 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
@@ -63,9 +64,11 @@ 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/StDes_armijo_" + point + ".png");
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)
% =========================================================================
@@ -112,5 +115,5 @@ 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/StDes_armijo_" + point + ".png");
disp(' ');
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.

Before

Width:  |  Height:  |  Size: 73 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 255 KiB

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.

Before

Width:  |  Height:  |  Size: 106 KiB

After

Width:  |  Height:  |  Size: 100 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 91 KiB

After

Width:  |  Height:  |  Size: 86 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 109 KiB

After

Width:  |  Height:  |  Size: 103 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 98 KiB

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

-1
View File
@@ -1 +0,0 @@
File to keep directory
Binary file not shown.

Before

Width:  |  Height:  |  Size: 58 KiB

After

Width:  |  Height:  |  Size: 55 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 104 KiB

After

Width:  |  Height:  |  Size: 93 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 92 KiB

After

Width:  |  Height:  |  Size: 86 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 80 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 101 KiB

After

Width:  |  Height:  |  Size: 94 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 89 KiB

After

Width:  |  Height:  |  Size: 84 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 104 KiB

After

Width:  |  Height:  |  Size: 98 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 97 KiB

After

Width:  |  Height:  |  Size: 92 KiB

+1 -1
View File
@@ -46,4 +46,4 @@ while b(k) - a(k) > lambda
end
end
end
end
+1 -1
View File
@@ -30,4 +30,4 @@ function [gamma] = gamma_armijo(f, grad_f, dk, xk)
end
end
end
end
+1 -1
View File
@@ -21,4 +21,4 @@ function [gamma] = gamma_minimized(f, ~, dk, xk)
% find g that minimizes fmin
%gamma = fminbnd(g, 0, 1);
end
end
+6 -4
View File
@@ -2,7 +2,9 @@ function [x_vals, f_vals, k] = method_lev_mar(f, grad_f, hessian_f, e, xk, tol,
% f: Objective function
% grad_f: Gradient of the function
% hessian_f: Hessian of the function
% e: mu offset for hessian damping H' = H_k + mI
% 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
@@ -35,10 +37,10 @@ function [x_vals, f_vals, k] = method_lev_mar(f, grad_f, hessian_f, e, xk, tol,
% 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));
nev = eig(hess + mI);
if min(nev) <= 0
if min(eig(hess + mI)) <= 0 % Fail-check
warning('Can not normalize hessian matrix.');
end
end
@@ -56,4 +58,4 @@ function [x_vals, f_vals, k] = method_lev_mar(f, grad_f, hessian_f, e, xk, tol,
x_vals = [x_vals; x_next]; % Store values
f_vals = [f_vals; f_next]; % Store function values
end
end
end
+1 -1
View File
@@ -44,4 +44,4 @@ function [x_vals, f_vals, k] = method_newton(f, grad_f, hessian_f, xk, tol, max_
x_vals = [x_vals; x_next]; % Store values
f_vals = [f_vals; f_next]; % Store function values
end
end
end
+1 -1
View File
@@ -40,4 +40,4 @@ function [x_vals, f_vals, k] = method_steepest_descent(f, grad_f, xk, tol, max_i
x_vals = [x_vals; x_next]; % Store values
f_vals = [f_vals; f_next]; % Store function values
end
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