• No results found

ROUTINES FACTOR AND SOLVE

ESTIMATING CONDITION

2.4 ROUTINES FACTOR AND SOLVE

hence this matrix is very ill-conditioned. n

The purpose of the condition number estimator in Factor is not to get an accurate value for the condition number, rather to recognize when it is “large.” The inequality (2.22) is a worst-case bound. If the condition estimate of Factor indicates that the problem is ill-conditioned, you might well feel justified in computing A-1 so that you can use the more informative (2.26) and (2.27) to assess the effects of perturbations to the data.

EXERCISES

2.7

2.8

For the linear system in Exercise 1.1, let

In exact arithmetic, calculate the residuals r = b - A y and s = b - Az. Does the better approximation have

the smaller residual? 2.9

Consider the system

x1 + x2 = 2

10x1 + 10

18

x2 = 10 + 10

18

Do not use more than 15-digit decimal arithmetic in the computations of parts (a) and (b). This will, for example, result in 10 + 1018 becoming just 10. (a) Solve using Gaussian elimination with partial piv- oting.

(b) Divide each row by its largest |aij| and then use

Gaussian elimination with partial pivoting. 2.10 (c) Solve by hand using any method and exact arith- metic.

(d) Use exact arithmetic to calculate residuals for each solution. Which method seems better [compare with part (c)]? Do the residuals indicate this? (e) Using the formula (2.21), compute A-1 for this

system, and cond(A).

Assume that the computed solution to a nonsingular linear system is

(-10.4631, 0.00318429, 3.79144, -0.000422790) and the condition number is 1200.

(a) What is the uncertainty (±?) in each component of the computed solution? Assume exact data and a unit roundoff of 10-6.

(b) Repeat (a) but for the case when and are each 10-5.

On a machine with unit roundoff 10-l7 with b exact

but how large a condition num-

ber can we tolerate if we want

2.4

ROUTINES FACTOR AND SOLVE

In this section we describe routines Factor and Solve that can be used to solve the system Ax = b. Routine Factor performs Gaussian elimination with partial pivoting on the matrix A and estimates its condition number. It saves the multipliers and the pivot information to be used by Solve to obtain the solution x for any right-hand side b. A typical call to Factor in FORTRAN is

CALL FACTOR (A,MAXROW,NEQ,COND,PVTIDX,FLAG,TEMP) while a typical function evaluation of Factor is

in the C++ version. The parameter cond must be passed by reference since its value is set by the function Factor; in C the address of cond must be explicitly passed so that its call looks like

flag = Factor(a, neq, &cond, pivot_index);

Input variables in FORTRAN are A, the array containing the coefficient matrix A; MAXROW, the declared row dimension of A in the program that calls Factor; and NEQ, the number of equations to be solved. Output variables are A, containing the upper triangular matrix U in positions aij, i < j, and the lower triangular matrix L in positions aij, i > j (as long as FLAG is zero); FLAG, an integer variable that indicates whether or not zero pivots were encountered; COND, an estimate of the condition number of A in the maximum norm; PVTIDX, an array that records the row inter- changes; and in FORTRAN 77 we need TEMP, an array used for temporary storage.

In the C and C++ versions corresponding variables are a for A, neq for NEQ, cond for COND, and pivotindex for PVTIDX. Note that in the C and C++ versions, (1) instead of declaring the matrix A to have two indices, we use a pointer to a vector consisting of the rows of A; (2) there is no need to reserve space for the temporary index TEMP because this allocation can be made dynamically when needed (as can be done for a and for pivotindex); (3) the output flag is the return variable for the function Factor. Because arrays are typically indexed starting with zero in C and C++, we have modified the algorithm accordingly. Some further comments are in order about the variables for Factor

MAXROW and NEQ (or neq). If, for example, the array A is declared as A(10,10) in the calling program and we are solving three equations in three unknowns, then MAXROW would have the value 10 in the FORTRAN version and NEQ (or neq in the C version) the value 3. COND. COND is a lower bound for cond(A) in the maximum norm and is often a good approximation to cond(A). If fl(COND + 1) = COND, the matrix A is singular to working precision. Because we are working with arithmetic of limited precision, exact singularity is difficult to detect. In particular, the occurrence of a zero pivot does not necessarily mean that the matrix is singular nor does a singular matrix necessarily produce a zero pivot (see Exercise 2.16). When FLAG is nonzero, the output COND is meaningless.

PVTIDX (or pivotindex). When the elimination process has been com- pleted, the kth component of PVTIDX (pivotindex) is the index of the kth pivot row and the nth component is set to (- 1)” where m is the number of interchanges. Computation of the determinant of A requires PVTIDX(NEQ) in FORTRAN or pivot_index[neq-l] in C and C++ (see Exercise 2.23).

The argument list for Solve is

2.4 ROUTINES FACTOR AND SOLVE

in FORTRAN; in C and C++ it is

Solve(a,neq,pivotindex,b);

