+ All Categories
Home > Documents > An Introduction to Numerical Linear...

An Introduction to Numerical Linear...

Date post: 25-May-2018
Category:
Upload: vuongnhan
View: 247 times
Download: 1 times
Share this document with a friend
36
An Introduction to Numerical Linear Algebra P. de Groen In these course notes for the course Numerical Linear Algebra in the second year bachelor in mathematics we explain the standard algorithms for the solution of a set of linear equations and a linear least squares problem. In order to enhance the understanding of the way algo- rithms work in practice, we first give an introduction to round-off error analysis. Moreover, we give a mini-tutorial to Matlab, which is an ideal programming and computing environ- ment for experiments with numerical algorithms. The standard reference for numerical linear algebra is the book G.H. Golub & C.F. Van Loan, Matrix Computations, The Johns Hopkins University Press, Baltimore, Maryland, USA, 3 rd print, 1996. Contents 1 A mini-tutorial to MATLAB 2 2 Examples of unstable algorithms 3 2.a Recursive computation of an exponential integral............. 3 2.b How to compute the Variance ....................... 4 3 Error analysis 5 3.a Elementary definitions ........................... 5 3.b Representation of real numbers and floating-point arithmetic ..... 6 3.c The unavoidable error ........................... 7 3.d Examples of round-off error analysis ................... 7 3.e Exercises .................................. 10 4 Linear Algebra 12 4.a notations .................................. 12 4.b Exercises .................................. 14 4.c The singular value decomposition ..................... 15 4.d The Condition Number of a Matrix .................... 17 4.e Exercises .................................. 18 4.f Gaussian Elimination ............................ 19 4.g The algorithm of Crout .......................... 23 4.h Round-off error analysis .......................... 24 4.i Exercises .................................. 24 5 Linear Least Squares Problems 26 5.a The normal equations ........................... 27 5.b The method of Gram-Schmidt ....................... 28 5.c Householder Transformations ....................... 31 5.d Givens rotations .............................. 35 1
Transcript
Page 1: An Introduction to Numerical Linear Algebrahomepages.vub.ac.be/~pdegroen/numeriek/CursusNA2BaEng.pdf · 1 A MINI-TUTORIAL TO MATLAB 2 1 A mini-tutorial to MATLAB “Matlab” is an

An Introduction to Numerical Linear Algebra

P. de Groen

In these course notes for the course Numerical Linear Algebra in the second year bachelor inmathematics we explain the standard algorithms for the solution of a set of linear equationsand a linear least squares problem. In order to enhance the understanding of the way algo-rithms work in practice, we first give an introduction to round-off error analysis. Moreover,we give a mini-tutorial to Matlab, which is an ideal programming and computing environ-ment for experiments with numerical algorithms.The standard reference for numerical linear algebra is the book

G.H. Golub & C.F. Van Loan, Matrix Computations,The Johns Hopkins University Press, Baltimore, Maryland, USA, 3rd print, 1996.

Contents

1 A mini-tutorial to MATLAB 2

2 Examples of unstable algorithms 3

2.a Recursive computation of an exponential integral. . . . . . . . . . . . . 32.b How to compute the Variance . . . . . . . . . . . . . . . . . . . . . . . 4

3 Error analysis 5

3.a Elementary definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . 53.b Representation of real numbers and floating-point arithmetic . . . . . 63.c The unavoidable error . . . . . . . . . . . . . . . . . . . . . . . . . . . 73.d Examples of round-off error analysis . . . . . . . . . . . . . . . . . . . 73.e Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

4 Linear Algebra 12

4.a notations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124.b Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144.c The singular value decomposition . . . . . . . . . . . . . . . . . . . . . 154.d The Condition Number of a Matrix . . . . . . . . . . . . . . . . . . . . 174.e Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184.f Gaussian Elimination . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194.g The algorithm of Crout . . . . . . . . . . . . . . . . . . . . . . . . . . 234.h Round-off error analysis . . . . . . . . . . . . . . . . . . . . . . . . . . 244.i Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

5 Linear Least Squares Problems 26

5.a The normal equations . . . . . . . . . . . . . . . . . . . . . . . . . . . 275.b The method of Gram-Schmidt . . . . . . . . . . . . . . . . . . . . . . . 285.c Householder Transformations . . . . . . . . . . . . . . . . . . . . . . . 315.d Givens rotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

1

Page 2: An Introduction to Numerical Linear Algebrahomepages.vub.ac.be/~pdegroen/numeriek/CursusNA2BaEng.pdf · 1 A MINI-TUTORIAL TO MATLAB 2 1 A mini-tutorial to MATLAB “Matlab” is an

1 A MINI-TUTORIAL TO MATLAB 2

1 A mini-tutorial to MATLAB

“Matlab” is an interactive computing environment, designed by Cleve Moler, that was startedas a demonstration project, in which students easily could experiment with the newly developedcomputational methods for linear algebra, implemented in the packages LINPACK and EIS-PACK. The environment was so succesful, that Moler created the company Mathworks aroundit. It commercialised and extended the design into a very powerful programming and comput-ing environment for solving and simulating mathematical and physical problems and graphicalvisualisation.

The basic data structure is the matrix. The instruction “p=5; q=7; A = rand(p,q)” createsa real matrix with 5 rows en 7 columns (in IR5×7) consisting of random numbers uniformly dis-tributed on [0 , 1] . A matrix containing only one column is a column vector, a matrix containingonly one row is a row vector and a 1×1–matrix is identified with one single “real” (or complex)number. Hence, the types “real” and “vector” are not considered as separate data types. Thebasic “real” is implemented as a standard floating point IEEE 64-bits real and a complex num-ber z ∈ C (represented by ‘‘z’’) is implemented as a pair of reals with u=real(z) its real andv=imag(z) its imaginary parts. The floating point relative accuracy eps = 2×10−16 is a standardvariable in Matlab.Let the matrices A ∈ IRp×q and B ∈ IRr×s be represented by the names ‘‘A’’ and ‘‘B’’ and letµ ∈ IR be a real represented by the name ‘‘mu’’. Operations with those matrices and vectorsfollow the usual rules of linear algebra.

• Multiplication by scalars: mu*A represents the multiple µA ∈ IRp×q.

• Matrix addition: A + B represents the sum A + B ∈ IRp×q provided their dimensions areequal, p = r and q = s.

• Matrix addition and multiplication: A * B represents the product AB ∈ IRp×s provided thenumber of columns in A is equal to the number of rows in B, q = r.

• Transposition: A’ represents the transposed matrix AT ; if A is a complex matrix Hermitiantransposition (transposition plus complex conjugation) is used.

Example (>> is the matlab-prompt):

>> x=[1+i,1-i]

x =

1.0000 + 1.0000i 1.0000 - 1.0000i

>> x’

ans =

1.0000 - 1.0000i

1.0000 + 1.0000i

>> x’*x

ans =

2.0000 0 - 2.0000i

0 + 2.0000i 2.0000

>> x*x’

ans = 4

>>

Submatrices can be selected in various ways; e.g., if A ∈ Cp×q, then• real(A)∈ IRp×q is the real part, and imag(A)∈ IRp×q is the imaginary part.• A(:,k) is the k-th column (provided 1 ≤ k ≤ q) and

Page 3: An Introduction to Numerical Linear Algebrahomepages.vub.ac.be/~pdegroen/numeriek/CursusNA2BaEng.pdf · 1 A MINI-TUTORIAL TO MATLAB 2 1 A mini-tutorial to MATLAB “Matlab” is an

2 EXAMPLES OF UNSTABLE ALGORITHMS 3

• A(1:3,2:2:q) is a matrix consisting of the elements from the first three rows of Athat have even column index.

The command x = A\b solves the system of linear equations Ax = b using the best numer-ical method available: it uses Gaussian elimination with row pivoting if A is square and wellconditioned, it uses a QR decomposition or a Singuar value decomposition if A is either badlyconditioned or non-square. Obviously, the dimensions of b en A have to be compatible.The whole body of standard matrix en vector routines is available, such as FFT, QR, LU,Cholesky, SVD and eigenvalues/eigenvectors.

The Matlab-primer of Kermit Sigmon can be found on my website,http://homepages.vub.ac.be/∼pdegroen/numeriek/matlab primer.pdf .

The search on the internet for a ‘matlab tutorial’ results in a large number of links to very goodintroductions to the use of matlab, inclusive the tutorials of the “Mathworks” company.

2 Examples of unstable algorithms

2.a Recursive computation of an exponential integral.

Define the integral

En :=

∫ 1

0xnex−1dx for n = 0, 1, 2, 3, · · · .

De value of E0 is,

E0 =

∫ 1

0ex−1dx = ex−1

1

0= 1− e−1 = 0.63212055882856 .

For all positive values of n we may use the following recursion, derived by integrating by parts:

En =

∫ 1

0xnex−1dx = xnex−1

1

0− n

∫ 1

0xn−1ex−1dx = 1 − nEn−1.

Forward recursion,

E0 := 1− e−1 , En := 1− nEn−1 (n = 1, 2, · · ·),

is unstable, as we may infer from the (theoretically impossible) negative value for n = 18 in thetable below. De reason is, that an error ε in Ek−1 is ampified to an error kε in Ek. Hence, theerror in E18 is approximately 18! ≈ 1016 times the error in E0.

The backward recursion,

choose Em = arbitray , En−1 = (1− En)/n (n = m, m− 1, · · ·),

is stable. For every starting value Em it yields the correct value of En provided m is sufficientlylarge with respect to n . This is shown in column 4, where the starting value E18 = 0 is chosen tobe zero; in every (backward) iteration step the error becomes smaller and in E5 it has disappearedbelow the rounding error of the entry.

Page 4: An Introduction to Numerical Linear Algebrahomepages.vub.ac.be/~pdegroen/numeriek/CursusNA2BaEng.pdf · 1 A MINI-TUTORIAL TO MATLAB 2 1 A mini-tutorial to MATLAB “Matlab” is an

2 EXAMPLES OF UNSTABLE ALGORITHMS 4

n forward backward backward difference betweenfrom k = 0 from n = 50 from n = 18 columns 3 and 4

0 0.63212055882856 0.63212055882856 0.63212055882856 0.000000000000001 0.36787944117144 0.36787944117144 0.36787944117144 0.000000000000002 0.26424111765712 0.26424111765712 0.26424111765712 0.000000000000003 0.20727664702865 0.20727664702865 0.20727664702865 -0.000000000000004 0.17089341188538 0.17089341188538 0.17089341188538 0.000000000000005 0.14553294057308 0.14553294057308 0.14553294057308 -0.000000000000006 0.12680235656152 0.12680235656153 0.12680235656152 0.000000000000017 0.11238350406936 0.11238350406930 0.11238350406934 -0.000000000000048 0.10093196744509 0.10093196744559 0.10093196744528 0.000000000000329 0.09161229299417 0.09161229298966 0.09161229299250 -0.00000000000284

10 0.08387707005829 0.08387707010339 0.08387707007499 0.0000000000284111 0.07735222935878 0.07735222886266 0.07735222917515 -0.0000000003124812 0.07177324769464 0.07177325364803 0.07177324989825 0.0000000037497813 0.06694777996972 0.06694770257562 0.06694775132275 -0.0000000487471414 0.06273108042387 0.06273216394138 0.06273148148148 0.0000006824599015 0.05903379364190 0.05901754087930 0.05902777777778 -0.0000102368984816 0.05545930172957 0.05571934593124 0.05555555555556 0.0001637903756817 0.05719187059731 0.05277111916899 0.05555555555556 -0.0027844363865618-0.02945367075154 0.05011985495809 0 0.05011985495809

2.b How to compute the Variance

The variance of a series of measurements can be computed by two Mathematically equivalentformulae. Given n measurements {x1, x2, · · · , xn} of a physical quantity X, then its mean gand variance S2 are give by

g :=1

n

n∑

k=1

xk , S2n :=

1

n− 1

n∑

k=1

(xk − g)2 =1

n− 1

(n∑

k=1

x2k − ng2

).

The second formula is potentially numerically unstable (if S2n << g2) and much more sensitive

to small variations in the mean g, as can be seen in the following experiment.

Experiment (using Matlab, >> is the matlab prompt)

>> format short e

>> RelPerturbG=1e-12

RelPerturbG =

1.0000e-012

>> n=10000;

>> x=randn(n,1)+1e8*ones(n,1);

>> g=sum(x)/n;

>> sig2=x’*x-n*g*g;

>> sig1=(x-g*ones(size(x)))’*(x-g*ones(size(x)));

>> g=sum(x)/n*(1+RelPerturbG);

>> sig2s=x’*x-n*g*g;

>> sig1s=(x-g*ones(size(x)))’*(x-g*ones(size(x)));

>> Values=[sig1,sig2,sig1s,sig2s]

Values =

Page 5: An Introduction to Numerical Linear Algebrahomepages.vub.ac.be/~pdegroen/numeriek/CursusNA2BaEng.pdf · 1 A MINI-TUTORIAL TO MATLAB 2 1 A mini-tutorial to MATLAB “Matlab” is an

3 ERROR ANALYSIS 5

9.7946e+003 -8.1920e+004 9.7946e+003 -2.0008e+008

>> sprintf([’computed value using formula 1 : %25.15e\ n’,...

’computed value using formula 1 and relative perturbation of g : %25.15e\ n’,...

’computed value using formula 2 : %25.15e\ n’,...

’computed value using formula 2 and relative perturbation of g : %25.15e\ n’]...

,sig1,sig2,sig1s,sig2s)

ans =

computed value using formula 1 : 9.794567005712350e+003

computed value using formula 1 and relative perturbation of g : 9.794567105990183e+003

computed value using formula 2 : -8.192000000000000e+004

