diff --git a/mp5/Project_5_Maggioni_Claudio/code_template.asv b/mp5/Project_5_Maggioni_Claudio/code_template.asv new file mode 100644 index 0000000..3c7199f --- /dev/null +++ b/mp5/Project_5_Maggioni_Claudio/code_template.asv @@ -0,0 +1,41 @@ +close all; +clear; clc; + +%% Load Default Img Data +load('blur_data/B.mat'); +B=double(B); +load('blur_data/A.mat'); +A=double(A); +ciao = A; + +% Show Image +figure +im_l=min(min(B)); +im_u=max(max(B)); +imshow(B,[im_l,im_u]) +title('Blured Image') + +% Vectorize the image (row by row) +b=B'; +b=b(:); + +%IL = ichol(A, struct('type', 'nofill', 'diagcomp', 0)); +y = IL \ b; +x0 = IL' \ y; + +[x, rvec] = myCG(A, b, diag(IL), 200, 1e-6); +semilogy(rvec); + +[X2,flag,~,~,rvec2] = pcg(A, b, 1e-6, 200); + + + +%% Validate Test values +load('test_data/A_test.mat'); +load('test_data/x_test_exact.mat'); +load('test_data/b_test.mat'); + +%res=||x^*-A^{-1}b|| +res=x_test_exact-inv(A_test)*b_test; +norm(res); +%(Now do it with your CG and Matlab's PCG routine!!!) diff --git a/mp5/Project_5_Maggioni_Claudio/code_template.m b/mp5/Project_5_Maggioni_Claudio/code_template.m index 3a0edb7..1ceabd1 100644 --- a/mp5/Project_5_Maggioni_Claudio/code_template.m +++ b/mp5/Project_5_Maggioni_Claudio/code_template.m @@ -1,9 +1,13 @@ close all; clear; clc; +addpath /Users/maggicl/Git/matlab2tikz/src/; %% Load Default Img Data load('blur_data/B.mat'); B=double(B); +load('blur_data/A.mat'); +A=double(A); +ciao = A; % Show Image figure @@ -16,6 +20,54 @@ title('Blured Image') b=B'; b=b(:); +AT = A * A'; +bt = A' * b; + +IL = ichol(AT, struct('type', 'nofill', 'diagcomp', 0.01)); +y = IL \ b; +x0 = IL' \ y; + +[x, rvec] = myCG(AT, bt, x0, 200, 1e-6); +[x2, ~, ~, ~, rvec2] = pcg(AT, bt, 1e-6, 200, IL, IL'); + +figure; +semilogy(rvec); +hold on; +semilogy(rvec2); +hold off; +title('Residual norms over iteration (y is log)') +matlab2tikz('showInfo', false, '../res_log.tex'); + + +X = zeros(250, 250); +for i = 0:249 + for j = 1:250 + X(i + 1, j) = x(i * 250 + j); + end +end + +figure; +im_l=min(min(X)); +im_u=max(max(X)); +imshow(X,[im_l,im_u]) +title('Sharp Image (myCG)') +matlab2tikz('showInfo', false, '../img_my.tex'); + + +X2 = zeros(250, 250); +for i = 0:249 + for j = 1:250 + X2(i + 1, j) = x2(i * 250 + j); + end +end + +figure; +im_l=min(min(X2)); +im_u=max(max(X2)); +imshow(X2,[im_l,im_u]) +title('Sharp Image (rcg)') +matlab2tikz('showInfo', false, '../img_rcg.tex'); + %% Validate Test values @@ -24,6 +76,6 @@ load('test_data/x_test_exact.mat'); load('test_data/b_test.mat'); %res=||x^*-A^{-1}b|| -res=x_test_exact-inv(A_test)*b_test -norm(res) +res=x_test_exact-inv(A_test)*b_test; +norm(res); %(Now do it with your CG and Matlab's PCG routine!!!) diff --git a/mp5/img_my.tex b/mp5/img_my.tex new file mode 100644 index 0000000..7d15c02 --- /dev/null +++ b/mp5/img_my.tex @@ -0,0 +1,257 @@ +% This file was created by matlab2tikz. +% +\definecolor{mycolor1}{rgb}{0.00000,0.44700,0.74100}% +\definecolor{mycolor2}{rgb}{0.85000,0.32500,0.09800}% +% +\begin{tikzpicture} + +\begin{axis}[% +width=4.521in, +height=3.566in, +at={(0.758in,0.481in)}, +scale only axis, +xmin=0, +xmax=250, +ymode=log, +ymin=1e-06, +ymax=118.544185368288, +yminorticks=true, +axis background/.style={fill=white}, +title style={font=\bfseries}, +title={Sharp Image (myCG)} +] +\addplot [color=mycolor1, forget plot] + table[row sep=crcr]{% +1 11.1886780605947\\ +2 37.688016651504\\ +3 20.5612191459901\\ +4 10.1126098486914\\ +5 6.60295496425051\\ +6 4.03660691189902\\ +7 2.83696084221764\\ +8 2.02776259797851\\ +9 1.55623996384361\\ +10 1.26373343652138\\ +11 0.968080560648255\\ +12 0.808014633949812\\ +13 0.666010141343337\\ +14 0.547935882226161\\ +15 0.455778233603678\\ +16 0.379140935249178\\ +17 0.325385471261729\\ +18 0.27468130551472\\ +19 0.241318492631058\\ +20 0.203089896725741\\ +21 0.186402910640978\\ +22 0.164285195168731\\ +23 0.161498606695505\\ +24 0.13554465057248\\ +25 0.108256329622331\\ +26 0.096685060868788\\ +27 0.0901032819163635\\ +28 0.0846560423043152\\ +29 0.0720452421305809\\ +30 0.0638081561682231\\ +31 0.0586938472203618\\ +32 0.0524931123015411\\ +33 0.0500311324114693\\ +34 0.0441377829780386\\ +35 0.0430660141184194\\ +36 0.0399007401033348\\ +37 0.0357338939687774\\ +38 0.0334582949737502\\ +39 0.0301143079798452\\ +40 0.0277450095617363\\ +41 0.0246457162237748\\ +42 0.0226451015644127\\ +43 0.0214585559097522\\ +44 0.0197750620834928\\ +45 0.0184656387170807\\ +46 0.01424126114094\\ +47 0.014925314691824\\ +48 0.0138939777471367\\ +49 0.0124854196790047\\ +50 0.0121443393806818\\ +51 0.0111248201462271\\ +52 0.0117048407914948\\ +53 0.0113323279545552\\ +54 0.0106729739632728\\ +55 0.00944425804326873\\ +56 0.00871066933482761\\ +57 0.00881210600673333\\ +58 0.00795156096327054\\ +59 0.00726613636631538\\ +60 0.00700470814378812\\ +61 0.00621935380743637\\ +62 0.00580029695866593\\ +63 0.00557779006381989\\ +64 0.00510498070037545\\ +65 0.0047009622833176\\ +66 0.00438789538442034\\ +67 0.00435585711109691\\ +68 0.00396640165581629\\ +69 0.00393477324085466\\ +70 0.00371617606606877\\ +71 0.00327886320159879\\ +72 0.00312237367539779\\ +73 0.00299349690875735\\ +74 0.00284074963413765\\ +75 0.00287860679448775\\ +76 0.0027731320103853\\ +77 0.00263952084059243\\ +78 0.00233720240014001\\ +79 0.00212697729708345\\ +80 0.00199788688851914\\ +81 0.00191003161317315\\ +82 0.00189242158319258\\ +83 0.00174836410231261\\ +84 0.0016140985772839\\ +85 0.00153318949743614\\ +86 0.00147245713810495\\ +87 0.00150762346036416\\ +88 0.00141698818640003\\ +89 0.00132612374554337\\ +90 0.0012294024439175\\ +91 0.00124357808130644\\ +92 0.00117351874267278\\ +93 0.00118805545766119\\ +94 0.00114318803197479\\ +95 0.00110915873689051\\ +96 0.00101959224657441\\ +97 0.00102415265668903\\ +98 0.000975302119471475\\ +99 0.000987996366258813\\ +100 0.00095742632740931\\ +101 0.000934265103971271\\ +102 0.000867894211150555\\ +103 0.000857812123034294\\ +104 0.000756099336226914\\ +105 0.000691162784782045\\ +106 0.000695473018163751\\ +107 0.000633925014757231\\ +108 0.000590287819831721\\ +109 0.000535337664369594\\ +110 0.000538252780320825\\ +111 0.000525826256051619\\ +112 0.000497879429315293\\ +113 0.000454616010876659\\ +114 0.000458838481347935\\ +115 0.000424839125494755\\ +116 0.000394335524998984\\ +117 0.000385945051019674\\ +118 0.000369692359961463\\ +119 0.000344267565220491\\ +120 0.000327893992619588\\ +121 0.000325828293258797\\ +122 0.000289237146884203\\ +123 0.000285070703523818\\ +124 0.000254192839215692\\ +125 0.000243444971741076\\ +126 0.000232080954193898\\ +127 0.000232619238643129\\ +128 0.000221784687572175\\ +129 0.000220230466851297\\ +130 0.000211029395043088\\ +131 0.000193736708718616\\ +132 0.000173392267570507\\ +133 0.000177242279228775\\ +134 0.000176692274847942\\ +135 0.000163578052215373\\ +136 0.000154604812916662\\ +137 0.000144545518887022\\ +138 0.00014456301163485\\ +139 0.000142459358743701\\ +140 0.0001308824450235\\ +141 0.000115457857714896\\ +142 0.00011695471803412\\ +143 0.000113103879953932\\ +144 0.000105257482462195\\ +145 0.000106888699118678\\ +146 0.000102374869876372\\ +147 9.76413644339824e-05\\ +148 8.4851357550636e-05\\ +149 7.68259382254786e-05\\ +150 7.45049326964518e-05\\ +151 7.30876640535304e-05\\ +152 7.5125116902824e-05\\ +153 7.84305708028061e-05\\ +154 8.45501238033231e-05\\ +155 8.17331778157062e-05\\ +156 7.55108766404751e-05\\ +157 7.48398655259728e-05\\ +158 7.14089505651308e-05\\ +159 6.99224620714426e-05\\ +160 7.04197347847714e-05\\ +161 6.53525683701177e-05\\ +162 6.08288863097357e-05\\ +163 5.89421929587584e-05\\ +164 5.72484731968775e-05\\ +165 5.18959286578684e-05\\ +166 5.16052819357681e-05\\ +167 5.18373975156787e-05\\ +168 5.1438260693119e-05\\ +169 4.74780120078228e-05\\ +170 4.76692285194498e-05\\ +171 4.63049226435535e-05\\ +172 4.3021574259984e-05\\ +173 4.34691080338747e-05\\ +174 4.07308380659776e-05\\ +175 3.92273577862465e-05\\ +176 3.78686988818524e-05\\ +177 3.75780813333193e-05\\ +178 3.89427911494855e-05\\ +179 3.55021611298483e-05\\ +180 3.21423511450592e-05\\ +181 3.1976175682275e-05\\ +182 3.15208931332488e-05\\ +183 3.02809343645595e-05\\ +184 2.93752192595988e-05\\ +185 2.83798341644248e-05\\ +186 2.84874468500327e-05\\ +187 2.6372748821241e-05\\ +188 2.83572106563901e-05\\ +189 2.73404573666424e-05\\ +190 2.64050982491902e-05\\ +191 2.68346097335579e-05\\ +192 2.66791537496404e-05\\ +193 2.44823861206895e-05\\ +194 2.44167662294569e-05\\ +195 2.42329915293711e-05\\ +196 2.57959950015393e-05\\ +197 2.5330918832603e-05\\ +198 2.43122483048608e-05\\ +199 2.29066675778814e-05\\ +200 2.28422313595056e-05\\ +201 2.2009103500163e-05\\ +}; +\addplot [color=mycolor2, forget plot] + table[row sep=crcr]{% +1 118.544185368288\\ +2 0.687337944312083\\ +3 0.0352052378196977\\ +4 0.0177116530504275\\ +5 0.00921115423330148\\ +6 0.00742079353381528\\ +7 0.00435978025623298\\ +8 0.00269077966124769\\ +9 0.00326151218772164\\ +10 0.00207394458931424\\ +11 0.00295893040643868\\ +12 0.00133204038205353\\ +13 0.00183522720801244\\ +14 0.00145996981575274\\ +15 0.000990797743084482\\ +16 0.00126621063572918\\ +17 0.000551496956428083\\ +18 0.000515017395303755\\ +19 0.000232316315215174\\ +20 0.000223232804362511\\ +21 0.000208790455128303\\ +22 0.000151172313680646\\ +23 0.000161588020568097\\ +24 0.000122705865740307\\ +25 0.000118687703674217\\ +26 0.000113287133295243\\ +}; +\end{axis} +\end{tikzpicture}% \ No newline at end of file diff --git a/mp5/img_rcg-1.png b/mp5/img_rcg-1.png new file mode 100644 index 0000000..a5b68fd Binary files /dev/null and b/mp5/img_rcg-1.png differ diff --git a/mp5/img_rcg.tex b/mp5/img_rcg.tex new file mode 100644 index 0000000..85b5ede --- /dev/null +++ b/mp5/img_rcg.tex @@ -0,0 +1,26 @@ +% This file was created by matlab2tikz. +% +\begin{tikzpicture} + +\begin{axis}[% +width=2.604in, +height=2.604in, +at={(0.962in,0.66in)}, +scale only axis, +point meta min=-0.0235938830592846, +point meta max=0.993939917509565, +axis on top, +xmin=0.5, +xmax=250.5, +tick align=outside, +y dir=reverse, +ymin=0.5, +ymax=250.5, +axis line style={draw=none}, +ticks=none, +title style={font=\bfseries}, +title={Sharp Image (rcg)} +] +\addplot [forget plot] graphics [xmin=0.5, xmax=250.5, ymin=0.5, ymax=250.5] {img_rcg-1.png}; +\end{axis} +\end{tikzpicture}% \ No newline at end of file diff --git a/mp5/res_log.tex b/mp5/res_log.tex new file mode 100644 index 0000000..b46fa1b --- /dev/null +++ b/mp5/res_log.tex @@ -0,0 +1,257 @@ +% This file was created by matlab2tikz. +% +\definecolor{mycolor1}{rgb}{0.00000,0.44700,0.74100}% +\definecolor{mycolor2}{rgb}{0.85000,0.32500,0.09800}% +% +\begin{tikzpicture} + +\begin{axis}[% +width=4.521in, +height=3.566in, +at={(0.758in,0.481in)}, +scale only axis, +xmin=0, +xmax=250, +ymode=log, +ymin=1e-06, +ymax=118.544185368288, +yminorticks=true, +axis background/.style={fill=white}, +title style={font=\bfseries}, +title={Residual norms over iteration (y is log)} +] +\addplot [color=mycolor1, forget plot] + table[row sep=crcr]{% +1 11.1886780605947\\ +2 37.688016651504\\ +3 20.5612191459901\\ +4 10.1126098486914\\ +5 6.60295496425051\\ +6 4.03660691189902\\ +7 2.83696084221764\\ +8 2.02776259797851\\ +9 1.55623996384361\\ +10 1.26373343652138\\ +11 0.968080560648255\\ +12 0.808014633949812\\ +13 0.666010141343337\\ +14 0.547935882226161\\ +15 0.455778233603678\\ +16 0.379140935249178\\ +17 0.325385471261729\\ +18 0.27468130551472\\ +19 0.241318492631058\\ +20 0.203089896725741\\ +21 0.186402910640978\\ +22 0.164285195168731\\ +23 0.161498606695505\\ +24 0.13554465057248\\ +25 0.108256329622331\\ +26 0.096685060868788\\ +27 0.0901032819163635\\ +28 0.0846560423043152\\ +29 0.0720452421305809\\ +30 0.0638081561682231\\ +31 0.0586938472203618\\ +32 0.0524931123015411\\ +33 0.0500311324114693\\ +34 0.0441377829780386\\ +35 0.0430660141184194\\ +36 0.0399007401033348\\ +37 0.0357338939687774\\ +38 0.0334582949737502\\ +39 0.0301143079798452\\ +40 0.0277450095617363\\ +41 0.0246457162237748\\ +42 0.0226451015644127\\ +43 0.0214585559097522\\ +44 0.0197750620834928\\ +45 0.0184656387170807\\ +46 0.01424126114094\\ +47 0.014925314691824\\ +48 0.0138939777471367\\ +49 0.0124854196790047\\ +50 0.0121443393806818\\ +51 0.0111248201462271\\ +52 0.0117048407914948\\ +53 0.0113323279545552\\ +54 0.0106729739632728\\ +55 0.00944425804326873\\ +56 0.00871066933482761\\ +57 0.00881210600673333\\ +58 0.00795156096327054\\ +59 0.00726613636631538\\ +60 0.00700470814378812\\ +61 0.00621935380743637\\ +62 0.00580029695866593\\ +63 0.00557779006381989\\ +64 0.00510498070037545\\ +65 0.0047009622833176\\ +66 0.00438789538442034\\ +67 0.00435585711109691\\ +68 0.00396640165581629\\ +69 0.00393477324085466\\ +70 0.00371617606606877\\ +71 0.00327886320159879\\ +72 0.00312237367539779\\ +73 0.00299349690875735\\ +74 0.00284074963413765\\ +75 0.00287860679448775\\ +76 0.0027731320103853\\ +77 0.00263952084059243\\ +78 0.00233720240014001\\ +79 0.00212697729708345\\ +80 0.00199788688851914\\ +81 0.00191003161317315\\ +82 0.00189242158319258\\ +83 0.00174836410231261\\ +84 0.0016140985772839\\ +85 0.00153318949743614\\ +86 0.00147245713810495\\ +87 0.00150762346036416\\ +88 0.00141698818640003\\ +89 0.00132612374554337\\ +90 0.0012294024439175\\ +91 0.00124357808130644\\ +92 0.00117351874267278\\ +93 0.00118805545766119\\ +94 0.00114318803197479\\ +95 0.00110915873689051\\ +96 0.00101959224657441\\ +97 0.00102415265668903\\ +98 0.000975302119471475\\ +99 0.000987996366258813\\ +100 0.00095742632740931\\ +101 0.000934265103971271\\ +102 0.000867894211150555\\ +103 0.000857812123034294\\ +104 0.000756099336226914\\ +105 0.000691162784782045\\ +106 0.000695473018163751\\ +107 0.000633925014757231\\ +108 0.000590287819831721\\ +109 0.000535337664369594\\ +110 0.000538252780320825\\ +111 0.000525826256051619\\ +112 0.000497879429315293\\ +113 0.000454616010876659\\ +114 0.000458838481347935\\ +115 0.000424839125494755\\ +116 0.000394335524998984\\ +117 0.000385945051019674\\ +118 0.000369692359961463\\ +119 0.000344267565220491\\ +120 0.000327893992619588\\ +121 0.000325828293258797\\ +122 0.000289237146884203\\ +123 0.000285070703523818\\ +124 0.000254192839215692\\ +125 0.000243444971741076\\ +126 0.000232080954193898\\ +127 0.000232619238643129\\ +128 0.000221784687572175\\ +129 0.000220230466851297\\ +130 0.000211029395043088\\ +131 0.000193736708718616\\ +132 0.000173392267570507\\ +133 0.000177242279228775\\ +134 0.000176692274847942\\ +135 0.000163578052215373\\ +136 0.000154604812916662\\ +137 0.000144545518887022\\ +138 0.00014456301163485\\ +139 0.000142459358743701\\ +140 0.0001308824450235\\ +141 0.000115457857714896\\ +142 0.00011695471803412\\ +143 0.000113103879953932\\ +144 0.000105257482462195\\ +145 0.000106888699118678\\ +146 0.000102374869876372\\ +147 9.76413644339824e-05\\ +148 8.4851357550636e-05\\ +149 7.68259382254786e-05\\ +150 7.45049326964518e-05\\ +151 7.30876640535304e-05\\ +152 7.5125116902824e-05\\ +153 7.84305708028061e-05\\ +154 8.45501238033231e-05\\ +155 8.17331778157062e-05\\ +156 7.55108766404751e-05\\ +157 7.48398655259728e-05\\ +158 7.14089505651308e-05\\ +159 6.99224620714426e-05\\ +160 7.04197347847714e-05\\ +161 6.53525683701177e-05\\ +162 6.08288863097357e-05\\ +163 5.89421929587584e-05\\ +164 5.72484731968775e-05\\ +165 5.18959286578684e-05\\ +166 5.16052819357681e-05\\ +167 5.18373975156787e-05\\ +168 5.1438260693119e-05\\ +169 4.74780120078228e-05\\ +170 4.76692285194498e-05\\ +171 4.63049226435535e-05\\ +172 4.3021574259984e-05\\ +173 4.34691080338747e-05\\ +174 4.07308380659776e-05\\ +175 3.92273577862465e-05\\ +176 3.78686988818524e-05\\ +177 3.75780813333193e-05\\ +178 3.89427911494855e-05\\ +179 3.55021611298483e-05\\ +180 3.21423511450592e-05\\ +181 3.1976175682275e-05\\ +182 3.15208931332488e-05\\ +183 3.02809343645595e-05\\ +184 2.93752192595988e-05\\ +185 2.83798341644248e-05\\ +186 2.84874468500327e-05\\ +187 2.6372748821241e-05\\ +188 2.83572106563901e-05\\ +189 2.73404573666424e-05\\ +190 2.64050982491902e-05\\ +191 2.68346097335579e-05\\ +192 2.66791537496404e-05\\ +193 2.44823861206895e-05\\ +194 2.44167662294569e-05\\ +195 2.42329915293711e-05\\ +196 2.57959950015393e-05\\ +197 2.5330918832603e-05\\ +198 2.43122483048608e-05\\ +199 2.29066675778814e-05\\ +200 2.28422313595056e-05\\ +201 2.2009103500163e-05\\ +}; +\addplot [color=mycolor2, forget plot] + table[row sep=crcr]{% +1 118.544185368288\\ +2 0.687337944312083\\ +3 0.0352052378196977\\ +4 0.0177116530504275\\ +5 0.00921115423330148\\ +6 0.00742079353381528\\ +7 0.00435978025623298\\ +8 0.00269077966124769\\ +9 0.00326151218772164\\ +10 0.00207394458931424\\ +11 0.00295893040643868\\ +12 0.00133204038205353\\ +13 0.00183522720801244\\ +14 0.00145996981575274\\ +15 0.000990797743084482\\ +16 0.00126621063572918\\ +17 0.000551496956428083\\ +18 0.000515017395303755\\ +19 0.000232316315215174\\ +20 0.000223232804362511\\ +21 0.000208790455128303\\ +22 0.000151172313680646\\ +23 0.000161588020568097\\ +24 0.000122705865740307\\ +25 0.000118687703674217\\ +26 0.000113287133295243\\ +}; +\end{axis} +\end{tikzpicture}% \ No newline at end of file