WIP
This commit is contained in:
@@ -19,4 +19,10 @@ hessian_fun = matlabFunction(hessian_fexpr, 'Vars', [x, y]); % Hessian
|
||||
global amijo_beta amijo_sigma
|
||||
|
||||
%fixed step size globals
|
||||
global gamma_fixed_step
|
||||
global gamma_fixed_step
|
||||
|
||||
global image_width,
|
||||
global image_height;
|
||||
|
||||
image_width = 960;
|
||||
image_height = 640;
|
||||
@@ -2,8 +2,8 @@
|
||||
GivenEnv
|
||||
|
||||
%
|
||||
% We plot the function in the domain of x in [-3, 3] and y in [-3, 3].
|
||||
% We also plot the contour in order to get a senso of the min and maximum
|
||||
% 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
|
||||
%
|
||||
|
||||
|
||||
@@ -10,43 +10,53 @@ amijo_beta = 0.5; % Armijo reduction factor
|
||||
amijo_sigma = 0.1; % Armijo condition constant
|
||||
|
||||
% Point x0 = (0, 0)
|
||||
% 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\n', x0, f, gf, hf);
|
||||
|
||||
% Points x0 = (-1, 1), (1, -1)
|
||||
%x0s = [-1, 1 ; 1, -1]; % Initial points
|
||||
|
||||
% Point x0 = (0, 0)
|
||||
x0s = [-1, 1 ; 1, -1]; % Initial points
|
||||
for i = 1:size(x0s, 1)
|
||||
x0 = x0s(i, :);
|
||||
point_str = "[" + x0(1) + ", " + x0(2) + "]";
|
||||
% Point x0 = (-1, 1)
|
||||
point = 2;
|
||||
x0 = [-1, 1];
|
||||
point_str = "[" + x0(1) + ", " + x0(2) + "]";
|
||||
|
||||
% 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)] = steepest_descent(fun, grad_fun, x0, tol, max_iter, 'fixed');
|
||||
j = j + 1;
|
||||
end
|
||||
plotIterationsOverGamma(n, k, "Iteration for different $\gamma$ values", "StDes_Iter_o_gamma_" + i + ".png");
|
||||
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);
|
||||
|
||||
[~, j] = min(k);
|
||||
gamma_fixed_step = n(j);
|
||||
% 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");
|
||||
|
||||
[x_fixed, f_fixed, kk] = 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, 2], [-2, 2], 100, point_str + ": Steepest descent $\gamma$ = " + gamma_fixed_step, "StDes_fixed_" + i + ".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] = steepest_descent(fun, grad_fun, x0, tol, max_iter, 'minimized');
|
||||
[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], [-2, 2], 100, point_str + ": Steepest descent minimized $f(x_k + \gamma_kd_k)$", "StDes_minimized_" + i + ".png");
|
||||
|
||||
|
||||
% Armijo Rule
|
||||
[x_armijo, f_armijo, kk] = steepest_descent(fun, grad_fun, x0, tol, max_iter, 'armijo');
|
||||
[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, 2], [-2, 2], 100, point_str + ": Steepest descent Armijo method", "StDes_armijo_" + i + ".png");
|
||||
end
|
||||
|
||||
|
||||
@@ -49,4 +49,3 @@ for i = 1:size(x0s, 1)
|
||||
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, 2], [-3, 3], 100, point_str + ": Newton Armijo method", "Newton_armijo_" + i + ".png");
|
||||
end
|
||||
|
||||
|
||||
@@ -51,4 +51,3 @@ for i = 1:size(x0s, 1)
|
||||
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, 2], [-3, 3], 100, point_str + ": LevMar Armijo method", "LevMar_armijo_" + i + ".png");
|
||||
end
|
||||
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 91 KiB After Width: | Height: | Size: 73 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 401 KiB After Width: | Height: | Size: 255 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 54 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 95 KiB |
@@ -23,4 +23,3 @@ function [gamma] = gamma_armijo(f, grad_f, x0)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
@@ -9,4 +9,4 @@ function [gamma] = gamma_fixed(~, ~, ~)
|
||||
% Perform line search
|
||||
gamma = gamma_fixed_step;
|
||||
|
||||
end
|
||||
end
|
||||
@@ -16,4 +16,3 @@ function [gamma] = gamma_minimized(f, grad_f, x0)
|
||||
% ToDo: Check if we can use fmin_bisection_der
|
||||
% from the previous assigment here!
|
||||
end
|
||||
|
||||
|
||||
@@ -46,4 +46,3 @@ function [x_vals, f_vals, k] = method_lev_mar(f, grad_f, hessian_f, m, x0, tol,
|
||||
f_vals = [f_vals; f_next]; % Store function values
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -45,4 +45,3 @@ function [x_vals, f_vals, k] = method_newton(f, grad_f, hessian_f, x0, tol, max_
|
||||
f_vals = [f_vals; f_next]; % Store function values
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -41,4 +41,3 @@ function [x_vals, f_vals, k] = method_steepest_descent(f, grad_f, x0, tol, max_i
|
||||
f_vals = [f_vals; f_next]; % Store function values
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -7,6 +7,9 @@ function plot3dFun(fun, x_lim, y_lim, size, plot_title, filename)
|
||||
% 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);
|
||||
@@ -17,7 +20,7 @@ function plot3dFun(fun, x_lim, y_lim, size, plot_title, filename)
|
||||
|
||||
% 3D plot
|
||||
figure('Name', 'f(x,y)', 'NumberTitle', 'off');
|
||||
set(gcf, 'Position', [100, 100, 960, 960]); % Set the figure size
|
||||
set(gcf, 'Position', [100, 100, image_width, image_height]); % Set the figure size
|
||||
surf(X, Y, Z);
|
||||
|
||||
% Customize the plot
|
||||
|
||||
@@ -7,6 +7,9 @@ function plotContour(fun, x_lim, y_lim, size, plot_title, filename)
|
||||
% 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);
|
||||
@@ -17,7 +20,7 @@ function plotContour(fun, x_lim, y_lim, size, plot_title, filename)
|
||||
|
||||
% Contour
|
||||
figure('Name', 'Contours of f(x,y)', 'NumberTitle', 'off');
|
||||
set(gcf, 'Position', [100, 100, 960, 960]); % Set the figure size
|
||||
set(gcf, 'Position', [100, 100, image_width, image_height]); % Set the figure size
|
||||
contour(X, Y, Z);
|
||||
|
||||
% Customize the plot
|
||||
|
||||
@@ -1,16 +1,19 @@
|
||||
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)
|
||||
%
|
||||
% 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, 960, 960]); % Set the figure size
|
||||
set(gcf, 'Position', [100, 100, image_width, image_height]); % Set the figure size
|
||||
plot(gamma, iterations, '*r', 'LineWidth', 2);
|
||||
|
||||
% Customize the plot
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
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)
|
||||
%
|
||||
% 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);
|
||||
@@ -19,7 +22,7 @@ function plotPointsOverContour(points, contour_fun, x_lim, y_lim, size, plot_tit
|
||||
|
||||
% 2D plot
|
||||
figure('Name', '(x,y)', 'NumberTitle', 'off');
|
||||
set(gcf, 'Position', [100, 100, 960, 960]); % Set the figure size
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user