computed value using formula 2 and relative perturbation of g : -2.000814080000000e+008

By chance the sum of squares computed using formula 2 in this experiment is even negative!

3 Error analysis

3.a Elementary definitions

We are given a real number X and an approximation X of it. The absolute en relative errors inthe approximation X are given by:

absolute error in X : FX := X −X such that X = X + FX ,

relative error in X : fX :=X −X

Xsuch that X = X(1 + fX) (provided X 6= 0).

(3.1)The concept “absolute error” does not have any relation to “absolute values”; we use absolute asopposed to relative. The absolute error has the same dimensions (e.g. length, weight, time) asX has, while the relative error is dimensionless.

Exercise 1: Show that the absolute and relative errors in the quantities X en Y satisfy:

FX+Y = FX + FY and fX∗Y = fX + fY + fXfY .

When we know the (absolute or relative) error in a quantity X (as the result of a measurementor a computation), then we also know the quantity exactly! Unfortunately, this (almost) neverhappens; in general we do not know more than an upper bound on the absolute value of the error.In ordinary language we are used to talk about the “error” in a quantity, meaning “an upper

bound for such an error”. Thus, for a given approximation X of a quantity X we define:

∆X is (an upper bound) for the absolute error in X if | X −X | ≤ ∆X ,

δX is (an upper bound) for the relative error in X if

∣∣∣∣∣X −X

X

∣∣∣∣∣ ≤ δX .(3.2)

Exercise 2: Prove the following rules for the computation of “the errors” in the sum and theproduct of X and Y :

∆X±Y ≤ ∆X + ∆Y , ∆XY ≤ |Y |∆X + |X|∆Y + ∆X∆Y ,

δX±Y ≤|X|δX + |Y |δY

|X ± Y | , δXY ≤ δX + δY + δXδY .(3.3)

Page 6: An Introduction to Numerical Linear Algebrahomepages.vub.ac.be/~pdegroen/numeriek/CursusNA2BaEng.pdf · 1 A MINI-TUTORIAL TO MATLAB 2 1 A mini-tutorial to MATLAB “Matlab” is an

3 ERROR ANALYSIS 6

Remark. You should read these lines as: If ∆X and ∆Y are upper bounds for the errors inX and Y respectively, then there is an upper bound ∆X±Y for the error in X ± Y satisfying∆X±Y ≤ ∆X + ∆Y . This implies that ∆X + ∆Y is an upper bound for the error in X ± Y .Find the corresponding rules for the computation of (upper bounds on) the absolute and relativeerrors in the quotient X/Y .

3.b Representation of real numbers and floating-point arithmetic

Real numbers generally are stored in a computer in “floating point” format as the product of amantissa times an exponent. This implies a large dynamical range for those numbers. Given abase1 β , a real number x ∈ IR can be represented by a pair (m, e) satisfying

x = m · βe , (3.4)

where m is the mantissa is and e the exponent. Since the pair (m · β , e− 1 ) represents the samenumber, we may normalise the mantissa imposing a condition like 1/β ≤ |m | < 1. Obviously,the number of bits used in the representation must be finite. In the IEEE-standard for 64-bitsREALs a binary representation (β = 2) is chosen with 53 bits for the absolute value van mantisse,10 bits for the absolute value of the exponent and 2 sign bits. Because the first bit of a normalisedmantissa is always equal to 1 (why??), this first bit need not be stored. Because only 10 bits areused for the exponent, numbers whose exponent is larger than 210 or smaller than 2−10 cannot bestored. Hence, only numbers with absolute values between 10−300 and 10300 (approximately) canbe represented. If the result of an arithmetical operation (+ , − , × , /) is smaller or larger, it iscalled “underflow” and “overflow” respectively. Unless otherwise specified, the result of underflowis set to zero and the result of overflow to NaN (not a number). A further operation with a NaNresults in a system error.

A real number x within the range 10−300 ≤ |x | ≤ 10300 most often cannot be representedexactly (only certain rationals can). It has to be approximated by a rational number that fitswithin the representation system, called a “machine number”. Usually, the nearest machinenumber is chosen. It is denoted by fl(x). The difference x− fl(x) is the “round-off error”.

Theorem. If in a processor the base β is chosen for the representation of numbers and if amantissa carries t digits in that base, then the relative rounding error satisfies the inequality(over- and underflow omitted):

∣∣∣∣x− fl(x)

x

∣∣∣∣ ≤ η but also

∣∣∣∣x− fl(x)

fl(x)

∣∣∣∣ ≤ η met η := 12β1−t . (3.5)

The symbol η denotes the machineprecision.

Exercise 3: Prove this theorem.Prove also, that for every arithmetical operation ⊙ ∈ {+,−,×, /} involving two machine numbersx and y (except for over- and underflow) there exist real numbers ε1 and ε2 , that satisfy (exactly)

fl(x⊙ y) = (x⊙ y)(1 + ε1) =x⊙ y

1 + ε2met | ε1 | ≤ η and | ε2 | ≤ η . (3.6)

Remark. Check that η also can be defined as the largest real number such that fl(1 + η) = 1!

Exercise 4: the power series for the exponential function is: ex =

∞∑

k=0

xk

k!.

•How many terms of the series are needed to compute e−5 with a relative error smaller than 10−3?

• Is this possible using a (decimal) calculator (computer), where real numbers are stored in decimal format

1The standard value today is β = 2, but in earlier times other values have been used, such as β = 8 (CDC) andβ = 16 (IBM).

Page 7: An Introduction to Numerical Linear Algebrahomepages.vub.ac.be/~pdegroen/numeriek/CursusNA2BaEng.pdf · 1 A MINI-TUTORIAL TO MATLAB 2 1 A mini-tutorial to MATLAB “Matlab” is an

3 ERROR ANALYSIS 7

with a mantissa of 4 digits? Why?

• Is there a way to circumvent the problems due to small mantissa length in the computation of e−5 using

such a computer?

3.c The unavoidable error

Let us consider the problem to compute the value of y := f(x) of a given smooth (C2 at least) realfunction f for some value of the (real) argument x. A priori, we know that all computations haveto be executed within the rounding environment of our computer. Hence, we know in advance,that the argument has to be converted to a (binary) ‘machine number’, and that we unavoidablystart making an error in computing y = f(x + ξ) by changing the argument to the roundedvalue x + ξ with | ξ/x | ≤ η. Even disregarding all other sources of errors that can arise by animplementation of an algorithm for computing the value of f(x), this may cause an error in thecomputed value. Using a Taylor expansion we find

y = f(x + ξ) = f(x) + ξ f ′(x) + O(ξ2) such that y − y ≈ ξ f ′(x) .

We can estimate the relative error due to the rounding of the argument by

y − y

y≈ ξ

x

x f ′(x)

f(x)and approximately

∣∣∣∣y − y

y

∣∣∣∣ ≤ C η where C :=

∣∣∣∣x f ′(x)

f(x)

∣∣∣∣ . (3.7)

The error in the argument is multiplied by the factor C. Generally it is called the “condition

number” of the problem.Because we want to read the result by our human eye, we want to convert it back to decimaly = y (1 + ϑ) making another relative error |ϑ | ≤ η in the result. We conclude, that in any casea relative error bounded by (y − y)/y ≤ Cη + η may be expected not depending on the way f iscomputed. We call this the “unavoidable error”.

3.d Examples of round-off error analysis

Task: given a (real) function ϕ, compute the value x = ϕ(a).

Using an algorithm for the computation ϕ(a) we find a computed value fl(x) possibly corruptedby round-off errors.

In an error analysis we try to find (or at least, estimate) errors δx, δa or εa and εx such that

fl(x) = x + δx forward error analysis= ϕ(a + δa) backward error analysis= ϕ(a + εa) + εx mixed error analysis

Definition: An algorithm is called numerically stable if we can prove:δx or εx is of the same order of magnitude as the unavoidable error is,δa of εa is of the same order of magnitude as the machine precision is.

Example 1: For given reals a and b there is an ε satisfying | ε | ≤ η (= machine precision ) suchthat

fl(a + b) =

a + b + ε (a + b) forward

a + b with a := a(1 + ε) and b := b(1 + ε) backward

In the forward line the error ε (a + b) is considered as deviation from the result and in the backward line

the errors ε a and ε b are considered as deviations of the arguments.

Example 2: There are numbers ε1 and ε2 ( satisfying | εi | ≤ η ) such that

fl(1 − x2) = (1 − x ∗ x ∗ (1 + ε1)) ∗ (1 + ε2)

= (1 − x2) (1 + ε2) with x := x√

1 + ε1 mixed.

Page 8: An Introduction to Numerical Linear Algebrahomepages.vub.ac.be/~pdegroen/numeriek/CursusNA2BaEng.pdf · 1 A MINI-TUTORIAL TO MATLAB 2 1 A mini-tutorial to MATLAB “Matlab” is an

3 ERROR ANALYSIS 8

The round-off error is in part attributed to the argument x and in part to the result.

Example 3: Estimate the round-off error in the computed value of the positive root of thequadratic equation

a − 2x − c x2 with a ≥ 0 and c ≥ 0

using the formula

x :=−1 +

√1 + a c

c

and assuming that the round-off error in the computed value of a square root satisfies the estimate

fl(√

x) =√

x (1 + εx) with | εx | ≤ η for all x .

Answer: There exist numbers ε1 , ε2 and ε3 with | εi | ≤ η , such that

fl(√

1 + a c) =√

(1 + a c (1 + ε1)) (1 + ε2) (1 + ε3)

=√

1 + a c (1 + ξ1) with ξ1 :=√

1 + ε2 (1 + ε3)− 1 and a := a (1 + ε1)

As a consequence, there are numbers ξ2 en ξ3, ( | ξi | ≤ η ) such that:

fl(x) =−1 +

√1 + a c (1 + ξ1)

c(1 + ξ2) (1 + ξ3)

=−1 +

√1 + a c

c(1 + ξ2) (1 + ξ3) +

√1 + a c

cξ1 (1 + ξ2) (1 + ξ3)

The second term may be large in comparison to x if | ac | ≪ 1 and in that case the formula isnot numerically stable and should be avoided.

An alternative (numerically stable) algorithm for this root is:

x :=a

1 +√

1 + a c.

Example 4: Round-off error in the computed value of an inner product

S :=n∑

i=1

xi yi to be computed by the algorithm:

