midterm: done 1, 2.3-2.5 (minus report commentary)

This commit is contained in:
Claudio Maggioni (maggicl) 2021-05-04 09:33:16 +02:00
parent 3f8034123e
commit cbd98658ee
2 changed files with 60 additions and 16 deletions

View file

@ -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

View file

@ -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