SAMPLE SOLUTION TO PROBLEM #2 (PAGE 77)
ITEM A) We first generate and 8x8 matrix and an 8-vector both having integers
entries between 0 and 10. You can use other commands to generate random
matrices here (as the one given in the syllabus, for example):
>> A=round(10*rand(8))
A =
10 8 9 1 4 8 3 4
2 4 9 2 9 0 2 9
6 6 4 2 5 7 2 9
5 8 9 6 4 4 7 6
9 9 1 3 8 8 3 5
8 7 4 2 5 5 5 9
5 2 8 0 2 7 2 8
0 4 0 7 7 4 7 6
>> b=round(10*rand(8,1))
b =
8
7
3
3
3
5
7
3
Here we reset the number of flops to zero
>> flops(0)
and solve the liner system usin "\" command of MATLAB:
>> x=A\b
x =
-2.5388
1.7280
0.4568
-4.4718
0.9337
1.2454
2.6135
-0.4036
>> flops
ans =
862
The program has performed 862 operations to generate this solution.
ITEM B) We reset the flops to zero again
>> flops(0)
and find the solution of the system by Gauss-Jordan reduction method:
>> U=rref([A b])
U =
Columns 1 through 7
1.0000 0 0 0 0 0 0
0 1.0000 0 0 0 0 0
0 0 1.0000 0 0 0 0
0 0 0 1.0000 0 0 0
0 0 0 0 1.0000 0 0
0 0 0 0 0 1.0000 0
0 0 0 0 0 0 1.0000
0 0 0 0 0 0 0
Columns 8 through 9
0 -2.5388
0 1.7280
0 0.4568
0 -4.4718
0 0.9337
0 1.2455
0 2.6135
1.0000 -0.4036
Here the last column is the solution and we set it equal to y by
>> y=U(1:8,9:9)
y =
-2.5388
1.7280
0.4568
-4.4718
0.9337
1.2455
2.6135
-0.4036
We now check the number of operations performed by the program
>> flops
ans =
2909
Clearly the first method used is more efficient.
ITEM C) We now have two solutions of the same linear system. We called
it x and y. Ideally they should be the same. But the program "rounds off"
to a certain number of decimal places which makes the answer only
approximatly correct. Hence, two different methods of solving
the same system would typically lead to two different answers.
We want to examine the difference:
>> format long
>> x-y
ans =
1.0e-04 *
0.10266297150618
-0.02319134292161
0.19034557154185
0.13163809095218
0.14624213128589
-0.08310582596538
0.02678623820085
-0.09452334997662
>> x
x =
-2.53882468515916
1.72804300664474
0.45680915801394
-4.47181782210640
0.93371628167169
1.24544623487195
2.61349961113916
-0.40362391016632
>> y
y =
-2.53883495145631
1.72804532577904
0.45679012345679
-4.47183098591549
0.93370165745856
1.24545454545455
2.61349693251534
-0.40361445783133
>> format short
>> x-y
ans =
1.0e-04 *
0.1027
-0.0232
0.1903
0.1316
0.1462
-0.0831
0.0268
-0.0945
ITEM D) Finally, we want to determine which method gives a "better"
answer, that is an answer closer to the unique exact solution of the
linear system in question. This is done by computing the so called
ressidual vectors. If the solutions x and y were THE EXACT solutions
their residual vectors would have to vanish. As they are approximate
solutions the residual vectors measure the "error". The smaller
the residual vector the more accurate is the answer. Let's compare:
>> r=b-A*x
r =
1.0e-14 *
0.2665 (bigger)
0.1776 (smaller)
0 (smaller)
0.3553 (bigger)
-0.0444 (smaller)
-0.1776 (bigger)
-0.1776 (bigger)
0 (smaller)
>> s=b-A*y
s =
1.0e-03 *
0.2308
0.2608
0.0854
0.2704
0.1413
0.1283
0.0998
0.1140
It appears that x gives a better answer as r is smaller even though
component by component comparison shows that in four cases the x-value
is better and in 4 case the y-value is better. Perhaps you will get
a more pronounced difference.