[S := 0;for i := 1 to n do S := S + xi ∗ yi

For the computed value of S numbers ξi and ηi exist with | ξi | , | εi | ≤ η, i = 1 · · · n such that:

fl(S) = x1 y1 (1 + ξ1) (1 + ε2) · · · (1 + εn)

+ x2 y2 (1 + ξ2) (1 + ε2) · · · (1 + εn)

+ · · ·+ xn−2 yn−2 (1 + ξn−2) (1 + εn−2) · · · (1 + εn)

+ xn−1 yn−1 (1 + ξn−1) (1 + εn−1) (1 + εn)

+ xn yn (1 + ξn) (1 + εn) .

Hence,

S − fl(S) =n∑

i=1

xi yi ζi

where

ζi := 1 − (1 + ξi) (1 + εi) · · · (1 + εn) and | ζi | ≤ (n − i + 2) η provided nη ≤ 0.1 .

Page 9: An Introduction to Numerical Linear Algebrahomepages.vub.ac.be/~pdegroen/numeriek/CursusNA2BaEng.pdf · 1 A MINI-TUTORIAL TO MATLAB 2 1 A mini-tutorial to MATLAB “Matlab” is an

3 ERROR ANALYSIS 9

As a consequence, the forward error satisfies:

| S − fl(S)

S| ≤ (n + 1) η

|S |n∑

i=1

|xi yi | ≤ (n + 1) η‖x ‖2 ‖y ‖2|xT y | (3.8)

Example 5: Compute xn from the equation

a =n∑

i=1

xi yi , a, x1 · · · xn−1 , y1 · · · yn given,

and estimate the round-off error in the computed value of xn.

algorithm:

S := a;for i := 1 to n − 1 DO S := S − xi ∗ yi ;xn := S / yn

For the computed value of S and xn numbers ξi en ηi exist, satisfying | ξi |, | εi | ≤ η , such that:

fl(S) = a (1 + ε1) · · · (1 + εn−1)

− x1 y1 (1 + ξ1) (1 + ε1) · · · (1 + εn−1)

− x2 y2 (1 + ξ2) (1 + ε2) · · · (1 + εn−1)

− · · ·− xn−2 yn−2 (1 + ξn−2) (1 + εn−2) (1 + εn−1)

− xn−1 yn−1 (1 + ξn−1) (1 + εn−1)

andxn := fl(xn) = fl(S) / ( yn (1 + ξn) )

Division by (1 + ε1) · · · (1 + εn−1) yields the backward error estimate:

a = x1 y1 (1 + ξ1) + x2 y21 + ξ2

1 + ε1+ · · ·

+ xn−1 yn−11 + ξn−1

(1 + ε1) · · · (1 + εn−2)

+ xn yn1 + ξn

(1 + ε1) · · · (1 + εn−1)

=n−1∑

i=1

xi yi (1 + δi) + xn yn (1 + δn)

where

δi :=1 + ξi

(1 + ε1) · · · (1 + εi−1)− 1 , satisfying | δi | ≤ (i + 1) η if n η < 0.1 .

Conclusion: The computed value xn is the solution of the neighbouring equation

a =n∑

j=1

xj yj , yj := yj (1 + δj) . (3.9)

Example 6, an error estimate for En. In section 2a we considered the recursion:

En = 1− n En−1 . (3.10)

Let En := fl(En) be the computed value of En, then there exist numbers ξn en ζn satisfying:

En = fl(1− fl(n En−1)) = (1− n En−1(1 + ξn))/(1 + ζn) , | ξn | ≤ η en | ζn | ≤ η , (3.11)

Page 10: An Introduction to Numerical Linear Algebrahomepages.vub.ac.be/~pdegroen/numeriek/CursusNA2BaEng.pdf · 1 A MINI-TUTORIAL TO MATLAB 2 1 A mini-tutorial to MATLAB “Matlab” is an

3 ERROR ANALYSIS 10

written in a different way,

En + ζnEn = 1− n En−1 − n ξn En−1 . (3.12)

Subtracting (3.10) we find a recursion for the errors:

En − En = −n(En−1 − En−1)− ζnEn − n ξn En−1 . (3.13)

Defining Fn := En − En and δn := −ζnEn − n ξn En−1 we find the recursion

Fn = −n Fn−1 + δn , F0 = fl(E0)−E0 , |F0 | ≤ η E0 ≤ η. (3.14)

Since En > 0 for all n , equation (3.10) implies, that En−1 ≤ 1/n; this should be true also for En−1

as long as it is a reasonable approximation of En is. Under this condition we have | δn | ≤ 2ηand Fn satisfies in that case the inequality

|Fn | ≤ n |Fn−1 | + 2η . (3.15)

Hence, there is a majorizing sequence {Fn} such that

|Fn | ≤ Fn with Fn = n Fn−1 + 2η , F0 = η . (3.16)

The recursion for Fn gives an a priori upper bound for the error in the computed value of En:

| En − En | = Fn ≤ Fn = n! η

(1 +

2

1!+

2

2!+ · · ·+ 2

n!

)≤ n! η (2 e1 − 1) . (3.17)

If n = 18, this upper bound is already much larger than 1, such that it is likely that the condition| En | ≤ 1 is no longer satisfied.

A better upper bound can be obtained by computing together with En a (tight) upper boundfor the round-off error in it. Since (3.13) implies

|Fn | ≤ n |Fn−1 | + η | En | + n η | En−1 | , (3.18)

we can compute a running error estimate Fn in the recursion together with En

Fn = n Fn + η | En | + n η | En−1 | . (3.19)

This provides for each n a relatively good a posteriori upper bound for the absolute error in thecomputed value of En in algorithm (3.10). We remark that this (smaller) upper bound can beobtained only after the actual computations, because it takes into account the actual round-offerrors.

3.e Exercises

1. Rewrite the following expressions in a numerically stable form

1

1 + 2x− 1− x

1 + xvoor |x | ≪ 1 (3.20)

√x +

1

x−√

x− 1

xvoor |x | ≫ 1 (3.21)

1− cos x

xvoor |x | ≪ 1 (3.22)

Page 11: An Introduction to Numerical Linear Algebrahomepages.vub.ac.be/~pdegroen/numeriek/CursusNA2BaEng.pdf · 1 A MINI-TUTORIAL TO MATLAB 2 1 A mini-tutorial to MATLAB “Matlab” is an

3 ERROR ANALYSIS 11

2. If a routine is available for computing the inverse sine function x 7→ arcsin(x) in a numericallystable way, we can evaluate the arctan (inverse tangent) function using the relation

arctan x = arcsinx√

1 + x2. (3.23)

Estimate the relative error in the result, assuming that the sqrt and arcsin functions returnan approximation with good relative accuracy. For what values of x this method is reliable?

3. Let f be a sufficiently smooth function (e.g. f(x) = sin(x)) satisfying

maxx|f ′′′(x)| ≤M .

The derivative of f in x can be approximated by the central difference

Dhf(x) :=f(x + h)− f(x− h))

2h.

a. Show, that the cut-off error in Dhf satiesfies:

f(x + h)− f(x− h)

2h= f ′(x) +

h2

6f ′′′(x + ϑh) for some |ϑ| ≤ 1 . (3.24)

b. Assume that a routine for the computation of f is available, that returns for every x aresult with a relative error smaller than 2η . Find a (good) upper bound for the relativeerror in the computed value of Dhf as a function of h and sketch the graph of the totalerror (cut-off plus round-off errors) in the computed approximation of the derivative f ′(x)as a function of h (i.e. sketch a graph for an upper bound of | {f ′(x)−fl(Dhf(x))}/f ′(x) |as a function of h).

4. We may represent a polynomial P of degree n by a sum or a product,

P (x) :=n∑

k=0

ak xn−k or P (x) := a0

n∏

k=1

(x− xk) (with a0 6= 0 ),

with coefficients a0 , a1 , · · · , an or (complex) zeros x1 , x2 , · · · , xn respectively and withnon-zero leading coefficient a0 6= 0 . In the first case, the best way to compute the value ofthe polynomial for a given argument ξ is the algorithm of Horner:

b0 := a0 ; for k := 1 to n do bk := bk−1 ∗ ξ + ak end , (3.25)

resulting in P (ξ) = bn . The value D of the derivative P ′(ξ) can be computed in the sameloop,

D := 0 ; P := a0 ; for k := 1 to n do D := D ∗ ξ + P ; P := P ∗ ξ + ak end .

a. Prove correctness of algorithm (3.25).

b. Show that the coefficients b0 , · · · , bn−1 computed in (3.25) satisfy

P (x) := bn + (x− ξ)n−1∑

k=0

bk xn−k (3.26)

such that bn = 0 implies that ξ is a zero of the polynomial and vice versa. This impliesthat Horner’s scheme computes the coefficients of the deflated polynomial P (x)/(x − ξ)of degree n−1 if ξ is a zero of P (synthetic division).

Page 12: An Introduction to Numerical Linear Algebrahomepages.vub.ac.be/~pdegroen/numeriek/CursusNA2BaEng.pdf · 1 A MINI-TUTORIAL TO MATLAB 2 1 A mini-tutorial to MATLAB “Matlab” is an

4 LINEAR ALGEBRA 12

c. Show that numbers δk exist, such that the value fl(P (x)) computed by Horner’s schemeis equal to the exact value of a neighbouring polynomial,

fl(P (x)) =n∑

k=0

ak xn−k met ak := ak (1 + δk) and | δk | ≤ (2n− 2k + 1)η + O(η2) .

d. Show that the following algorithm,

P := a0 ; d := 0; for k := 1 to n do d := d+ |P | ; P := P ∗x+ak ; d := d∗ |x | + |P | end

computes together with the value of the polynomial a “running error estimate” d thatsatisfies after termination:

|fl(P (x))− P (x) | ≤ d η .

5. The standard deviation S of a set of measurements {x1 · · · xn} can be computed in twomathematically equivalent ways:

S2 =1

n− 1(

n∑

i=1

x2i − n g2 ) and S2 =

1

n− 1

n∑

i=1

(xi − g)2

where g is the mean:

g :=1

n

n∑

i=1

xi .

Which of both formulae should be preferred in numerical computations and why?Apply to the computations of g and S2 an error analysis analogous to those in the previoussection.

4 Linear Algebra

4.a notations

The theory in linear algebra and their proofs can be be formulated quite elegantly in terms ofan abstract vector space E of dimension n over the field of real or complex numbers. However,for actual computations we always have to choose a basis and we have to represent vectors andmatrices as a set of numbers with respect to this basis. So, we will work always with the vectorspaces IRn or Cn in which a vector is a column of n numbers and where a (linear) transformationis a matrix, an array of m× n numbers in IRm×n or Cm×n.

• A vector x ∈ IRn is a column of n real (or complex) numbers,

x =

x1

x2...

xn

with components x1 , · · · , xn . (4.1)

In print we use the boldface type x and in manuscript we use underlining x ; we denote itscomponents by the italic type of the same letter plus a subscript xk .

• For a matrix A ∈ IRm×n we always use a (italic) capital letter. The matrix elements aij aredenoted by the corresponding minuscule with two indices. The columns of a matrix are vectors inIRm, denoted by boldface minuscules with one index; their span is the image (sub)space Im(A):

A =

a11 · · · a1n...

. . ....

am1 · · · amn

= (a1 | · · · |an) , such that ak =

a1k...

amk

. (4.2)

Page 13: An Introduction to Numerical Linear Algebrahomepages.vub.ac.be/~pdegroen/numeriek/CursusNA2BaEng.pdf · 1 A MINI-TUTORIAL TO MATLAB 2 1 A mini-tutorial to MATLAB “Matlab” is an

4 LINEAR ALGEBRA 13

The corresponding notation in matlab is: if A is a matrix, then the vector A(:,k) is its k–thcolumn.As is usual in Matlab, a vector is identified with a matrix consisting of one column.

• A matrix A ∈ IRm×n and a vector x ∈ IRn can be partitioned as follows:

A =

(A11 A12

A21 A22

)and x =

(x1

x2

)such that Ax =

(A11x1 + A12x2

A21x1 + A22x2

)(4.3)

provided the dimensions match:

A11 ∈ IRp×r , A12 ∈ IRq×r , A21 ∈ IRp×s , A22 ∈ IRq×s

u ∈ IRp , v ∈ IRq , p + q = n and r + s = mThe corresponding notation in matlab works as follows: if A is a matrix, then the part A22 isselected by the statement B=A(p+1:n, r+1:m) . Remember that the indices in B are shifted, suchthat B(1,1)=A(p+1,r+1), etc.

• The transposed of a matrix A is denoted by AT ; for complex matrices we have ordinary trans-position (denoted by AT ) and complex or Hermitian transposition (denoted by AH). In the latterall elements are transposed and complex conjugated. In matlab the accent A’ means Hermitiantransposition.

• The norm of a vector x ∈ IRn is denoted by ‖x ‖ . As is well known, all norms in a vector spaceof finite dimension are equivalent (why?). In this course we shall use only three vector norms, theEuclidian norm ‖ · ‖2 (or ℓ2-norm) , the max-norm ‖ · ‖∞ (or ℓ∞-norm) and the 1–norm ‖ · ‖1(ℓ1–norm or dual of the max-norm):

‖x ‖1 :=n∑

j=1

|xj | , ‖x ‖22 :=n∑

j=1

|xj | 2 and ‖x ‖∞ := maxj|xj | (4.4)

The Euclidian norm is derived from an inner product:

If u , v ∈ Cn , then 〈u , v〉 := uHv =n∑

j=1

uj vj . (4.5)

Since uH is a row-vector and is identified with an 1×n–matrix, we may identify the inner product(4.5) with the matrix–matrix product uHv (or uTv for real vectors) and with u’*v in matlab.The vectors u , v are called orthogonal if uT v = 0 .

• For a matrix A ∈ IRm×n the matrixnorm A 7→ ‖A ‖ , associated (subordinate) to the vectornorm x 7→ ‖x ‖ , is defined by

‖A ‖ := maxx ∈ IR

n

, ‖x‖ 6= 0

‖Ax ‖‖x‖ = max

x ∈ IRn

, ‖x‖ = 1‖Ax ‖ (4.6)

In the numerator we see a vectornorm in IRm and in the denominator a vectornorm in IRn .A matrix norm defined in this way is often called a lub–norm (lub is derived from ‘least upperbound’). Check, that a lub–norm not only satisfies all requirements for a norm, but also satisfiesthe product (or algebra) property ‖AB ‖ ≤ ‖A ‖ ‖B ‖ .A lub–norm generally is denoted by the same symbol ‖ · ‖1 , ‖ · ‖2 or ‖ · ‖∞ as is the vectornorm to which it is subordinate.

• The Frobenius–norm of a matrix A ∈ IRm×n is defined by

‖A ‖2F :=m∑

i=1

n∑

j=1

| aij | 2 . (4.7)

Page 14: An Introduction to Numerical Linear Algebrahomepages.vub.ac.be/~pdegroen/numeriek/CursusNA2BaEng.pdf · 1 A MINI-TUTORIAL TO MATLAB 2 1 A mini-tutorial to MATLAB “Matlab” is an

4 LINEAR ALGEBRA 14

This norm satisfies the product property ‖AB ‖F ≤ ‖A ‖F ‖B ‖F but it is not a lub–norm. Infact it is the Euclidian norm of the matrix considered as an element of an m×n–dimensionalvector space.

• In Matlab those vector and matrix norms of an object a are computed by the functionnorm(a,p), where p stands for one of the symbols “1”, “2”, “inf” or “’fro’” (in the lastone the quotes are mandatory!).

• A square (real) matrix A ∈ IRn×n is orthogonal if AT A = I , the identity in IRn; a (complex)matrix A ∈ Cn×n is unitary if AHA = I. Check that these definitions imply: AAT = I andAAH = I respectively.If A ∈ IRm×n with m > n and AT A = I , then the columns of A are orthonormal and A is calleda partial isometry.

• A diagonal matrix D ∈ IRm×n is a matrix whose elements outside the main diagonal are zero,i.e. D = (dij) with dij = 0 if i 6= j .For a vector a ∈ IRn we define the diagonal matrix D := diag(a) ∈ IRm×n with m ≥ n by dii = ai

and dij = 0 if i 6= j ; we assume m = n, unless it is clear from the context that m should belarger.The matlab function diag constructs from a vector a square matrix with the elements of thisvector on the main diagonal. The application of this function to an m×n matrix (m > 1 andn > 1) extracts the main diagonal and delivers it as a vector of length min(m,n).

4.b Exercises

1. Prove the following identities for the subordinate matrix norms:

‖A‖1 = maxj

n∑

i=1

| aij | , ‖A‖∞ = maxi

n∑

j=1

| aij | and ‖A‖2 = maxx 6=0,y 6=0

| (Ax,y) |‖x‖2‖y‖2

,

where (x,y) :=∑n

i=1 xiyi .

2. Prove the following inequalities for any x ∈ IRn and any A ∈ IRn×n:

1) ‖x‖2 ≤ ‖x‖1 ≤√

