diff --git a/hw1/contour.tex b/hw1/contour.tex index f9b6b3a..56a1f20 100644 --- a/hw1/contour.tex +++ b/hw1/contour.tex @@ -17,7 +17,7 @@ xmax=10, ymin=-10, ymax=10, axis background/.style={fill=white}, -title style={font=\bfseries}, +title style={yshift=1ex, font=\tiny\bfseries}, title={u=1} ] \addplot[contour prepared, contour prepared format=matlab, contour/labels=false] table[row sep=crcr] {% @@ -533,7 +533,7 @@ xmax=10, ymin=-10, ymax=10, axis background/.style={fill=white}, -title style={font=\bfseries}, +title style={yshift=1ex, font=\tiny\bfseries}, title={u=2} ] \addplot[contour prepared, contour prepared format=matlab, contour/labels=false] table[row sep=crcr] {% @@ -1065,7 +1065,7 @@ xmax=10, ymin=-10, ymax=10, axis background/.style={fill=white}, -title style={font=\bfseries}, +title style={yshift=1ex, font=\tiny\bfseries}, title={u=3} ] \addplot[contour prepared, contour prepared format=matlab, contour/labels=false] table[row sep=crcr] {% @@ -1595,7 +1595,7 @@ xmax=10, ymin=-10, ymax=10, axis background/.style={fill=white}, -title style={font=\bfseries}, +title style={yshift=1ex, font=\tiny\bfseries}, title={u=4} ] \addplot[contour prepared, contour prepared format=matlab, contour/labels=false] table[row sep=crcr] {% @@ -2113,7 +2113,7 @@ xmax=10, ymin=-10, ymax=10, axis background/.style={fill=white}, -title style={font=\bfseries}, +title style={yshift=1ex, font=\tiny\bfseries}, title={u=5} ] \addplot[contour prepared, contour prepared format=matlab, contour/labels=false] table[row sep=crcr] {% @@ -2645,7 +2645,7 @@ xmax=10, ymin=-10, ymax=10, axis background/.style={fill=white}, -title style={font=\bfseries}, +title style={yshift=1ex, font=\tiny\bfseries}, title={u=6} ] \addplot[contour prepared, contour prepared format=matlab, contour/labels=false] table[row sep=crcr] {% @@ -3161,7 +3161,7 @@ xmax=10, ymin=-10, ymax=10, axis background/.style={fill=white}, -title style={font=\bfseries}, +title style={yshift=1ex, font=\tiny\bfseries}, title={u=7} ] \addplot[contour prepared, contour prepared format=matlab, contour/labels=false] table[row sep=crcr] {% @@ -3679,7 +3679,7 @@ xmax=10, ymin=-10, ymax=10, axis background/.style={fill=white}, -title style={font=\bfseries}, +title style={yshift=1ex, font=\tiny\bfseries}, title={u=8} ] \addplot[contour prepared, contour prepared format=matlab, contour/labels=false] table[row sep=crcr] {% @@ -4205,7 +4205,7 @@ xmax=10, ymin=-10, ymax=10, axis background/.style={fill=white}, -title style={font=\bfseries}, +title style={yshift=1ex, font=\tiny\bfseries}, title={u=9} ] \addplot[contour prepared, contour prepared format=matlab, contour/labels=false] table[row sep=crcr] {% @@ -4723,7 +4723,7 @@ xmax=10, ymin=-10, ymax=10, axis background/.style={fill=white}, -title style={font=\bfseries}, +title style={yshift=1ex, font=\tiny\bfseries}, title={u=10} ] \addplot[contour prepared, contour prepared format=matlab, contour/labels=false] table[row sep=crcr] {% diff --git a/hw1/ex3.asv b/hw1/ex3.asv deleted file mode 100644 index 747d631..0000000 --- a/hw1/ex3.asv +++ /dev/null @@ -1,161 +0,0 @@ -%% Homework 1 - Optimization Methods -% Author: Claudio Maggioni -% -% Sources: -% - https://www.youtube.com/watch?v=91RZYO1cv_o - -clear -clc -close all -format short - -%% Exercise 3.1 - -% f(x1, x2) = x1^2 + u * x2^2; - -% 1/2 * [x1 x2] [2 0] [x1] + [0][x1] -% [0 2u] [x2] + [0][x2] - -% A = [1 0; 0 u]; b = [0; 0] - -%% Exercise 3.2 -xaxis = -10:1:10; -yaxis = xaxis; -Zn = zeros(size(xaxis, 2), size(yaxis, 2)); -Zs = {Zn,Zn,Zn,Zn,Zn,Zn,Zn,Zn,Zn,Zn}; - -for u = 1:10 - A = [1 0; 0 u]; - - for i = 1:size(xaxis, 2) - for j = 1:size(yaxis, 2) - vec = [xaxis(i); yaxis(j)]; - Zs{u}(i, j) = vec' * A * vec; - end - end -end - -for u = 1:10 - subplot(2, 5, u); - h = surf(xaxis, yaxis, Zs{u}); - set(h,'LineStyle','none'); - title(sprintf("u=%d", u)); -end -sgtitle("Surf plots"); - -% comment these lines on submission -addpath /home/claudio/git/matlab2tikz/src -matlab2tikz('showInfo', false, './surf.tex') - -figure - -yi = zeros(30, 25); -ni = zeros(30, 25); -its = zeros(30, 1); - -for u = 1:10 - subplot(2, 5, u); - contour(xaxis, yaxis, Zs{u}, 10); - title(sprintf("u=%d", u)); - - -%% Exercise 3.3 - - A = [2 0; 0 2*u]; - b = [0; 0]; - xs = [[0; 10] [10; 0] [10; 10]]; - - syms sx sy - f = 1/2 * [sx sy] * A * [sx; sy]; - - g = gradient(f, [sx; sy]); - - hold on - j = 1; - for x0 = xs - ri = u * 3 - 3 + j; - x = x0; - i = 1; - - xi = zeros(2, 25); - xi(:, 1) = x0; - - yi(ri, 1) = subs(f, [sx sy], x0'); - - while true - p = -1 * double(subs(g, [sx sy], x')); - - ni(ri, i) = log10(norm(p, 2)); - if norm(p, 2) == 0 || ni(ri, i) <= -8 - break - end - - alpha = dot(b - A * x, p) / dot(A * p, p); - - x = x + alpha * p; - i = i + 1; - - xi(:, i) = x; - yi(ri, i) = subs(f, [sx sy], x'); - end - - xi = xi(:, 1:i); - - plot(xi(1, :), xi(2, :), '-'); - - fprintf("u=%2d x0=[%2d,%2d] it=%2d x=[%d,%d]\n", u, ... - x0(1), x0(2), i, x(1), x(2)); - - its(ri) = i; - - j = j + 1; - end - hold off -end -sgtitle("Contour plots and iteration steps"); - -% comment these lines on submission -addpath /home/claudio/git/matlab2tikz/src -matlab2tikz('showInfo', false, './contour.tex') - -figure - -for u = 1:10 - subplot(2, 5, u); - title(sprintf("u=%d", u)); - hold on - for j = 1:3 - ri = u * 3 - 3 + j; - vec = yi(ri, :); - vec = vec(1:its(ri)); - - plot(1:its(ri), vec); - end - hold off -end -sgtitle("Iterations over values of objective function"); - -% comment these lines on submission -addpath /home/claudio/git/matlab2tikz/src -matlab2tikz('showInfo', false, './yseries.tex') - -figure - -for u = 1:10 - title(sprintf("u=%d", u)); - subplot(2, 5, u); - hold on - for j = 1:3 - ri = u * 3 - 3 + j; - vec = ni(ri, :); - vec = vec(1:its(ri)); - - plot(1:its(ri), vec); - end - hold off -end - -% comment these lines on submission -addpath /home/claudio/git/matlab2tikz/src -matlab2tikz('showInfo', false, './norms.tex') - diff --git a/hw1/ex3.m b/hw1/ex3.m index 2ce7f9d..1896868 100644 --- a/hw1/ex3.m +++ b/hw1/ex3.m @@ -9,6 +9,9 @@ clc close all format short +colw = 3; +colh = 4; + %% Exercise 3.1 % f(x1, x2) = x1^2 + u * x2^2; @@ -36,10 +39,10 @@ for u = 1:10 end for u = 1:10 - subplot(2, 5, u); + subplot(colh, colw, u); h = surf(xaxis, yaxis, Zs{u}); set(h,'LineStyle','none'); - title(sprintf("u=%d", u)); + title(sprintf("\nu=%d", u)); end sgtitle("Surf plots"); @@ -54,9 +57,9 @@ ni = zeros(30, 25); its = zeros(30, 1); for u = 1:10 - subplot(2, 5, u); + subplot(colh, colw, u); contour(xaxis, yaxis, Zs{u}, 10); - title(sprintf("u=%d", u)); + title(sprintf("\nu=%d", u)); %% Exercise 3.3 @@ -121,8 +124,8 @@ matlab2tikz('showInfo', false, './contour.tex') figure for u = 1:10 - subplot(2, 5, u); - title(sprintf("u=%d", u)); + subplot(colh, colw, u); + title(sprintf("\nu=%d", u)); hold on for j = 1:3 ri = u * 3 - 3 + j; @@ -142,7 +145,7 @@ matlab2tikz('showInfo', false, './yseries.tex') figure for u = 1:10 - subplot(2, 5, u); + subplot(colh, colw, u); hold on for j = 1:3 ri = u * 3 - 3 + j; @@ -152,7 +155,7 @@ for u = 1:10 plot(1:its(ri), vec); end hold off - title(sprintf("u=%d", u)); + title(sprintf("\nu=%d", u)); end sgtitle("Iterations over log10 of gradient norms"); diff --git a/hw1/main.pdf b/hw1/main.pdf index e36bf9d..15ef9d1 100644 Binary files a/hw1/main.pdf and b/hw1/main.pdf differ diff --git a/hw1/main.tex b/hw1/main.tex index fe92631..a059a4d 100644 --- a/hw1/main.tex +++ b/hw1/main.tex @@ -1,11 +1,28 @@ \documentclass{scrartcl} \usepackage[utf8]{inputenc} +\usepackage{graphicx} +\usepackage{subcaption} \usepackage{amsmath} \usepackage{pgfplots} -\pgfplotsset{compat=1.17} -\usepackage{mathrsfs} -\usepackage{hyperref} -\usetikzlibrary{arrows} +\pgfplotsset{compat=newest} +\usetikzlibrary{plotmarks} +\usetikzlibrary{arrows.meta} +\usepgfplotslibrary{patchplots} +\usepackage{grffile} +\usepackage{amsmath} +\usepackage{subcaption} +\usepgfplotslibrary{external} +\tikzexternalize +\usepackage[margin=2.5cm]{geometry} + +% To compile: +% sed -i 's#title style={font=\\bfseries#title style={yshift=1ex, font=\\tiny\\bfseries#' *.tex +% luatex -enable-write18 -shellescape main.tex + +\pgfplotsset{every x tick label/.append style={font=\tiny, yshift=0.5ex}} +\pgfplotsset{every title/.append style={font=\tiny, align=center}} +\pgfplotsset{every y tick label/.append style={font=\tiny, xshift=0.5ex}} +\pgfplotsset{every z tick label/.append style={font=\tiny, xshift=0.5ex}} \setlength{\parindent}{0cm} \setlength{\parskip}{0.5\baselineskip} @@ -88,7 +105,13 @@ where: The graphs generated by MATLAB are shown below: -\resizebox{\textwidth}{!}{\input{ex32.tex}} +\resizebox{\textwidth}{!}{\input{surf.tex}} + +\resizebox{\textwidth}{!}{\input{contour.tex}} + +\resizebox{\textwidth}{!}{\input{yseries.tex}} + +\resizebox{\textwidth}{!}{\input{norms.tex}} Isolines get stretched along the y axis as $\mu$ increases. For a large $\mu$, points well far away from the axes could be a problem since picking search directions and steps using a naive gradient based method iterations will zig-zag to the minimizer reaching it slowly. diff --git a/hw1/norms.tex b/hw1/norms.tex index 8250ec6..e4479ac 100644 --- a/hw1/norms.tex +++ b/hw1/norms.tex @@ -17,7 +17,7 @@ xmax=2, ymin=1.3, ymax=1.45154499349597, axis background/.style={fill=white}, -title style={font=\bfseries}, +title style={yshift=1ex, font=\tiny\bfseries}, title={u=1}, axis x line*=bottom, axis y line*=left @@ -50,7 +50,7 @@ xmax=20, ymin=-10, ymax=2, axis background/.style={fill=white}, -title style={font=\bfseries}, +title style={yshift=1ex, font=\tiny\bfseries}, title={u=2}, axis x line*=bottom, axis y line*=left @@ -99,7 +99,7 @@ xmax=22, ymin=-10, ymax=2, axis background/.style={fill=white}, -title style={font=\bfseries}, +title style={yshift=1ex, font=\tiny\bfseries}, title={u=3}, axis x line*=bottom, axis y line*=left @@ -152,7 +152,7 @@ xmax=22, ymin=-10, ymax=2, axis background/.style={fill=white}, -title style={font=\bfseries}, +title style={yshift=1ex, font=\tiny\bfseries}, title={u=4}, axis x line*=bottom, axis y line*=left @@ -205,7 +205,7 @@ xmax=22, ymin=-10, ymax=2.00851666964939, axis background/.style={fill=white}, -title style={font=\bfseries}, +title style={yshift=1ex, font=\tiny\bfseries}, title={u=5}, axis x line*=bottom, axis y line*=left @@ -258,7 +258,7 @@ xmax=22, ymin=-10, ymax=2.08513085769748, axis background/.style={fill=white}, -title style={font=\bfseries}, +title style={yshift=1ex, font=\tiny\bfseries}, title={u=6}, axis x line*=bottom, axis y line*=left @@ -311,7 +311,7 @@ xmax=20, ymin=-10, ymax=2.15051499783199, axis background/.style={fill=white}, -title style={font=\bfseries}, +title style={yshift=1ex, font=\tiny\bfseries}, title={u=7}, axis x line*=bottom, axis y line*=left @@ -362,7 +362,7 @@ xmax=20, ymin=-10, ymax=2.20748667398541, axis background/.style={fill=white}, -title style={font=\bfseries}, +title style={yshift=1ex, font=\tiny\bfseries}, title={u=8}, axis x line*=bottom, axis y line*=left @@ -413,7 +413,7 @@ xmax=20, ymin=-10, ymax=2.25793692185584, axis background/.style={fill=white}, -title style={font=\bfseries}, +title style={yshift=1ex, font=\tiny\bfseries}, title={u=9}, axis x line*=bottom, axis y line*=left @@ -464,7 +464,7 @@ xmax=20, ymin=-10, ymax=5, axis background/.style={fill=white}, -title style={font=\bfseries}, +title style={yshift=1ex, font=\tiny\bfseries}, title={u=10}, axis x line*=bottom, axis y line*=left diff --git a/hw1/surf.tex b/hw1/surf.tex index 63e3c66..1387164 100644 --- a/hw1/surf.tex +++ b/hw1/surf.tex @@ -16,7 +16,7 @@ zmin=0, zmax=200, view={-37.5}{30}, axis background/.style={fill=white}, -title style={font=\bfseries}, +title style={yshift=1ex, font=\tiny\bfseries}, title={u=1}, axis x line*=bottom, axis y line*=left, @@ -490,7 +490,7 @@ zmin=0, zmax=300, view={-37.5}{30}, axis background/.style={fill=white}, -title style={font=\bfseries}, +title style={yshift=1ex, font=\tiny\bfseries}, title={u=2}, axis x line*=bottom, axis y line*=left, @@ -964,7 +964,7 @@ zmin=0, zmax=400, view={-37.5}{30}, axis background/.style={fill=white}, -title style={font=\bfseries}, +title style={yshift=1ex, font=\tiny\bfseries}, title={u=3}, axis x line*=bottom, axis y line*=left, @@ -1438,7 +1438,7 @@ zmin=0, zmax=600, view={-37.5}{30}, axis background/.style={fill=white}, -title style={font=\bfseries}, +title style={yshift=1ex, font=\tiny\bfseries}, title={u=4}, axis x line*=bottom, axis y line*=left, @@ -1912,7 +1912,7 @@ zmin=0, zmax=600, view={-37.5}{30}, axis background/.style={fill=white}, -title style={font=\bfseries}, +title style={yshift=1ex, font=\tiny\bfseries}, title={u=5}, axis x line*=bottom, axis y line*=left, @@ -2386,7 +2386,7 @@ zmin=0, zmax=700, view={-37.5}{30}, axis background/.style={fill=white}, -title style={font=\bfseries}, +title style={yshift=1ex, font=\tiny\bfseries}, title={u=6}, axis x line*=bottom, axis y line*=left, @@ -2860,7 +2860,7 @@ zmin=0, zmax=1000, view={-37.5}{30}, axis background/.style={fill=white}, -title style={font=\bfseries}, +title style={yshift=1ex, font=\tiny\bfseries}, title={u=7}, axis x line*=bottom, axis y line*=left, @@ -3334,7 +3334,7 @@ zmin=0, zmax=1000, view={-37.5}{30}, axis background/.style={fill=white}, -title style={font=\bfseries}, +title style={yshift=1ex, font=\tiny\bfseries}, title={u=8}, axis x line*=bottom, axis y line*=left, @@ -3808,7 +3808,7 @@ zmin=0, zmax=1000, view={-37.5}{30}, axis background/.style={fill=white}, -title style={font=\bfseries}, +title style={yshift=1ex, font=\tiny\bfseries}, title={u=9}, axis x line*=bottom, axis y line*=left, @@ -4282,7 +4282,7 @@ zmin=0, zmax=1100, view={-37.5}{30}, axis background/.style={fill=white}, -title style={font=\bfseries}, +title style={yshift=1ex, font=\tiny\bfseries}, title={u=10}, axis x line*=bottom, axis y line*=left, diff --git a/hw1/yseries.tex b/hw1/yseries.tex index 5ba8fef..966817b 100644 --- a/hw1/yseries.tex +++ b/hw1/yseries.tex @@ -16,7 +16,7 @@ xmax=2, ymin=0, ymax=200, axis background/.style={fill=white}, -title style={font=\bfseries}, +title style={yshift=1ex, font=\tiny\bfseries}, title={u=1}, axis x line*=bottom, axis y line*=left @@ -48,7 +48,7 @@ xmax=20, ymin=0, ymax=300, axis background/.style={fill=white}, -title style={font=\bfseries}, +title style={yshift=1ex, font=\tiny\bfseries}, title={u=2}, axis x line*=bottom, axis y line*=left @@ -96,7 +96,7 @@ xmax=22, ymin=0, ymax=400, axis background/.style={fill=white}, -title style={font=\bfseries}, +title style={yshift=1ex, font=\tiny\bfseries}, title={u=3}, axis x line*=bottom, axis y line*=left @@ -148,7 +148,7 @@ xmax=22, ymin=0, ymax=500, axis background/.style={fill=white}, -title style={font=\bfseries}, +title style={yshift=1ex, font=\tiny\bfseries}, title={u=4}, axis x line*=bottom, axis y line*=left @@ -200,7 +200,7 @@ xmax=22, ymin=0, ymax=600, axis background/.style={fill=white}, -title style={font=\bfseries}, +title style={yshift=1ex, font=\tiny\bfseries}, title={u=5}, axis x line*=bottom, axis y line*=left @@ -252,7 +252,7 @@ xmax=22, ymin=0, ymax=800, axis background/.style={fill=white}, -title style={font=\bfseries}, +title style={yshift=1ex, font=\tiny\bfseries}, title={u=6}, axis x line*=bottom, axis y line*=left @@ -304,7 +304,7 @@ xmax=20, ymin=0, ymax=800, axis background/.style={fill=white}, -title style={font=\bfseries}, +title style={yshift=1ex, font=\tiny\bfseries}, title={u=7}, axis x line*=bottom, axis y line*=left @@ -354,7 +354,7 @@ xmax=20, ymin=0, ymax=1000, axis background/.style={fill=white}, -title style={font=\bfseries}, +title style={yshift=1ex, font=\tiny\bfseries}, title={u=8}, axis x line*=bottom, axis y line*=left @@ -404,7 +404,7 @@ xmax=20, ymin=0, ymax=1000, axis background/.style={fill=white}, -title style={font=\bfseries}, +title style={yshift=1ex, font=\tiny\bfseries}, title={u=9}, axis x line*=bottom, axis y line*=left @@ -454,7 +454,7 @@ xmax=20, ymin=0, ymax=1200, axis background/.style={fill=white}, -title style={font=\bfseries}, +title style={yshift=1ex, font=\tiny\bfseries}, title={u=10}, axis x line*=bottom, axis y line*=left