A version of Work 3 without some small parts
@@ -1,33 +0,0 @@
|
||||
% Given environment
|
||||
|
||||
clear;
|
||||
% Setup the function under test
|
||||
syms x [2 1] real;
|
||||
fexpr = (1/3)*x(1)^2 +3*x(2)^2;
|
||||
title_fun = "$f(x) = frac{1}{3}{x_1}^2 + 3{x_2}^2$";
|
||||
|
||||
% Calculate the gradient and Hessian
|
||||
grad_fexpr = gradient(fexpr, x); % Gradient of f
|
||||
hessian_fexpr = hessian(fexpr, x); % Hessian of f
|
||||
|
||||
% Convert symbolic expressions to MATLAB functions
|
||||
fun = matlabFunction(fexpr, 'Vars', {x}); % Function
|
||||
grad_fun = matlabFunction(grad_fexpr, 'Vars', {x}); % Gradient
|
||||
hessian_fun = matlabFunction(hessian_fexpr, 'Vars', {x}); % 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;
|
||||
@@ -3,7 +3,7 @@ GivenEnv
|
||||
|
||||
% Define parameters
|
||||
max_iter = 1000; % Maximum iterations
|
||||
tol = 1e-4; % Tolerance
|
||||
tol = 0.001; % Tolerance
|
||||
|
||||
% Point x0 = (1, 1)
|
||||
% =========================================================================
|
||||
@@ -15,15 +15,26 @@ gf = grad_fun(x0);
|
||||
hf = hessian_fun(x0);
|
||||
fprintf('Initial point (%d, %d), f = %f, grad = [%f;%f], hessian = [%f %f ; %f %f]=> Method applicable\n', x0, f, gf, hf);
|
||||
|
||||
for g=[0.1, 0.3, 0.5, 3, 5]
|
||||
|
||||
k = zeros(100, 1);
|
||||
j = 1;
|
||||
n = linspace(0.1, 1.5, 100);
|
||||
for g = n
|
||||
gamma_fixed_step = g;
|
||||
[~, ~, k(j)] = method_SteepDesc(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");
|
||||
|
||||
for g=[0.1, 0.3, 3, 5]
|
||||
gamma_fixed_step = g;
|
||||
|
||||
[x_fixed, f_fixed, kk] = method_SteepDesc(fun, grad_fun, x0, tol, max_iter, 'fixed');
|
||||
fprintf('Fixed step g=%f: Initial point (%f, %f), steps:%d, Final (x1,x2)=(%f, %f), f(x1,x2)=%f\n', g, x0, kk, x_fixed(:, end), f_fixed(end));
|
||||
if g <= 1
|
||||
plotPointsOverContour(x_fixed, fun, XSetLimmits(1, :), XSetLimmits(2, :), 100, point_str + ": Steepest descent $\gamma$ = " + gamma_fixed_step, "");
|
||||
plotPointsOverContour(x_fixed, fun, XSetLimmits(1, :), XSetLimmits(2, :), 100, point_str + ": Steepest descent $\gamma$ = " + gamma_fixed_step, "figures/StDes_gamma_" + g + ".png");
|
||||
else
|
||||
plotPointsOverContour(x_fixed, fun, [-500, 500], [-500, 500], 100, point_str + ": Steepest descent $\gamma$ = " + gamma_fixed_step, "");
|
||||
plotPointsOverContour(x_fixed, fun, [-500, 500], [-500, 500], 100, point_str + ": Steepest descent $\gamma$ = " + gamma_fixed_step, "figures/StDes_gamma_" + g + ".png");
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ fprintf('Initial point (%d, %d), f = %f, grad = [%f;%f], hessian = [%f %f ; %f %
|
||||
gamma_fixed_step = gamma;
|
||||
[x_fixed, f_fixed, kk] = method_SteepDesc_Proj(fun, grad_fun, x0, sk_step, XSetLimmits, tol, max_iter, 'fixed');
|
||||
fprintf('Fixed step g=%f: Initial point (%f, %f), steps:%d, Final (x1,x2)=(%f, %f), f(x1,x2)=%f\n', gamma_fixed_step, x0, kk, x_fixed(:, end), f_fixed(end));
|
||||
plotPointsOverContour(x_fixed, fun, XSetLimmits(1, :), XSetLimmits(2, :), 100, point_str + ": Steepest descent $\gamma$ = " + gamma_fixed_step, "");
|
||||
plotPointsOverContour(x_fixed, fun, XSetLimmits(1, :), XSetLimmits(2, :), 100, point_str + ": Steepest descent proj $s_k$ = " + sk_step + ", $\gamma$ = " + gamma_fixed_step, "figures/StDesProj_sk_" + sk_step + "_gamma_" + gamma + ".png");
|
||||
|
||||
%[x_minimized, f_minimized, kk] = method_SteepDesc_Proj(fun, grad_fun, x0, sk_step, XSetLimmits, tol, max_iter, 'minimized');
|
||||
%fprintf('Minimized f: Initial point (%f, %f), steps:%d, Final (x1,x2)=(%f, %f), f(x1,x2)=%f\n', x0, kk, x_fixed(:, end), f_fixed(end));
|
||||
|
||||
@@ -8,7 +8,7 @@ max_iter = 1000; % Maximum iterations
|
||||
% =========================================================================
|
||||
x0 = [-5, 10]';
|
||||
gamma = 0.1;
|
||||
sk_step = 55;
|
||||
sk_step = 15;
|
||||
tol = 0.01; % Tolerance
|
||||
|
||||
|
||||
@@ -23,5 +23,5 @@ fprintf('Initial point (%d, %d), f = %f, grad = [%f;%f], hessian = [%f %f ; %f %
|
||||
gamma_fixed_step = gamma;
|
||||
[x_fixed, f_fixed, kk] = method_SteepDesc_Proj(fun, grad_fun, x0, sk_step, XSetLimmits, tol, max_iter, 'fixed');
|
||||
fprintf('Fixed step g=%f: Initial point (%f, %f), steps:%d, Final (x1,x2)=(%f, %f), f(x1,x2)=%f\n', gamma_fixed_step, x0, kk, x_fixed(:, end), f_fixed(end));
|
||||
plotPointsOverContour(x_fixed, fun, XSetLimmits(1, :), XSetLimmits(2, :), 100, point_str + ": Steepest descent $\gamma$ = " + gamma_fixed_step, "");
|
||||
plotPointsOverContour(x_fixed, fun, XSetLimmits(1, :), XSetLimmits(2, :), 100, point_str + ": Steepest descent proj $s_k$ = " + sk_step + ", $\gamma$ = " + gamma_fixed_step, "figures/StDesProj_sk_" + sk_step + "_gamma_" + gamma + ".png");
|
||||
|
||||
|
||||
@@ -23,5 +23,5 @@ fprintf('Initial point (%d, %d), f = %f, grad = [%f;%f], hessian = [%f %f ; %f %
|
||||
gamma_fixed_step = gamma;
|
||||
[x_fixed, f_fixed, kk] = method_SteepDesc_Proj(fun, grad_fun, x0, sk_step, XSetLimmits, tol, max_iter, 'fixed');
|
||||
fprintf('Fixed step g=%f: Initial point (%f, %f), steps:%d, Final (x1,x2)=(%f, %f), f(x1,x2)=%f\n', gamma_fixed_step, x0, kk, x_fixed(:, end), f_fixed(end));
|
||||
plotPointsOverContour(x_fixed, fun, XSetLimmits(1, :), XSetLimmits(2, :), 100, point_str + ": Steepest descent $\gamma$ = " + gamma_fixed_step, "");
|
||||
plotPointsOverContour(x_fixed, fun, XSetLimmits(1, :), XSetLimmits(2, :), 100, point_str + ": Steepest descent proj $s_k$ = " + sk_step + ", $\gamma$ = " + gamma_fixed_step, "figures/StDesProj_sk_" + sk_step + "_gamma_" + gamma + ".png");
|
||||
|
||||
|
||||
|
Before Width: | Height: | Size: 90 KiB After Width: | Height: | Size: 91 KiB |
|
Before Width: | Height: | Size: 1.1 MiB After Width: | Height: | Size: 1.1 MiB |
|
After Width: | Height: | Size: 131 KiB |
|
After Width: | Height: | Size: 124 KiB |
|
After Width: | Height: | Size: 109 KiB |
|
After Width: | Height: | Size: 135 KiB |
|
After Width: | Height: | Size: 66 KiB |
|
Before Width: | Height: | Size: 112 KiB |
|
Before Width: | Height: | Size: 110 KiB |
|
After Width: | Height: | Size: 110 KiB |
|
After Width: | Height: | Size: 125 KiB |
|
After Width: | Height: | Size: 130 KiB |
|
After Width: | Height: | Size: 132 KiB |