n‖x‖2 2) ‖x‖∞ ≤ ‖x‖2 ≤√

n‖x‖∞

3)1√n‖A‖2 ≤ ‖A‖1 ≤

√n‖A‖2 4)

1√n‖A‖∞ ≤ ‖A‖2 ≤

√n‖A‖∞

Show, that the inequalies are “sharp”, i.e. find for any of the above inequalities a vector ormatrix for which equality holds.

3. Show, that the 2-norm of a matrix is unitarily invariant (i.e. ‖U A‖2 = ‖A‖2 for any unitarytransformation U).

4. Show that the “Frobenius” norm cannot be subordinate to a vector norm. Show also thatit satisfies the product property (‖B A‖F ≤ ‖A‖F ‖B‖F ) and that it is unitarily invariant(‖U A‖F = ‖A‖F for every unitary transformation U).

5. Prove:‖A ‖2F = trace (AT A ) = sum of all eigenvalues of AT A

‖A ‖22 = largest eigenvalue of AT A

1√n‖A ‖F ≤ ‖A ‖2 ≤ ‖A ‖F

Remark. The square roots of the eigenvalues of AT A are the “singular values” of A.

6. For given vector a ∈ IRn the mapping fa := x 7→ aT x is a linear transformation from IRn toIR. Show that ‖ fa ‖1 = ‖a ‖∞ , ‖ fa ‖∞ = ‖a ‖1 and ‖ fa ‖2 = ‖a ‖2 .

Page 15: An Introduction to Numerical Linear Algebrahomepages.vub.ac.be/~pdegroen/numeriek/CursusNA2BaEng.pdf · 1 A MINI-TUTORIAL TO MATLAB 2 1 A mini-tutorial to MATLAB “Matlab” is an

4 LINEAR ALGEBRA 15

4.c The singular value decomposition

Theorem: For any (real) matrix A ∈ IRm×n there exist orthogonal matrices U ∈ IRm×m andV ∈ IRn×n and p := min{m,n} non-negative numbers σ1 , · · · , σp such that

A = U Σ V T , Σ := diag(σ1 , · · · , σp) ∈ IRm×n. (4.8)

Notes. – The numbers σ1 , · · · , σp are called the singular values of A.– It is common practice to order the singular values in decreasing sense σk ≥ σk+1 .– In Matlab the singular value decomposition is computed by the function svd:

s=svd(A) returns the singular values in the vector s.[U,S,V]=svd(A) returns in U, S and V the three matrices of the decomposition (4.8).

Proof. We give two proofs, one using the eigenvalue decomposition of AT A, the other is moreelementary . For simplicity we choose m ≥ n. The norms in this proof are the Euclidean vectornorm and its subordinate matrix norm.

1. The matrix AT A ∈ IRn×n is symmetric and non-negative definite. Hence, it has n non-negativeeigenvalues; we order them in decreasing sense λ1 ≥ λ2 ≥ · · · ≥ λn ≥ 0 . Associated to theseeigenvalues is an orthonormal basis of eigenvectors v1 , · · · , vn such that AT Avk = λkvk . Define

Σ := diag(√

λ1 , · · · ,√

λn) , V := (v1 | · · · |vn) and U :=

(Av1√

λ1

∣∣∣∣ · · ·∣∣∣∣Avn√

λn

),

then V is an orthogonal matrix and the matrix U has orthonormal columns. We supplement thismatrix with m−n columns to an orthogonal matrix (how?). The result satisfies eq. (4.8).

2. An elegant elementary proof by induction runs as follows. Define σ1 := ‖A‖ . The functionx 7→ ‖Ax‖ is continuous and has a maximum on the unit ball {‖x ‖ = 1}. Hence, there is avector v1 with norm ‖v1‖ = 1 such that ‖Av1‖ = ‖A‖ = σ1 . Define u1 := Av1/σ1 and constructorthogonal matrices U := (u1 | U) and V := (v1 | V ) containing these vectors as their first columnsby supplementing the sets {u1} en {v1} to orthonormal bases in IRm and IRn respectively (e.g.using the Gram-Schmidt process). So we find:

V := (v1 | V ) such that AV = A (v1 | V ) = (Av1 |AV ) = (σ1u1 |AV )

and

A := UT AV =

(uT

1

U

)(σ1u1 |AV ) =

(σ1u

T1 u1 uT

1 AV

σ1UTu1 UT AV

)=

(σ1 wT

0 A

). (4.9)

Here 0 is the zero vector; its components are zero because the columns of U are orthogonal to u1

by definition.The (row) vector wT := uT

1 AV is the first row of A but for the first element; we shall provethat this vector is zero too. The remaining part of the matrix A := UT AV ∈ IR(m−1)×(n−1) is ofsmaller dimension.

In order to prove that w is the null vector, we estimate the following norm in two ways. First we

estimate the norm of the image of the vector (σ1 , wT )T

from below by its first component,

∥∥∥∥∥A(

σ1

w

)∥∥∥∥∥

2

=

∥∥∥∥∥

(σ2

1 + wTw

Aw

)∥∥∥∥∥

2

≥ (σ21 + wTw)2;

however, since the matrix norm is invariant under orthogonal transformations, we also have theestimate from above

∥∥∥∥∥A(

σ1

w

)∥∥∥∥∥

2

≤ σ21

∥∥∥∥∥

(σ1

w

)∥∥∥∥∥

2

= σ21 (σ2

1 + wTw) .

Page 16: An Introduction to Numerical Linear Algebrahomepages.vub.ac.be/~pdegroen/numeriek/CursusNA2BaEng.pdf · 1 A MINI-TUTORIAL TO MATLAB 2 1 A mini-tutorial to MATLAB “Matlab” is an

4 LINEAR ALGEBRA 16

As a consequence we have σ21 + wTw ≤ σ2

1 . This squeezes the vector w ∈ IRn−1 to zero length.So we find

UT AV =

(σ1 0T

0 A

). (4.10)

We can now apply the same argument to the smaller matrix A and go on until it has size 1.

The singular value decomposition, abbreviated SVD, looks as a very simple tool to computethe rank of a matrix. In practice round-off errors disturb the picture. Any reliable algorithmto compute the SVD will at best compute the singular value decomposition of a neighbouringmatrix. Since the invertible matrices form a dense subset in the set of all n×n matrices (and thesubset of all n×n matrices of rank k < n is a dense subset in subset of all n×n matrices of rankk+1) a neighbouring matrix almost surely is of full rank. Hence it is impossible to find the truerank of a rank-deficient matrix (by numerical means). Therefore the numerical rank rank(A, ε)is defined as the minimal rank of all matrices in a ball around A with radius ε:

rank(A, ε) := min {rank(A + E) |E ∈ IRm×n, ‖E‖ ≤ ε} (4.11)

In this setting most often the 2-norm is used, because this norm is unitarily invarant and the2-norm of a matrix is equal to the largest singular value. Let A ∈ IRm×n have the singular valuesσ1 , · · · , σp with p = min{m,n}. If these satisfy

σ1 ≥ · · · ≥ σr > ε ≥ σr+1 ≥ · · · ≥ σp , then rank(A, ε) := r ; (4.12)

if A = UΣ V T , then the matrix E := U diag(0 , · · · , 0 , σr+1 , · · · , σp)V T satisfies the condition‖E‖2 ≤ ε and rank(A− E) = r .

The study of reliable algorithms for computing the SVD is outside the scope of this course. Thefunction svd(A) in Matlab computes factors U , Σ and V that satisfy ‖A−U Σ V T ‖2 ≤ η ‖A ‖2,i.e. factors that form the exact SVD of a neighbouring matrix.

Example of the use of SVD for data reduction. The magic square from the woodcut“Melencolia” of Durer is scanned to a matrix of 359×371 gray values. The SVD of this matrix iscomputed and the singular values are plotted in the left plot of fig.2. The right picture shows thematrix that results if all but the largest singular value is set to zero and the middle if all but the 36largest are set to zero. The grid is the most dominant feature in the picture. The reconstructionusing only the 36 dominant singular values provides already a very good approximation. This

Duerer, Melancholia 359 x 371 pixels

Figure 1: Durer’s woodcut “melencolia” (left) and the detail “the magicsquare” in the upper right corner (right)

analysis technique is not very common for pictures. In statistics this type of data reduction isvery popular under the name “principal component analysis”. The grid in the right picture offig. 2 is the first principal component of the picture of the magic square; the left picture of thesingular values is the “scree plot”.

Page 17: An Introduction to Numerical Linear Algebrahomepages.vub.ac.be/~pdegroen/numeriek/CursusNA2BaEng.pdf · 1 A MINI-TUTORIAL TO MATLAB 2 1 A mini-tutorial to MATLAB “Matlab” is an

4 LINEAR ALGEBRA 17

0 50 100 150 200 250 300 35010

0

101

102

103

104

singular values, logarithmic plot reduction to q = 36 singular values reduction to q = 1 singular values

Figure 2: Logarithmic plot of the 359 singular values of the matrix of grayvalues of the pixels of the detail (left) and reconstructions of thedetail using 36 (middle) and only 1 singular value (right). Appar-ently, the grid is the most dominant feature in the picture.

4.d The Condition Number of a Matrix

Given an invertible matrix A ∈ IRn×n and a vector b ∈ IRn we want to solve the set of n linearequations

Ax = b . (4.13)

Before we study algorithms, it is advantageous to study the sensitivity of the problem with respectto small perturbations of A and b (as we did in section 3.c for a univariate function). Let E andd be (small) perturbations of A and b. We consider the “perturbed” problem

(A + E) (x + w) = b + d , where E ∈ IRn×n and d ∈ IRn small. (4.14)

We try to estimate the resulting deviation w of from the solution x of (4.13). The perturbedequation can be solved (uniquely) only if A + E is invertible:

Lemma. If A ∈ IRn×n is invertible and if E ∈ IRn×n is so small that ‖A−1E‖ < 1 , then A + Einvertible and satisfies the estimate

‖(A + E)−1‖ ≤ ‖A−1‖1− ‖A−1E‖ (4.15)

Proof. Let I be the identity in IRn and let F ∈ IRn×n satisfy ‖F‖ < 1 , then all x ∈ IRn satisfythe inequality

‖Ix + Fx‖ ≥ ‖x‖ − ‖Fx‖ ≥ (1− ‖F‖)‖x‖ > 0 .

Hence, no non-zero vector is mapped to the zero vector by I +F and so it is invertible. Replacingx in this formula by (I + F )−1y we find

‖y‖ = ‖(I + F )(I + F )−1y‖ ≥ (1− ‖F‖)‖(I + F )−1y‖ for all y ∈ IRn .

Taking the maximum over all y in the unit ball we find

‖(I + F )−1‖ ≤ 1

1− ‖F‖ .

Since A + E = A(I + A−1E) this implies the inequality (4.15).

We now return to problem (4.14), to find an upper bound for ‖w‖. We subtract (4.13) from(4.14) and find

(A + E)w = d − Ex .

Page 18: An Introduction to Numerical Linear Algebrahomepages.vub.ac.be/~pdegroen/numeriek/CursusNA2BaEng.pdf · 1 A MINI-TUTORIAL TO MATLAB 2 1 A mini-tutorial to MATLAB “Matlab” is an

4 LINEAR ALGEBRA 18

Using the lemma we estimate:

‖w‖ ≤ ‖(A + E)−1‖ (‖d‖ + ‖E‖ ‖x‖) ≤ ‖A−1‖(‖d‖ + ‖E‖ ‖x‖)

1− ‖A−1E‖ . (4.16)

Dividing by ‖x‖ and using the inequality ‖A‖ ‖x‖ ≥ ‖Ax‖ = ‖b‖ we find an estimate for therelative perturbation:

‖w‖‖x‖ ≤

‖A−1‖ ‖A‖1− ‖A−1E‖

(‖d‖‖b‖ +

‖E‖‖A‖

). (4.17)

We see in this formula that the relative magnitudes of the perturbations of A and b are multipliedby the factor κ(A) := ‖A−1‖ ‖A‖ (provided that the term ‖A−1E‖ in the numerator is negligible).This factor κ is called the Condition number of the matrix A. This condition number dependson the matrix and vector norms used in the analysis. Most often we use the condition numbers

κ1 := ‖A−1‖1 ‖A‖1 κ2 := ‖A−1‖2 ‖A‖2 and κ∞ := ‖A−1‖∞ ‖A‖∞ (4.18)

with respect to the usual lub matrix norms. The computation of these condition numbers requiresthe computation of the inverse (for κ1 and κ∞) or the SVD (for κ2). Condition number estimatorsexist, that require much less computational effort.

4.e Exercises

1. Compute the singular value decomposition of the n×1–matrix A :=

a1...

an

.

2. Compute the singular value decomposition of the n×2–matrix A := (u |v ) , where the vectorsu ∈ IRn en v ∈ IRn are perpendicular (uTv = 0).

3. For the following matrices B−1 compute the inverse and the condition number κ∞(B) :=‖B‖∞ ‖B−1‖∞a.

B :=

1 −1 · · · −10 1 · · · −1...

.... . .

...0 0 · · · 1

∈ IRn×n where Bij =

1 if j = i ,−1 if j > i ,0 if j < i .

(4.19)

b.

B :=

1 1 · · · 10 1 · · · 1...

.... . .

...0 0 · · · 1

∈ IRn×n where Bij =

