diff --git a/Claudio_Maggioni_midterm/main.m b/Claudio_Maggioni_midterm/main.m new file mode 100644 index 0000000..ccb6fa0 --- /dev/null +++ b/Claudio_Maggioni_midterm/main.m @@ -0,0 +1,48 @@ +clear +clc +close all +syms xsym ysym + +%% 2.4 +f1 = (ysym - 4 * xsym^2)^2 + (1 - xsym)^2; +[x1, xs1, ~] = trust_region(f1, 2, 1, 0.2, [0;0], 1e-8, 1000); + +%% 2.4c - Energy function plot +surf_iterates(f1, xs1, -0.25:0.01:1.25, -0.5:0.01:4.25); +figure; + +%% 2.5 - Rosenbrock's function +f2 = (1 - xsym)^2 + 100 * (ysym - xsym^2)^2; +[x2, xs2, gnorms2] = trust_region(f2, 2, 1, 0.2, [0;0], 1e-8, 1000); + +%% 2.5b - Energy function plot +surf_iterates(f2, xs2, -0.25:0.01:1.25, -0.25:0.01:1.25); + +%% 2.5c - Log gradient norms +figure; +semilogy(0:size(gnorms2, 2)-1, gnorms2, '.-k'); + +%% Helper functions +function surf_iterates(f, xs, xrange, yrange) + fl = matlabFunction(f); + Z = zeros(size(yrange,2), size(xrange,2)); + + for x = 1:size(xrange, 2) + for y = 1:size(yrange, 2) + Z(y,x) = fl(xrange(x), yrange(y)); + end + end + + surf(xrange, yrange', Z, 'EdgeColor', 'none', 'FaceAlpha', 0.4); + + yrange = zeros(1, size(xs, 2)); + for i=1:size(xs, 2) + yrange(1, i) = fl(xs(1, i), xs(2, i)); + end + + hold on + plot3(xs(1, :), xs(2, :), yrange, '.r-'); + plot3(xs(1, 1), xs(2, 1), yrange(1), '.w-'); + plot3(xs(1, size(xs,2)), xs(2, size(xs,2)), yrange(size(xs,2)), '.k-'); + hold off +end \ No newline at end of file diff --git a/Claudio_Maggioni_midterm/trust_region.m b/Claudio_Maggioni_midterm/trust_region.m index 09f22c4..d42f7ee 100644 --- a/Claudio_Maggioni_midterm/trust_region.m +++ b/Claudio_Maggioni_midterm/trust_region.m @@ -1,19 +1,5 @@ - -syms x y -f1 = (y - 4 * x^2)^2 + (1 - x)^2; -[x, xs, gnorms] = trust_reg(f1, 2, 1, 0.2, [0;0], 1e-8, 1000); - -% Convert lambda to accept vector parameters -function vl = vecLambda(fl) - vl = @(x) fl(x(1), x(2)); -end - -% Compute quadratic form -function y = qf(B, g, p, fk) - y = fk + 1/2 * p' * B * p + dot(g, p); -end - -function [xk, xs, gnorms] = trust_reg(f, delta_hat, delta0, eta, x0, tol, max_n) +function [xk, xs, gnorms] = trust_region(f, delta_hat, delta0, eta, ... + x0, tol, max_n) xs = zeros(2, max_n); gnorms = zeros(max_n); @@ -69,3 +55,13 @@ function [xk, xs, gnorms] = trust_reg(f, delta_hat, delta0, eta, x0, tol, max_n) xs(:, i) = xk; end end + +% Convert lambda to accept vector parameters +function vl = vecLambda(fl) + vl = @(x) fl(x(1), x(2)); +end + +% Compute quadratic form +function y = qf(B, g, p, fk) + y = fk + 1/2 * p' * B * p + dot(g, p); +end