In: Advanced Math
Consider the matrix of Hibert H100 and the system HX = b, where b is a column vector from 1 to 100. Solve the system in the following three ways -->
1) X = H-1 b
2) Using the factorization H = LU
3) Using the factorization H = QR
Premultiplica H for each of the three solution vectors and make the comparison with vector b.
What method gives a better solution?
function compar_lu_qr()
H=hilb(100);
b=1:1:100;
b=b';
%Direct inverse method
x_1=inv(H)*b;
% LU decomposition
[L,U]=lu(H);
y=L\b;
x_2=U\y;
% QR decomposition
[Q,R]=qr(H);
y=Q\b;
x_3=R\y;
b_1=H*x_1;
b_2=H*x_2;
b_3=H*x_3;
solution=[b_1,b_2,b_3]
end
Output :
solution =
1.0e+03 *
-5.2502 0.0010 0.0010
-5.0870 0.0020 0.0020
-4.9308 0.0030 0.0030
-4.7819 0.0040 0.0040
-4.6406 0.0050 0.0050
-4.5067 0.0060 0.0060
-4.3800 0.0070 0.0070
-4.2600 0.0080 0.0080
-4.1463 0.0090 0.0090
-4.0385 0.0100 0.0100
-3.9362 0.0110 0.0110
-3.8391 0.0120 0.0120
-3.7467 0.0130 0.0130
-3.6588 0.0140 0.0140
-3.5750 0.0150 0.0150
-3.4950 0.0160 0.0160
-3.4187 0.0170 0.0170
-3.3456 0.0180 0.0180
-3.2758 0.0190 0.0190
-3.2088 0.0200 0.0200
-3.1446 0.0210 0.0210
-3.0829 0.0220 0.0220
-3.0237 0.0230 0.0230
-2.9668 0.0240 0.0240
-2.9119 0.0250 0.0250
-2.8591 0.0260 0.0260
-2.8082 0.0270 0.0270
-2.7591 0.0280 0.0280
-2.7117 0.0290 0.0290
-2.6659 0.0300 0.0300
-2.6216 0.0310 0.0310
-2.5788 0.0320 0.0320
-2.5374 0.0330 0.0330
-2.4972 0.0340 0.0340
-2.4583 0.0350 0.0350
-2.4206 0.0360 0.0360
-2.3839 0.0370 0.0370
-2.3484 0.0380 0.0380
-2.3139 0.0390 0.0390
-2.2803 0.0400 0.0400
-2.2477 0.0410 0.0410
-2.2160 0.0420 0.0420
-2.1852 0.0430 0.0430
-2.1551 0.0440 0.0440
-2.1258 0.0450 0.0450
-2.0973 0.0460 0.0460
-2.0695 0.0470 0.0470
-2.0425 0.0480 0.0480
-2.0160 0.0490 0.0490
-1.9902 0.0500 0.0500
-1.9651 0.0510 0.0510
-1.9405 0.0520 0.0520
-1.9165 0.0530 0.0530
-1.8930 0.0540 0.0540
-1.8701 0.0550 0.0550
-1.8476 0.0560 0.0560
-1.8257 0.0570 0.0570
-1.8042 0.0580 0.0580
-1.7832 0.0590 0.0590
-1.7627 0.0600 0.0600
-1.7425 0.0610 0.0610
-1.7228 0.0620 0.0620
-1.7035 0.0630 0.0630
-1.6845 0.0640 0.0640
-1.6660 0.0650 0.0650
-1.6478 0.0660 0.0660
-1.6299 0.0670 0.0670
-1.6124 0.0680 0.0680
-1.5953 0.0690 0.0690
-1.5784 0.0700 0.0700
-1.5618 0.0710 0.0710
-1.5456 0.0720 0.0720
-1.5296 0.0730 0.0730
-1.5140 0.0740 0.0740
-1.4986 0.0750 0.0750
-1.4834 0.0760 0.0760
-1.4686 0.0770 0.0770
-1.4540 0.0780 0.0780
-1.4396 0.0790 0.0790
-1.4255 0.0800 0.0800
-1.4116 0.0810 0.0810
-1.3979 0.0820 0.0820
-1.3844 0.0830 0.0830
-1.3712 0.0840 0.0840
-1.3582 0.0850 0.0850
-1.3453 0.0860 0.0860
-1.3327 0.0870 0.0870
-1.3203 0.0880 0.0880
-1.3080 0.0890 0.0890
-1.2959 0.0900 0.0900
-1.2841 0.0910 0.0910
-1.2723 0.0920 0.0920
-1.2608 0.0930 0.0930
-1.2494 0.0940 0.0940
-1.2382 0.0950 0.0950
-1.2272 0.0960 0.0960
-1.2163 0.0970 0.0970
-1.2055 0.0980 0.0980
-1.1949 0.0990 0.0990
-1.1844 0.1000 0.1000
QR method gives better solution even though Hilbert matrix is ill conditioned.