{1 if j ≥ i ,0 if j < i .

(4.20)

c.

B :=

1 2 · · · n0 1 · · · n−1...

.... . .

...0 0 · · · 1

∈ IRn×n where Bij =

{j−i+1 if j ≥ i ,

0 if j < i .(4.21)

Hint: Reduce B by Gauss-Jordan algorithm to the form (4.20).

d.

B :=

1 −1 · · · (−1)n−1

0 1 · · · (−1)n−2

......

. . ....

0 0 · · · 1

∈ IRn×n where Bij =

{(−1)j−i if j ≥ i ,

0 if j < i .(4.22)

Page 19: An Introduction to Numerical Linear Algebrahomepages.vub.ac.be/~pdegroen/numeriek/CursusNA2BaEng.pdf · 1 A MINI-TUTORIAL TO MATLAB 2 1 A mini-tutorial to MATLAB “Matlab” is an

4 LINEAR ALGEBRA 19

4.f Gaussian Elimination

A triangular sytem of equations Lx = b or Uy = c where L is a lower (left) triangular matrixwith Lij = 0 if j > i and where U is an upper (right) triangular matrix with Uij = 0 if j < i ,

L =

L11 0 · · · 0...

. . .. . .

...L1,n−1 · · · Ln−1,n−1 0Ln1 · · · · Lnn

and U =

U11 · · · · · · U1n

0 U22 · · · U2n...

. . .. . .

...0 · · · 0 Unn

(4.23)

can be solved easily top down or bottom up. In Matlab this is coded in the following way:

x(1)=b(1)/L(1,1);

for k=2:n,

x(k)=(b(k)-L(k,1:k-1)*x(1:k-1))/L(k,k);

end

(4.24)

y(n)=c(n)/U(n,n);

for k=n-1:-1:1,

y(k)=(c(k)-U(k,k+1:n)*y(k+1:n))/U(k,k);

end

(4.25)

Exercise: Check that the following (columnwise) algorithm computes the same result as (4.24) does:

for k=1:n-1,

x(k)=b(k)/L(k,k);

b(k+1:n)=b(k:1:n)-L(k+1:n,k)*x(k);

end,

x(n)=b(n)/L(n,n).

(4.26)

Write down the analogous columnwise algorithm for the solution of y in eq. (4.25). Determine the number

of flops used for the computations of x and y in (4.24), (4.25) and (4.26).

Gauss elimination is an algorithm that reduces a general linear system of equations

Ax = b or

A11 · · · A1n...

. . ....

An1 · · · Ann

x1...

xn

=

b1...bn

(4.27)

to upper triangular form Ux = c. The basic idea is that any linear combination of equations ofsystem (4.27) is again an equation that is satisfied by the solution. If A11 6= 0 , we can subtractfrom the second up to the n–th equation a multiple of the first equation in such a way that thecoefficient of the first unknown x1 in these equations vanishes. This is accomplished by replacingrow(k) in the matrix by row(k)−Ak1/A11 × row(1).

If A22 6= 0 in the resulting matrix, we can eliminate similarly the dependence on x2 from thethird up to the n–th equations, etc. After n− 1 steps an (equivalent) triangular set of equationsremains, that can be solved easily by algorithm (4.25). We can write down the algorithm a littlemore formally as:

for k = 1 : n− 1If Akk 6= 0 ,

for j = k + 1 : nreplace row(j) of A by row(j)−Ajk/Akk × row(k)

and replace the j-th element bj of the r.h.s. by bj −Ajk/Akk × bk

end

end(4.28)

Page 20: An Introduction to Numerical Linear Algebrahomepages.vub.ac.be/~pdegroen/numeriek/CursusNA2BaEng.pdf · 1 A MINI-TUTORIAL TO MATLAB 2 1 A mini-tutorial to MATLAB “Matlab” is an

4 LINEAR ALGEBRA 20

In matlab this is coded compactly as

for k=1:n-1,

for j=k+1:n,

A(j,k+1:n) = A(j,k+1:n) - A(j,k) / A(k,k) * A(k,k+1:n);

b(j) = b(j) - A(j,k) / A(k,k) * b(k);

end

end

(4.29)

After termination all matrix elements A(i,j) with i>j should be zero. However, we did nottake the trouble (and the additional work) to set all those elements to zero explicitly, becausethey are not used any more in the final solution step (4.25). Moreover, since the lower triangularpart of the matrix has become irrelevant, we can use the memory space to store the multipliersA(j,k) / A(k,k):

for k=1:n-1,

A(k+1:n,k) = A(k+1:n,k)/A(k,k);

A(k+1:n,k+1:n) = A(k+1:n,k+1:n) - A(k+1:n,k)*A(k,k+1:n);

b(k+1:n) = b(k+1:n) - A(k+1:n,k)*b(k);

end

(4.30)

The benefit of this is that we can split the algorithm in a part that works on the matrix aloneand a part that works on the right-hand side b:

for k=1:n-1,

A(k+1:n,k) = A(k+1:n,k)/A(k,k);

A(k+1:n,k+1:n) = A(k+1:n,k+1:n) - A(k+1:n,k)*A(k,k+1:n);

end

for k=1:n-1,

b(k+1:n) = b(k+1:n) - A(k+1:n,k)*b(k);

end

(4.31)

After termination of algorithm (4.31) we can assign the elements of A partially to the lowertriangular matrix L and partially to the upper triangular U :

Lij =

1 if i = jA(i, j) if i > j

0 if i < jand Uij =

{A(i, j) if i ≤ j

0 if i > j.

The matrices L and U constructed in this way satisfy the property Aoriginal = LU , since everypair consisting of a solution x and a right-hand side b = Ax satisfies by construction Ux = y

and Ly = b .

Row pivoting. An essential point in (4.28) is the fact that the pivot Akk in de k–th step shouldbe non-zero. However, this is not true in general as can be inferred from the following example

(0 11 0

)(x1

x2

)=

(b1

b2

).

This system is perfectly solvable, but algorithm (4.30) does not work, because A11 = 0. Theremedy is to interchange the order of both equations,

(1 00 1

)(x1

x2

)=

(b2

b1

),

and, hence, the interchange of both rows of the matrix. It is clear that this does not change theorder of the unknowns x1 and x2 .

Page 21: An Introduction to Numerical Linear Algebrahomepages.vub.ac.be/~pdegroen/numeriek/CursusNA2BaEng.pdf · 1 A MINI-TUTORIAL TO MATLAB 2 1 A mini-tutorial to MATLAB “Matlab” is an

4 LINEAR ALGEBRA 21

Consider now the general case. Let us assume that algorithm (4.28) has executed k−1 elimi-nation stages in which the elements below the diagonal in the first up to the k−1–st column havebeen zeroed. Hence, the system Ax = b has been reduced to the equivalent system

A11 · · · · · · A1k · · · A1n

0. . .

......

.... . .

. . ....

...0 · · · 0 Akk · · · Akn...

......

...0 · · · 0 Ank · · · Ann

x1......

xk...

xn

=

b1......bk...bn

. (4.32)

In the next stage of algorithm (4.28) it is required, that Akk is non-zero. If the original matrixA is invertible, then the equivalent matrix A is invertible too. Since all elements in the first k−1places of the k–th up to n–th rows are zero, at least one element at the k–th place of these rowshas to be non-zero, otherwise A is singular. Hence, if Akk = 0, we can find a row with indexp > k such that Apk 6= 0 and we can interchange the corresponding p–th and k–th rows (and also

the elements bp and bk of the right-hand side) and continue the elimination of the k–th column.The element Apk is called the pivot of stage k.

With this addition Gaussian elimination reduces every (uniquely solvable) set of equationsto an equivalent upper triangular system, at least in theory. In practice, round-off errors maydisrupt this picture. In theory, every non-zero pivot Apk will do. In practice, if it is very small (in

absolute value) in comparison to other elements of the column, a multiplier Ajk/Apk may becomevery large, such that the original j-th row is drowned in the round-off errors by the operationrow(j)← row(j)−Ajk/Apk×row(p); as a consequence this new j–th row is (almost) dependent onthe p–th and the new matrix becomes (nearly) singular. This problem can be avoided by choosingthe largest (in absolute value) from the column Akk , · · · , Ank as the pivot. This strategy impliesthat no multiplier has an absolute value larger than one.

for k = 1 : n− 1Search among the elements Akk , · · · , Ank

the element largest in absolute value. Assume it has row-index p.Interchange row(k) and row(p) of A and the elements bk and bp of the r.h.s.for j = k + 1 : n

replace row(j) of A by row(j) −Ajk/Akk × row(k)

and replace the j-th element bj of the r.h.s. by bj −Ajk/Akk × bk

end

end

(4.33)

In Matlab this can be coded in the following way2:

for k=1:n-1,

[m,p] = max(abs(A(k:n,k))); p = p+k-1;

hulp = A(k,k:n); A(k,k:n) = A(p,k:n); A(p,k:n)=hulp;

hulp = b(k); b(k) = b(p); b(p)=hulp;

A(k+1:n,k) = A(k+1:n,k)/A(k,k);

for j=k+1:n, A(j,k+1:n) = A(j,k+1:n) - A(j,k)*A(k,k+1:n); end

b(k+1:n) = b(k+1:n) - A(k+1:n,k)*b(k);

end

(4.34)

Finally, we can store all elimination information; the multipliers A(j,k) / A(k,k) go into thefree locations A(j,k) (j > k) and the row index of the pivot in the k-th stage is stored in the k-th

2Because the Matlab standard function max computes the maximum m and its row index p in the vectorz(1:n-k+1)=abs(A(k:n,k)), which has n-k+1 elements numbered from 1 up to n−k+1, we have to correct theoffset of this index by adding k-1 in order to find the correct position in the matrix.

Page 22: An Introduction to Numerical Linear Algebrahomepages.vub.ac.be/~pdegroen/numeriek/CursusNA2BaEng.pdf · 1 A MINI-TUTORIAL TO MATLAB 2 1 A mini-tutorial to MATLAB “Matlab” is an

4 LINEAR ALGEBRA 22

place of an additional array p of permutation indices. As before, we can now split the algorithmin an elimination phase and a solution phase, as in (4.31):

for k=1:n-1,

[m,q] = max(abs(A(k:n,k))); p(k) = q+k-1;

if p(k)>k, hulp = A(k,1:n); A(k,1:n) = A(p(k),1:n); A(p(k),1:n)=hulp; end

A(k+1:n,k) = A(k+1:n,k)/A(k,k);

A(k+1:n,k+1:n) = A(k+1:n,k+1:n) - A(k+1:n,k)*A(k,k+1:n);

end

for k=1:n-1,

if p(k)>k, hulp = b(k); b(k) = b(p(k)); b(p(k))=hulp; end

b(k+1:n) = b(k+1:n) - A(k+1:n,k)*b(k);

end

(4.35)

We remark that we changed more in the transition from (4.34) to (4.35); we interchanged inthe k-th stage not only the the elements A(k, k : n) and A(p, k : n), but also the multipliersA(k, 1 : k−1) and A(p, 1 : k−1), that have been formed in the previous stages. This is motivatedby the following observation. At the start of the k–th stage of the basic algorithm (4.32) we haveA(1) := Aoriginal = L(k)A(k),

A(1) := Aoriginal =

1 0 · · · · · · 0...

. . .. . .

...

Lk1 · · · 1. . .

...... · · · 0

. . . 0Ln1 · · · 0 0 1

A(k)11 · · · A

(k)1k · · · A

(k)1n

0. . .

......

0 · · · A(k)kk · · · A

(k)kn

0...

...0 · · · A

(k)nk · · · A

(k)nn

. (4.36)

where the non-trivial elements of A(k) are stored in the matrix elements A(i,j) with i ≤ j orj ≥ k and where the non-trivial elements of L(k) are contained in A(i,j) with i > j and j < k. Inthe k–th stage we multiply A(k) from the left by the matrix G−1

k (dubbed “Gauss Transformation”by Golub & Van Loan)

G−1k :=

1 0 · · · 0...

. . ....

0 · · · 1. . .

...... · · · − Lk+1,k 0

... · · · ...

0 · · · − Lnk · · · 1

and Gk :=

1 0 · · · 0...

. . ....

0 · · · 1. . .

...... · · · Lk+1,k 0

... · · · ...

0 · · · Lnk · · · 1

where Ljk := A(k)jk /A

(k)kk for j = k+1 · · · n. This results in A(k+1) = G−1

k A(k) . In order to maintainthe identity

Aoriginal = L(k)A(k) (4.37)

at the start of the next stage, we have to multiply L(k) from the right by Gk . This has preciselythe effect that the k–th column of Gk is inserted as the k–th column of L(k) (check!).

The interchange of row(k) and row(p) in A(k) can be described as the multiplication of A(k)

from the left by the permutation matrix Pk , which has the form

Pk :=

1 · · · 0. . .

0 · · · 1...

......

...1 · · · 0

. . .

0 · · · 1

row(k)

row(p)

(4.38)

Page 23: An Introduction to Numerical Linear Algebrahomepages.vub.ac.be/~pdegroen/numeriek/CursusNA2BaEng.pdf · 1 A MINI-TUTORIAL TO MATLAB 2 1 A mini-tutorial to MATLAB “Matlab” is an

4 LINEAR ALGEBRA 23

In order to maintain the identity (4.37), we have to multiply L(k) from the right by the samepermutation matrix Pk; this means the interchange of the columns of L(k) with indices k andp(k). Because this product is not a lower triangular matrix, we also multiply L(k) from the leftby Pk and we add the permutation matrix to the invariant

Aoriginal = P1 · · ·Pk−1PkPkL(k)PkPkA

(k). (4.39)

To the matrix PkA(k) (whose rows k and p(k) are interchanged) we apply the Gauss transformation

G−1k from the left and to PkL

(k)Pk we apply Gk from the right, such that

Aoriginal = P1 · · ·Pk−1PkPkL(k)PkGkG−1

k PkA(k) = P1 · · ·Pk−1PkL

(k+1)A(k+1) (4.40)

where L(k+1) := PkL(k)PkGk and A(k+1) := G−1

k A(k).

In this way we construct the decomposition A = PLU in a product consisting of a lower triangularmatrix L with Ljj = 1 and |Lij | ≤ 1 if i > j and and upper triangular matrix U and a productP := P1 · · ·Pn of permutation matrices. This proves the correctness of algorithm (4.35) and itproves the existence of a decomposition of the form A = PLU for every invertible matrix A.

4.g The algorithm of Crout

Using the knowledge that the decomposition A = PLU exists for every invertible matrix, we canderive the existence and the construction of it in a different way, that leads to variants of thealgorithm. We assume for the sequel that the row interchanges already have been applied to A(or are not necessary) and that we have A = LU , or componentwise:

Aik =

min{i,k}∑

j=1

LijUjk or

Akk =∑k−1

j=1 LkjUjk + Ukk if i = k (a)

Aki =∑k−1

j=1 LkjUji + Uki if i > k (b)

Aik =∑k−1

j=1 LijUjk + LikUkk if i > k (c)

(4.41)

where the indices i and k in (b) are interchanged to get i ≥ k in all equations. If k = 1, thesums are empty and we see that the first row of U is equal to the first row of A and that the firstcolumn of L is equal to the first column of A divided by U11 = A11. If the first k−1 columns ofL and the first k−1 rows of U have been computed (if Lij and Uji with j < k are known for agiven k), then Ukk can be computed from equation (a) and the remaining part of the k-th rowof U can be computed from equation (b). Finally, with Ukk known, the elements of the k–thcolumn of L can be computed from equation (c). Thus we find the algorithm of Crout for theLU-decomposition of A (without row interchanges):

for k = 1 : n,

Ukk = Akk −∑k−1

j=1 LkjUjk ;

Uki = Aki −∑k−1

j=1 LkjUji ; (i = k + 1 · · · n)

Lik = (Aik −∑k−1

j=1 LijUjk)/Ukk ; (i = k + 1 · · · n)

end

(4.42)

Since an element Apq of A is only addressed once for the computation of the corresponding Upq

or Lpq and is no more used thereafter, we may overwrite the memory location by this elementof L or U , as we did in (4.31) (Gauss elimination). So we derive the algorithm, known as the“Crout’s LU-decomposition”

for k=1:n,

A(k,k) = A(k,k) - A(k,1:k-1)*A(1:k-1,k);

A(k,k+1:n) = A(k,k+1:n) - A(k,1:k-1)*A(1:k-1,k+1:n);

A(k+1:n,k) = (A(k+1:n,k) - A(k+1:n,1:k-1)*A(1:k-1,k))/A(k,k);end

(4.43)

Page 24: An Introduction to Numerical Linear Algebrahomepages.vub.ac.be/~pdegroen/numeriek/CursusNA2BaEng.pdf · 1 A MINI-TUTORIAL TO MATLAB 2 1 A mini-tutorial to MATLAB “Matlab” is an

4 LINEAR ALGEBRA 24

This algorithm is only a reordering of the computations in comparison to Gauss elimination. Letus consider a fixed element (memory location) Apq. During Gauss elimination (4.31) it is accessedin every stage k < min(p, q) for one subtraction Apq ← Apq−ApkAkq, whereas all subtractions aredone at once in the k = min(p, q)–th stage of (4.43) [A(p,q) = A(p,q) - A(p,1:k-1)*A(1:k-1,q)].This implies that the round-off errors made during the computations are exactly the same3 forboth algorithms. This observation also provides the clue, how to incorporate the row-exchangestrategy of Gaussian elimination in Crout’s variant.

Exercise. Incorporate the row-exchange strategy of Gaussian elimination in Crout’s LU-decom-position and write down the algorithm in Matlab.

4.h Round-off error analysis

From Crout’s algorithm we see that every element of L and U is calculated from an equation ofthe form

Aik =

min{i,k}∑

j=1

LijUjk .

This is exactly the form of example 5 in section 3. Hence, the computed values Lij and Ujk ofthe elements of L and U satisfy exactly the equations

Aik =

min{i,k}∑

j=1

LijUjk(1 + εijk) where | εijk | ≤ (j+1)η .

As a consequence, the computed matrices L and U satisfy exactly a neighbouring equation

A = L U + E where |Eik | ≤ (n + 1) η

min{i,k}∑

j=1

| LijUjk . | (4.44)

So, the perturbation matrix E satisfies

‖E‖∞ ≤ (n + 1) η ‖L‖∞ ‖U‖∞ (4.45)

Since all elements of L are smaller than or equal to 1 in absolute value because of the rowinterchanges, the max-norm of this matrix is bounded by n. The most important factor in thebound for E is the magnitude of ‖U‖∞ . Although in practice this norm is comparable to that ofA, examples exist in which this norm is 2n−1 times as large (see exercise 5 below). We concludethat Gaussian elimination with row interchanges (pivoting) in general is a very reliable and robustmethod for the solution of a system of linear equations.

4.i Exercises

1. There is a method to minimize the potential ill-conditioning of the factor U in Gaussianelimination by incorporating both row and column interchanges. In Gaussian eliminationwith pivoting, we search in the k–th stage for the maximal element of abs(A(k:n,k)) andinterchange the corresponding rows. We can do better when we search for the largest elementof the whole sub-matrix abs(A(k:n,k:n)) and bring this element in the (k,k)-position byinterchanging both the corresponding row and column with the k–th row and column respec-tively. Columns can be interchanged by multiplying the matrix by a permutation matrix(4.38) from the right. All permutations from the right can be aggregated in one matrix Q

3Provided, no additional rounding occurs, when a result of one or of a series of arithmetical operations betweenregisters in the processor is written back from a register to the memory.

Page 25: An Introduction to Numerical Linear Algebrahomepages.vub.ac.be/~pdegroen/numeriek/CursusNA2BaEng.pdf · 1 A MINI-TUTORIAL TO MATLAB 2 1 A mini-tutorial to MATLAB “Matlab” is an

4 LINEAR ALGEBRA 25

resulting in a decomposition A = P LU Q where L and U are lower and upper triangular ma-trices, pre- and post-multiplied by permutation matrices P and Q Write down the algorithmin Matlab.

2. When the decomposition A = LU is given, we compute the solution x of Ax = b by solvingfirst y from Ly = b and subsequently x from Ux = y . Show that the computed vectors y

and x are the exact solutions of the neighbouring equations (L+F )y = b and (U +G)x = y

for some perturbation matrices |Fi,j | ≤ (i + 1)η |Li,j | and |Gi,j | ≤ (i + 1)η |Ui,j | .

3. Let A ∈ IRn×n be a regular matrix and let u , v ∈ IRn be column vectors. Assume vT A−1u 6=−1 . Prove the Sherman-Morrison formula :

(A + uvT )−1 = A−1 − A−1uvT A−1

1 + vT A−1u. (4.46)

4. For given vector y ∈ IRn and index k ∈ IN , a matrix of the form

N( y , k ) := I + y eTk ∈ IRn×n

is called a Gauss-Jordan transformation.

a. Inder what condition on y the matrix N(y , k) is invertible; find a formula for its inverse.

b. Given a (fixed) vector x ∈ IRn, under what conditions a vector y ∈ IRn exists such that

N(y , k) (x) = ek.

Deduce a formula for it.

c. Deduce an algorithm that overwrites the matrix A by its inverse A−1 using n Gauss-Jordantransformations.

d. What conditions on A ensure that the algorithm is succesful?

5. Let A ∈ IRn×n be a matrix with elements

Akk = 1 , Aki = −1 if i < k , Akn = 1 and Aki = 0 if k < i < n .

Calculate the LU–decomposition of A and the norms ‖A‖∞ and ‖U‖∞ .This is an example in which the norm of U is much larger than the norm of A is.

6. Let A ∈ IRn×n be a rowwise diagonally dominant matrix, i.e.

if A = (aij )ni,j=1 then | ajj | >n∑

i=1, i6=j

| aji | , ∀ j,

Prove that A has an LU–decomposition without row permutations with a U–factor satisfying‖U ‖∞ ≤ 2maxk |Ukk|.

Hint: Show that the submatrix A(2 : n, 2 : n) after the first stage of the Gaussian eliminationis again diagonally dominant.Remark: The norm of the factor L may become very large in this case, as can be seen from thefollowing example. We display the matrix, the result after the first stage of the eliminationand the final result after the second stage :

A :=

1√

α 0√α 1 0

0 α 1

=

1 0 0√α 1 0

0 0 1

1√

α 00 1− α 00 α 1

=

1 0 0√α 1 0

0 α

1−α1

1√

α 00 1− α 00 0 1

.

Page 26: An Introduction to Numerical Linear Algebrahomepages.vub.ac.be/~pdegroen/numeriek/CursusNA2BaEng.pdf · 1 A MINI-TUTORIAL TO MATLAB 2 1 A mini-tutorial to MATLAB “Matlab” is an

5 LINEAR LEAST SQUARES PROBLEMS 26

For every α ∈ [0, 1) the matrix A is diagonally dominant, but ‖L‖∞ ր∞ as αր 1 .When the usual row interchange strategy is used, we have to interchange the second andthird rows in the second stage if α ∈ (1

2 , 1). In that case we find

A =

(1 0 00 0 10 1 0

) (1 0 00 1 0√α 0 1

) (1

√α 0

0 α 10 1 − α 0

)=

(1 0 00 0 10 1 0

) (1 0 00 1 0√α 1−α

α1

) (1

√α 0

0 α 10 0 α−1

α

).

However, we observe the conservation of trouble; instead of an L–factor with a large normwe find a badly conditioned U -factor as α ≈ 1 .Analogously: If A is columnwise diagonally dominant, then A has an LU–decompositionwithout pivoting with a bound on the L-factor ‖L ‖1 ≤ 2 .

5 Linear Least Squares Problems

A standard example of the origin of linear least squares problems is the (statistical) question tofind the best fitting line to a number of datapoints {(x1 , y1) · · · (xn , yn)} in the plane (regres-sion).

-5

0

5

0

xx

x

x

xx

x x

x

x

x

a. best line when y is considerd a function of x

-5

0

5

0

xx

x

x

xx

x x

x

x

x

b. best line when x is considerd a function of y

-5

0

5

0

xx

x

x

xx

x x

x

x

x

d. the three approximating lines in one plot

-5

0

5

0

xx

x

x

xx

x x

x

x

x

c. total least squares approximation

Figure 3: Datapoints in the plane with “best fitting” lines. In each of the pictures (a), (b) and(c) this is the line that minimizes the sum of squares of the length of the dotted lines,the distances along the x-axis (a), the distance along the y-axis (b), the Euclideandistance (c). In (d) the three regression lines are plotted for comparison.

The question is to find a line y = a+b x such that the sum of squares of the deviations is minimal:

find (a , b) such that J(a, b) :=n∑

k=1

(yk − a− bxk)2 is minimal. (5.1)

Among all optimality criteria the minimisation of a sum of squares is by far the easiest, becausethis functional is quadratic in the unknowns a and b, implying a unique minimum, that is thesolution of a set of linear equations.

Page 27: An Introduction to Numerical Linear Algebrahomepages.vub.ac.be/~pdegroen/numeriek/CursusNA2BaEng.pdf · 1 A MINI-TUTORIAL TO MATLAB 2 1 A mini-tutorial to MATLAB “Matlab” is an

5 LINEAR LEAST SQUARES PROBLEMS 27

The functional J in (5.1) can be viewed as the square of a norm in IRn; define the vectors x ,y and e ∈ IRn and the matrix A ∈ IRn×2,

x :=

x1...

xn

, y :=

x1...

yn

, e :=

1...1

and A := (e |x) ,

then J can be written as

J(a, b) = ‖A(

ab

)− y ‖2

and we can interprete problem (5.1) as the search for the point in the image of A, nearest to y

(in Euclidean norm).

This can be generalised as follows. Given a matrix A ∈ IRm×n with m ≥ n and a vectorb ∈ IRm

find x ∈ IRn such that J(x) := ‖Ax− b ‖2 is minimal. (5.2)

Stated otherwise, find the point x in the image of A (= Im(A)) that is nearest to b (in Euclideannorm) and find its pre-image.In the sequel we shall assume, that matrix A is of full rank, such that A, considerd as a transfor-mation from IRn onto Im(A) is one-to-one and invertible.

5.a The normal equations

A simple method for solving problem (5.2) is, to use the geometric argument that the distance

��

��

��

��

��

��

��

��

��

��

��

-������������*AAAAAAU

O b

Im(A)

Ax

b−Ax

Figure 4: The vector b, its orthogonal projection Ax on Im(A) and the residualb−Ax . The residual is minimal if it is perpendicular to Im(A).

between the vector b and a vector y ∈ Im(A) is minimal if their difference is perpendicular toIm(A). Hence,

‖Ax− b ‖2 is minimal ⇔ Ax− b ⊥ Im(A) ⇔ zT AT (Ax− b) = 0 for all z ∈ IRn.

As a consequence, the search for the minimum of (5.2) is equivalent with the solution of the linearsystem

AT Ax = ATb . (5.3)

These equations are called the normal eqations corresponding to the least squares problem (5.2).

If A ∈ IRm×n (m>n) is of full column rank, rank(A) = n, then AT A is symmetric and positivedefinite and (5.2) has a unique solution that can be computed by calculating first AT A and ATb

and subsequently solving system (5.3) using a Cholesky decomposition (a variant of Gauss orCrout elimination for a positive definite symmetric matrix, that requires only half of the numberflops).

This solution method via the normal equations will work also if m = n and, hence, if A issquare and invertible (because we assume it is of full rank). The least squares problem (5.2) and

Page 28: An Introduction to Numerical Linear Algebrahomepages.vub.ac.be/~pdegroen/numeriek/CursusNA2BaEng.pdf · 1 A MINI-TUTORIAL TO MATLAB 2 1 A mini-tutorial to MATLAB “Matlab” is an

5 LINEAR LEAST SQUARES PROBLEMS 28

its solution method via the normal equations (5.3) is then equivalent to the solution of problemAx = b. However, the condition numbers of both problems (w.r.t. the ‖ · ‖2–norm)

κ2(A) =σ1

σn

and κ2(AT A) =

σ21

σ2n

(5.4)

where σ1 and σn are the largest and smallest singular vaues of A, may be quite different. Thecondition number of the problem, we have to solve using the normal equations, is the square ofthe condition number of the original problem. If the condition number of A is already large, itssquare may be huge, making the solution of the normal equations completely unreliable.

We also can define a condition number for problem (5.2) in case A is of full rank but notsquare. In that case A is a one-to-one mapping onto Im(A); its restriction to a transformationfrom IRn to Im(A) has a well-defined inverse. So we can solve the least squares problem (5.2)(in theory) computing first the orthogonal projection y of b on Im(A) and solving subsequentlythe consistent system of equations Ax = y. De sensitivity of this problem is then characterisedby the condition number κ2(A) = σ1/σn, where σ1 and σn are (again) the largest and smallestsingular values of the restriction of A. Clearly we should define the condition number of the leastsquares problem (5.2) (w.r.t. the ‖ · ‖2–norm) as the condition number of this restriction. In thisview, the solution of the least squares problem (5.2) from the normal equations always impliessquaring of the condition number. If A is well conditioned, this is no problem. However, if A isbadly conditioned, this may produce a “solution” that is completely unreliable. There are severalmethods to circumvent this squaring by exploiting the idea of orthogonality.

5.b The method of Gram-Schmidt

The method of normal equations (5.3) is derived from the observation, that the residual b−Ax

is perpendicular to Im(A). The construction of an orthogonal basis in Im(A) provides an easymethod for the computation of the orthogonal projection y of b on Im(A) and the computationof the solution of the compatible system of equation Ax = y. This can be accomplished by themethod of Gram-Schmidt. The columns of the matrix A = (a1 | · · · |an) form a basis in Im(A);the method of Gram-Schmidt computes from this an orthogonal basis as follows:

Normalise the first column of A and denote it by q1 ,

q1 := a1/‖a1‖ ;for k = 2 : n , do

Orthogonalise the k–th column of A w.r.t. all previous ones (i.e. ⊥ to {q1 · · · qk−1}),ak := ak −

∑k−1j=1 qT

j ak qj ;

Normalise the result and denote it by qk ,

qk := ak/‖ak‖ ;end

(5.5)

This produces the orthonormal basis {q1 · · · qn} for Im(A). The relation between the vectors ofthe original basis {a1 · · · an} and those of the new one is

ak =k−1∑

j=1

qTj ak qj + ‖ak‖qk , (5.6)

or in matrix notation,

A = QR with Q :=(q1 | · · · |qn

), R =

(rjk

), rjk =

qTj ak if j < k ,

‖ak‖ if j = k ,

0 if j > k .

(5.7)

Page 29: An Introduction to Numerical Linear Algebrahomepages.vub.ac.be/~pdegroen/numeriek/CursusNA2BaEng.pdf · 1 A MINI-TUTORIAL TO MATLAB 2 1 A mini-tutorial to MATLAB “Matlab” is an

5 LINEAR LEAST SQUARES PROBLEMS 29

Using this new basis the projection of b in Im(A) is given by y =∑n

k=1(qTk b)qk . With this

projection the system Ax = y is compatible because y ∈ Im(A), but this system cannot besolved in practice because round-off errors may drive the computed projection outside Im(A).The practical solution method comes from the fact that R is the matrix of the (abstract) trans-formation A with respect to this new basis of Im(A) and that the coefficients of y in this basisare {qT

1 b , · · · , qTnb}. So we only have to solve Rx = QTb , which is easy because R is upper

triangular.

Summarizing, we find the (GS) algorithm: Compute by the Gram-Schmidt method (5.5) adecomposition of the matrix A = QR into factors Q ∈ IRm×n with orthonormal columns andR ∈ IRn×n upper triangular and solve the upper triangular system Rx = QTb by (4.25). We caneasily prove the correctness. Since R is invertible we have

minx∈IRn

‖Ax− b‖ = minx∈IRn

‖QRx− b‖ = minx∈IRn

‖Qz− b‖ . (5.8)

The minimum of the right-hand side is given by the normal equations QT Qz = z = QTb (whichare perfectly conditioned with condition number 1). Hence, the minimum of the original problemis given by the solution of Rx = z = QTb.

In practice it is observed that this Gram-Schmidt method (5.5) may be quite sensitive torounding errors, in particular if the angles between the columns of A are small. We may stronglyimprove on this by the following heuristics. In the algorithm we have to orthogonalise ak withrespect to all predecessors {q1 · · · qk−1} and, hence to compute the inner products of ak withall those vectors. In example 4 of section 3.d we have derived the estimate (3.8) for the absoluterounding error in the computed value of an inner product

|fl(qTj ak)− qT

j ak | ≤ nη ‖qj‖2 ‖ak‖2 = nη ‖ak‖2 because ‖qj‖2 = 1 .

Let us consider the sequential computation of qT1 ak , qT

2 ak , etc. for some k > 2. When we havefinished the computation of the inner product qT

1 ak and we are to begin the calculation of thenext inner product qT

2 ak, we may also use the formula qT2 (ak−αq1), which should be independent

of α, because qT2 q1 = 0 by definition. In a rounding environment this independence is likely to

be lost and a good choice of α may make the difference. For the absolute error in the computedvalue we have the α–dependent upper bound

|fl(qT2 (ak − αq1))− qT

2 (ak − αq1) | ≤ nη ‖ak − αq1‖2 , (5.9)

which is minimal if ak − αq1 ⊥ q1 . So we can minimize the upper bound (5.9) when weorthogonalise ak w.r.t. q1 before we start the computation of the inner product with q2 . Next weorthogonalise ak w.r.t. q2 before we compute the inner product with q2 , and so on. Minimisationof an upper bound for the error obviously does not imply that the actual error is minimal, butthis strategy is the best feasible. This strategy results in a numerically stable variant of (5.9),which has been dubbed MGS or “Modified Gram Schmidt” in literature:

r11 := ‖a1‖ ; q1 := a1/r11 ;

for k = 2 : n , do

for j = 1 : k − 1 , do

rjk = qTj ak ; ak := ak − rjk qj ;

end

rkk := ‖ak ‖ ; qk := ak/rkk ;end

(5.10)

In the computation of c := QTb we obviously have to apply the same idea:

for k = 1 : n , do ck := qTk b ; b := b− ckqk ; end , (5.11)

Page 30: An Introduction to Numerical Linear Algebrahomepages.vub.ac.be/~pdegroen/numeriek/CursusNA2BaEng.pdf · 1 A MINI-TUTORIAL TO MATLAB 2 1 A mini-tutorial to MATLAB “Matlab” is an

5 LINEAR LEAST SQUARES PROBLEMS 30

and also here we have to orthogonalise immediately after the computation of each inner product.It can be shown, that MGS is a numerically stable algorithm that produces a (computed) QR-decomposition that is the exact decomposition of a neighbouring matrix. This implies that therounding errors in the solution x are dominated by those produced by the solution of the triangularsystem Rx = QTb , which are bounded by the condition number of R or, equivalently, thecondition number of A. We conclude that MGS should always be preferred to normal equations,although it is somewhat more expensive in flop count.

Exercises.

1. Show that the solution of (5.2) by MGS requires 2mn2 + O(n2) flops and that its solution bynormal equations requires “only” mn(n+1) + n3/3 + O(n2) flops.

2. We can do the MGS computations in a different order,

for k = 1 : n , do

rkk := ‖ak ‖ ; qk := ak/rkk ;for j = k + 1 : n , do

rkj = qTk aj ; aj := aj − rkj qk ;

end

end .

(5.12)

In the k–th stage we first normalise ak into the vector qk and we subsequently normalise theremaining columns ak+1 · · · an with respect to this vector qk . Obviously, we may overwritethe columns of Q by those of A in practice.Show that this order is equivalent to (5.10), inclusive the MGS-idea that immediately aftereach inner product computation the vector is orthogonalised w.r.t. this vector.

3. We can obtain a further improvement of the accuracy (mainly useful if the matrix is rank-deficient in order to produce a “rank revealing decomposition”), when in each stage k we assignto qk the largest of the remaining columns ak · · · an and orthogonalise the other columnswith respect to this one. The usual strategy is to interchange the largest column with thek–th and store its index for later use. This implies that we have to know the length of allremaining vectors at the begin of each stage. At first sight the computation of all those normsin each stage requires 2m(n − k + 1) flops in the k-th stage. However, using the Pythagorastheorem we can derive the square of the the norm of a vector in stage k + 1 from its value instage k using only 2 flops. This results in a decomposition of the form A = QRP where P isa permutation matrix.

In order to derive a correct implementation of those interchanges, we have a more preciselook at algorithm (5.12). In the k-th stage we add the upper index (k) to the update of

the j-th column, denoting this update by a(k)j ; hence, a

(0)j is equal to the j-th column of the

original matrix A. We define for k = 0 , · · · , n intermediate results

Q(k) := (q1 | · · · |qk |0 | · · · |0 ) ∈ IRm×n ,

A(k) :=(0 | · · · |0 |a(k)

k+1 | · · · |a(k)n

)∈ IRm×n and

R(k) :=

r11 · · · · · · · · · · · · r1n

0. . .

......

. . . rkk · · · · · · rkn...

. . . 0 · · · 0...

. . .. . .

...0 · · · · · · · · · 0 0

∈ IRn×n .

(5.13)

This implies, that Q(0) , R(0) en A(n) are null matrices and that A(0) = A is the original

Page 31: An Introduction to Numerical Linear Algebrahomepages.vub.ac.be/~pdegroen/numeriek/CursusNA2BaEng.pdf · 1 A MINI-TUTORIAL TO MATLAB 2 1 A mini-tutorial to MATLAB “Matlab” is an

5 LINEAR LEAST SQUARES PROBLEMS 31

matrix. Hence, A = Q(0) R(0) + A(0) is true at the beginning of the algorithm:

for k = 1 : n , do

rkk := ‖a(k−1)k ‖ ; qk := a

(k−1)k /rkk ;

for j = k + 1 : n , do

rkj = qTk a

(k−1)j ; a

(k)j := a

(k−1)j − rkj qk ;

end{a

(k−1)j = qk rkj + a

(k)j voor j = k+1 · · · n en dus geldt A = Q(k) R(k) + A(k)

}

end

(5.14)

In each stage we add a column to Q and a row to R, we remove a column from A and weupdate it such that the equality (the invariant) A = Q(k) R(k) + A(k) remains true. So wehave the equality A = QR at the end.Applying the column interchange in the k-th stage, we have to bring the longest column ofA(k−1) on the k-th position. This accomplished by multiplying it (from the right) by thepermutation matrix (4.38). In order to conserve the invariant A = Q(k−1) R(k−1) + A(k−1),we have to apply this to every term in it.Write down the Matlab code for this variant of MGS with column interchanges.

5.c Householder Transformations

Around 1950 A.S. Householder proposed an elegant and numerically stable method for the com-putation of the solution of the least squares problem (5.2) by orthogonalisation. For a givennon-trivial vector u ∈ IRm we define the Householder transformation Hu ∈ IRm×m by

Hu := I − 2uuT

uTu, I is the identity matrix. (5.15)

This transformation satisfies the following properties:

a. Hu is symmetric and orthogonal,

Hu = HTu and HT

u Hu = I − 4uuT

uTu+

4uuT uuT

(uT u)2= I

b. Hu maps u onto − u and leaves all vectors in u⊥ invariant, Huv = v if v ⊥ u . A vectorw ∈ IRm can be decomposed into a component parallel to u and a component perpendicularto u. The first component is mapped onto minus itself and the second part remains invariant.Hence, Hu is a reflection of the space with respect to the plane perpendicular to u , see figure 5.

The vector u is called the Householder vector or the reflection vector. For a given reflection vectoru we can compute the image Huw of a vector w ∈ IRm .

We can go the other way around an ask for a reflection vector u such that the correspondingHouseholder transformation maps a given vector w onto the mirror image v. Obviously, thosevectors should have the same length ‖v ‖ = ‖w ‖ because the reflection is orthogonal. If thisis true, we see from figure 5, that v is the mirror image of w if the “mirror” is the bisecting(hyper)plane of v and w. This plane is (v −w)⊥, the subspace of all vectors perpendicular tothe difference v −w. Indeed, if ‖v‖ = ‖w‖ and zT (v −w) = 0, the difference of the squares ofthe distances from z to v and w satisfies:

‖v − z‖2 − ‖w − z‖2 = ‖v‖2 − 2zT v + ‖z‖2 − ‖w‖2 + 2zT w − ‖z‖2 = 0 .

With the help of a sequence of such Householder transformations or reflections we can trans-form a matrix A ∈ IRm×n into upper triangular form in a way analogous to Gaussian elimination.

Page 32: An Introduction to Numerical Linear Algebrahomepages.vub.ac.be/~pdegroen/numeriek/CursusNA2BaEng.pdf · 1 A MINI-TUTORIAL TO MATLAB 2 1 A mini-tutorial to MATLAB “Matlab” is an

5 LINEAR LEAST SQUARES PROBLEMS 32

{λu}

wHuw

u⊥

O u�

��

��

��

��>

ZZ

ZZ

ZZ

ZZZ}

-

Figure 5: A vector w, its decomposition in a component parallel to the reflection vector u

and a component perpendicular to it and the mirror image Huw with respect to the(hyper)plane perpendicular to u.

In the k–th stage of Gaussian elimination the matrix is multiplied from the left by a Gaussiantransformation Gk , that sets to zero all elements of the k–th column below the main diagonal,see (4.36). The same can be done by a suitable Householder transformation.

Let us consider the first stage; we want to find a reflection vector u1 such that Hu1

maps the

first column a1 of A on (α, 0, · · · 0)T = α e1 , a vector whose components are all zero except forthe first. Because the length is invariant, we may choose α in two ways, α = ±‖a1‖. Hence, thetwo reflection vectors are a1 ∓ ‖a1‖ e1. Those vectors differ from a1 in their first componentsonly. So we are able to choose the sign in the first component a11 ∓ ‖a1‖ in such a way, that noloss of significance is occurring. So we choose plus if both terms have the same sign and minusif the signs are opposite4,

u1 = a1 + sign(a11)‖a1‖ e1 , (5.16)

such that

uT1 u1 = aT

1 a1 + 2 sign(a11)‖a1‖ eT1 a1 + ‖a1‖2 eT

1 e1 = 2‖a1‖(‖a1‖+ | a11 | ) ,

uT1 a1 = aT

1 a1 + sign(a11)‖a1‖ eT1 a1 = ‖a1‖(‖a1‖+ | a11 | ) ,

Hu1a1 = a1 −

2uT1 a1

uT1 u1

u1 = − sign(a11)‖a1‖ e1 .

(5.17)

This implies:

Hu1A =

α1 a12 · · · a1n

0 a22 · · · a2n...

......

0 am2 · · · amn

, α1 = −sign(a11) .

In an analogous way we can transform the elements {a32 · · · am2} of the second column to zeroby a reflection that maps

a2 :=

0a22...

am2

onto

0α2

0...0

using the reflection vector u2 = a2−α2‖a2‖ e2 , α2 = −sign(a22).

Here, it should be clear that the first component of u2 has to be zero, since the first row of Hu1A

should not change any more in the multiplication by Hu2.

4The function “sign” here has the value +1 if its argument is non-negative and the value −1 otherwise. TheMatlab function sign cannot be used since it is zero if its argument happens to be zero.

Page 33: An Introduction to Numerical Linear Algebrahomepages.vub.ac.be/~pdegroen/numeriek/CursusNA2BaEng.pdf · 1 A MINI-TUTORIAL TO MATLAB 2 1 A mini-tutorial to MATLAB “Matlab” is an

5 LINEAR LEAST SQUARES PROBLEMS 33

Continuing in this way we find n reflection vectors u1 , · · · , un and the associated Householdertransformations transform A into an upper triangular matrix R ,

Hun

· · · Hu1A =

r11 · · · r1n

0. . .

......

. . . rnn... 0...

...0 · · · 0

=: R (5.18)

such that

A = QR with Q := Hu1· · · Hu

n

∈ IRm×m an orthogonal matrix. (5.19)

Also in this way we find a QR–decomposition of A. However, it differs strongly from (5.6). In(5.19) Q is an orthogonal (and hence square) matrix and R has the same dimensions as A has;in contrast, MGS only computes a matrix Q with orthonormal columns and a square matrix R.

In order to find the solution of the original least squares problem (5.2) using this Householderdecomposition, we split R in the square upper triangular matrix R1 ∈ IRn×n consisting of thefirst n (non-trivial) rows of R and an (m−n)×n submatrix consisting of zeros,

R =

(R1

0

).

If A is of full rank, R1 is invertible. The LS problem (5.2) is solved in the following way. Sincethe norm is invariant under othogonal transformations, we have:

‖Ax − b ‖2 = ‖QRx− b ‖2 = ‖Rx−QTb ‖2 .

Partitioning the vectors Rx =

(R1x

0

)and QTb =

(c

d

)in two vectors consisting of the first n

components and the remaining m−n components respectively, we find

‖Rx−QTb ‖2 = ‖(

R1x

0

)−(

c

d

)‖2 = ‖R1x− c ‖2 + ‖d‖2 .

The right-hand side is minimized by the solution of R1x = c (provided A is of full rank); thissolution solves the least squares problem (5.2). The residual is d .

The factorisation algorithm is:

for k = 1 : n,{Transform the part ak+1,k · · · am,k of the k-th column to zero

using a suitable Householder transformation;}α :=

√∑mj=k a2

jk ; {norm of the relevant vector}

uk := (0 · · · 0 , akk + α sign(akk) , ak+1,k · · · amk)T ; {reflection vector}

akk := − sign(akk) ∗ α ,

γ := α |ukk | , {= 12 × norm of uk}

for j = k + 1 : n , {apply the Householder transformation}aj = aj − uk uT

k aj/γ ; {to the remaining columns of A}end

b = b−uk uTk b/γ ; {apply the transformation to the right-hand side b}

end

(5.20)

Page 34: An Introduction to Numerical Linear Algebrahomepages.vub.ac.be/~pdegroen/numeriek/CursusNA2BaEng.pdf · 1 A MINI-TUTORIAL TO MATLAB 2 1 A mini-tutorial to MATLAB “Matlab” is an

5 LINEAR LEAST SQUARES PROBLEMS 34

After termination of the algorithm, the upper triangle of A contains the relevant (non-zero)elements of R and b contains the transformed vector QTboriginal . It remains to solve the uppertriangular system for the solution of the LS-problem. Clearly, it is not necessary to compute theorthogonal matrix Q explicitly.

Remark. In case it is useful to store Q for later use, the best strategy is to store rescaledreflection vectors; this uses less memory and much less flops than the explicit computation ofQ does. After the application of the reflection to A in the k-th stage, the content of the partak+1,k · · · am,k of the k–th column of A has become irrelevant. Hence, those (memory) locationscan be used to store the k+1–st up to m–th components of the k-th reflection vector divided byuk,k. By this division the k–th component is set to 1 and needs not be stored. The first up tok−1–st element are zero anyway. This way of storing Q in the form of the reflection vectors isreferred to as “in factored form”.

Exercises.

1. Show that algorithm (5.20) uses 2n2(m− n/3) + O(mn) flops; so it is more costly than themethod of normal equations is but less than MGS.Find the additional number of flops that is needed to compute Q explicitly.Compare the number of flops used for the computation of QTb with explicit and factored Q.

2. Write the code for algorithm (5.20) in matlab.

3. In the same way as in MGS (exercise 3 in section 6c) we can improve numerical stability bycolumn interchanges. In the k–th stage we interchange the k–th column with the longest ofthe remaining columns (with indices k up to n). Since the k–th reflection works only on thesub-vector consisting of the k–th up to m–th elements, we have to restrict the norms to thoseelements. The norms of the remaining columns need not be recomputed in each stage; sincethe number of relevant elements of the columns deminishes by one in each stage, we simplycan update the (squares of) each column norm with two flops. This improvement results ina (rank revealing) factorisation A = QRP of S in the product of an orthogonal matrix Q, anupper triangular matrix R and a permutation matrix P .Write the code of the algorithm in Matlab.

4. The pseudo-inverse or Moore Penrose inverse of a matrix A ∈ IRm×n, denoted by A†, isdefined via the SVD. If

A = UΣV T where Σ = diag(σ1 · · · σp) ∈ IRm×n and p := min{m,n}

is the singular value decomposition and if

rank(A) = r , such that σ1 ≥ · · · ≥ σr > 0 and σr+1 = · · · = σp = 0 ,

then the pseudo inverse is defined as

A† := V Σ†UT where Σ† := diag(σ−11 · · · σ−1

r , 0 · · · 0) ∈ IRn×m . (5.21)

Prove the following properties:

a. A† is map from IRm to IRn with kernel Ker(A†) = Im(A)⊥

and image Im(A†) = Ker(A)⊥.

b. A†A and AA† are orthogonal projections on Im(A) and Ker(A)⊥ respectively.

c. AA†A = A and A†AA† = A†.

d. If m ≥ n and rank(A) = n then A†b is the solution of the least squares problem (5.2).

e. If rank(A) < n (or m < n), then A†b is the solution of the least squares problem (5.2)

that has minimal norm, i.e. A†b = argminx∈IRn { ‖x ‖ x = argminy∈IRn ‖Ay − b‖2 }.Remark: Equivalently, the pseudo inverse A† can be defined as the (unique) matrix satisfying(b) and (c) (the Penrose conditions) and (5.21) is then a consequence.

Page 35: An Introduction to Numerical Linear Algebrahomepages.vub.ac.be/~pdegroen/numeriek/CursusNA2BaEng.pdf · 1 A MINI-TUTORIAL TO MATLAB 2 1 A mini-tutorial to MATLAB “Matlab” is an

5 LINEAR LEAST SQUARES PROBLEMS 35

5.d Givens rotations

A third (often used) way to factorise a matrix A into a product of an orthogonal transformationand an upper triangular matrix works with Givens rotations. The idea is easily explained in IR2.

The rotation of a vector (x, y)T ∈ IR2 over an angle ϕ is given by the matrix

J(ϕ) :=

(cos ϕ sinϕ− sin ϕ cos ϕ

)such that J(ϕ)

(xy

)=

(x cos ϕ + y sin ϕy cos ϕ− x sin ϕ

). (5.22)

For a given vector (x, y)T ∈ IR2 the angle ϕ can be chosen such that the second component ofthe image is zero, i.e.

J(ϕ)

(xy

)=

(x cos ϕ + y sin ϕy cos ϕ− x sin ϕ

)=

(z0

)such that y cos ϕ− x sinϕ = 0 . (5.23)

The cosine and the sine can be computed in two ways,

y2(1− sin2 ϕ) = x2 sin2 ϕ such that sin ϕ = ± y√x2 + y2

and cos ϕ =x sinϕ

y,

y2 cos2 ϕ = x2(1− cos2 ϕ) such that cos ϕ = ± x√x2 + y2

and sin ϕ =y cos ϕ

x,

(5.24)provided y 6= 0 or x 6= 0 respectively. We see that it is of no use to compute the angle ϕ explicitlyin the computation of the rotation matrix. It suffices to compute c := cos ϕ and s := sin ϕ viaone of both formulae in (5.24). Moreover, the choice of the sign is free. We may use this sign tomake the first component z of the image positive. In order to minimize the number of flops andthe rounding errors in the computation of c and s, this computation most often is implementedas follows (provided x and y are not both zero):

if |x | ≥ | y | then

t :=y

x; c :=

sign(x)√1 + t2

; s := t ∗ c ; z =x

c;

else

t :=x

y; s :=

sign(y)√1 + t2

; c := t ∗ s ; z =y

s;

end

(5.25)

A Givens rotation in IRm is an m×m–matrix of the form

J(k, ℓ, ϕ) :=

1. . .

1c · · · s · · ·

1...

. . ....

1−s · · · c · · ·

1...

.... . .

1

—- row(k)

—- row(ℓ)

|column(k)

|column(ℓ)

(5.26)

In this matrix all diagonal elements are 1, except those with index (k, k) and (ℓ, ℓ) , which areequal to c := cos ϕ . All other matrix elements are zero, except those with index (k, ℓ)– and (ℓ, k) ,

Page 36: An Introduction to Numerical Linear Algebrahomepages.vub.ac.be/~pdegroen/numeriek/CursusNA2BaEng.pdf · 1 A MINI-TUTORIAL TO MATLAB 2 1 A mini-tutorial to MATLAB “Matlab” is an

5 LINEAR LEAST SQUARES PROBLEMS 36

which are equal to s and −s respectively with s := sin ϕ . This matrix acts as a rotation in in theplane spanned by the k–th and ℓ–th coordinate vectors ek and eℓ .

Such a Givens rotation or “plane rotation” can be used to zero elements of a vector or amatrix selectively. The ℓ–th component of a vector a ∈ IRm can be rotated to zero by a Givensrotation J(k, ℓ, ϕ), in which the sine and cosine of the rotation angle ϕ are computed by formula(5.25) with x = ak and y = aℓ. Using a series of m−1 rotations of the form J(k, k + 1, ϕk) fork = m−1 : −1 : 1 we can transform the vector a to a multiple of the first coordinate vectore1. This can also be accomplished by a sequence of the form J(1, k, ϑk) in the order k = 2 : m(or k = m : −1 : 2). We conclude, that there is a large freedom in the choice of subsequentrotation planes. We have to take care that zeros, once they are created, do not disappear againin subsequent rotations.

In the same way the matrix A ∈ IRm×n can be transformed to upper trangular form using aseries of Givens rotations; e.g. we may chose the order (working along diagonals)

for k = m− 1 : −1 : 1 do

for j = 1 : min(n,m− k) do

Make ak+j,j zero by multiplying A from the left by the rotationJ(k+ j−1 , k+ j , ϕkj) in the plane spanned by ek+j−1 and ek+j ;c and s are to be computed from (5.25) with x = ak+j−1,j andy = ak+j,j .

end

end

(5.27)

Here too, we may choose different orders in which we zero out the elements of the lower triangle.The computation of a QR factorisation using Givens rotations requires more flops than thosewith Householder transformations or MGS. The advantage of Givens rotations is that elementsof a matrix can be zeroed selectively. Such an operation only affects the two rows involved andno other rows. This may be very important mainly for sparse matrices in which the largemajority of matrix elements are zero (e.g. produced by the discretisation of a partial differentialequations).

Exercise: 1. Write down the Matlab code for the solution of the least squares problem (5.2)using Givens rotations. Like in the Householder–QR we do not have to store the rotations, if weapply them immediately to the right-hand side.Show, that this algorithm (5.27) uses 3n2(m− n/3) + O(mn) flops.


Recommended