63

The variables A, MAXROW, NEQ, PVTIDX are as specified in the Factor list. Routine Solve uses the arrays A and PVTIDX as output from Factor and the right-hand side contained in the vector B to solve Ax = b. The solution x is returned in B.

Example 2.14. To illustrate the codes, we solve the problem

for two right sides b = (39, 3, 2)T and b = (6, 7, - 12)T. The main program sets up the problem and calls the routines Factor/Solve to obtain a solution. Note that after the call to Factor, the variable FLAG is checked to see if any pivots are zero. If FLAG > 0 and Solve were used, a zero divide error would result. When FLAG = 0 the routine Solve is used once for each right-hand side to obtain the solutions. Note that Factor is not (and should not be) used twice since it does not act on b. The output is as follows (the floating point values may vary slightly from one computer to another).

C o n d i t i o n n u m b e r = 106.642857142857100 Solution of the first system

2.000000000000000 1.000000000000000 3.000000000000000

Solution of the second system

76.75000000000000 -31.00000000000000 - 4 . 2 5 0 0 0 0 0 0 0 0 0 0 0 0 0

n

EXERCISES

2.11 Solve the system in Exercise 2.2 using Factor/Solve. Compare to the true solution.

2.12 Solve the system in Exercise 2.3 using Factor/Solve. Compute the residual.

2.13 The codes Factor/Solve can be used to find the ele- ments of A-1. The inverse is used to estimate certain

statistical parameters. It is also used to study the sen- sitivity of the solution to errors in the data. Let xi denote the solution of

Ax=bi, i = 1, 2 ,..., n,

where the ith right-hand side bi is

If we form the matrix

it is easy to show that X = A-1. Do so. Use Fac-

tor/Solve to find the inverse of the matrix

2.14 Consider the linear system of Exercise 2.4.

(a) Using the method of Exercise 2.13, find the in- verse of the original linear system. Calculate the exact

condition number.

(b) Use Factor/Solve on the system in Exercise 2.4b. What is COND? Is the condition number inequal- ity (2.22) valid for this problem? (Use

0.003333.)

2.15 Suppose we are given the electrical network shown in Figure 2.1, and we desire to find the potentials at junctions (1) through (6). The potential applied be- tween A and B is V volts. Denoting; the potentials

(d) Compute the residuals for(b) and for (c). Do they give any indication of singularity?

2.17 In analyzing environmental samples taken from the atmosphere, a simple model with m samples and n sources and chemicals produces AX = B, where ajk

is the average concentration of element i from source

k, xki is the mass of particles from source k contribut-

ing to sample j, bij is the concentration of element i

in sample j, and 1 < i < n, 1 < k < n, 1 < j < m. If by v1, v2, ..., v6, application of Ohm’s law and Kirch-

hoff’s current law yield the following set of linear equations for the vi:

11v1 - 5v2 - v6 = 5V m = 4, n = 3, then -20v1 + 41v2 - 15v3 - 6v5 = 0 -3v2 + 7v3 - 4v4 = 0 -v3 + 2v4 - v5 = 0 -3v2 - 10v4 + 28v5 - 15v6 = 0 -2v1 - 15v5 + 47v6 = 0. Solve when V = 50. 2.16 The system 0.473 x1 - 0.115x2 = bl 0.731x1 -0.391x2 + 0.267x3 = b2 -0.782 x2 + 0.979x3 = b3 is singular.

(a) Apply Factor to the coefficient matrix. What is the smallest pivot? Is it near the unit roundoff u? Is

(a) What is X? What does COND tell you about the reliability of this result? First assume exact data, then that the entries of A and B are rounded to the displayed values.

(b) Use the method of Exercise 2.13 to compute A-1.

What is the exact cond(A)?

(c) What does (2.24) tell you about the sensitivity of

x21 to changes in b11 ? Replace b11 by 1.43 and re-

calculate x21. Do the numerical answers confirm the

theory? Here you are to consider relative changes to the data and the solution.

it near underflow? Is COND large? The results you 2.18 Consider the linear system obtain will depend on the hardware and software that

you use. If Factor turns up a pivot that is exactly zero, perturb the coefficient 0.979 by a very small amount so as to get a system that is computationally nonsin-

gular for the rest of this problem. Adding 10-l4 to the (a) Solve for x using Factor/Solve. coefficient will suffice for a number of configurations.

(b) Use Factor/Solve to compute x for b = (0.084,0.357,0.833). Is there any indication of sin- gularity in the answer?

(c) Use Factor/Solve to compute x for b = (0.566,0.404,0.178). Is there any indication of sin- gularity in the answer?

(b) If each entry in A and b might have an error of ±0.0005, how reliable is x?

(c) Make arbitrary changes of ±0.0005 in the ele- ments of A to get A + AA and in the elements of b to

get b + Solve to get

x + Calculate Is this consistent with (b)? What is the relative change in each xi?