Solution: Assignment 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Assignment 2

(Be sure to observe the rules about handing in homework)

x1  x 2  x3  3
1. Solve: 6 x1  2 x 2  2 x3  2
 3x1  4 x 2  x3  1

with (a) (10 pts) naive Gauss elimination, (b) (15 pts) Gauss with partial pivoting

*You need to show all of the steps manually. MATLAB code alone is not acceptable.

Solution
(a) The system is first expressed as an augmented matrix:

 1 1 1 3
 
 6 2 2 2
 3 4 1 1 

Forward elimination:

a21 is eliminated by multiplying row 1 by 6/1 = 6 and subtracting the result from row 2. a31 is
eliminated by multiplying row 1 by –3/1 = –3 and subtracting the result from row 3.

1 1 1 3 
 
0  4 8 20 
0 7  2  8

a32 is eliminated by multiplying row 2 by 7/(–4) = –1.75 and subtracting the result from row 3.

1 1 1 3
 
0  4 8 20
0 0 12 27

Back substitution:

27
x3   2.25
12

20  8(2.25)
x2   0.5
4

3  (1)(2.25)  1(0.5)
x1   0.25
1
(a) The system is first expressed as an augmented matrix:

 1 1 1 3
 
 6 2 2 2
 3 4 1 1 

Forward elimination: First, we pivot by switching rows 1 and 2:


6 2 2 2
 
 1 1  1  3
 3 4 1 1 

Multiply row 1 by 1/6 = 0.16667 and subtract from row 2 to eliminate a21. Multiply row 1 by –3/6 =
–0.5 and subtract from row 3 to eliminate a31.

6 2 2 2 
 
 0 0.66667  1.33333  3.33333
0 5 2 2 

Pivot:

6 2 2 2 
 
 0 5 2 2 
0 0.66667  1.33333  3.33333

Multiply row 2 by 0.66667/5 = 0.133333 and subtract from row 3 to eliminate a32.

6 2 2 2 
 
 0 5 2 2 
0 0  1.6  3.6

Back substitution:

3.6
x3   2.25
1.6

2  2(2.25)
x2   0.5
5

2  2(2.25)  2(0.5)
x1   0.25
6
2. (a) (10 pts) Solve the following system of equations by LU decomposition without pivoting.
Show explicitly the L and U matrices found during the process.

8 x1  4 x 2  x3  11
 2 x1  3x 2  x3  4
2 x1  x 2  6 x3  7

(b) (15 pts) Determine the matrix inverse in three steps (column by column) as taught in class.
Check your results by verifying that [A][A]-1 = [I]. You may use MATLAB to verify your results.

Solution
(a) The coefficient a21 is eliminated by multiplying row 1 by f21 = –2/8 = –0.25 and subtracting the
result from row 2. a31 is eliminated by multiplying row 1 by f31 = 2/8 = 0.25 and subtracting the
result from row 3. The factors f21 and f31 can be stored in a21 and a31.
 8 4 1 
 0.25 4 .75 

 0.25  2 6.25

a32 is eliminated by multiplying row 2 by f32 = –2/4 = –0.5 and subtracting the result from row 3.
The factor f32 can be stored in a32.
 8 4 1 
 0.25 4 .75 

 0.25  0.5 6.625

Therefore, the LU decomposition is


 1 0 0 8 4 1 

[L]   0.25 1 0 [U ]  0 4 .75 

 0.25  0.5 1 0 0 6.625
Forward substitution: [L]{d} = {b}

 1 0 0  d1  11
 0.25    
 1 0 d 2    4 
   
 0.25  0.5 1 d 3   7 

Solving yields d1 = 11, d2 = 6.75, and d3 = 7.625

Back substitution:

8 4  1   x1   11 
0 4 0.75   x    6.75 
  2   
0 0 6.625   7.625
 3 
x 

7.625 6.75  0.75(1.1509) 11  4(1.4717)  1(1.1509)


x3   1.1509 x2   1.4717 x1   0.783
6.625 4 8
(b) The first column of the inverse can be computed by using [L]{d} = {b}

 1 0 0  d1  1
 0.25    
 1 0 d 2   0
 0.25  0.5 1    
d3  0

This can be solved for d1 = 1, d2 = 0.25, and d3 = -0.125. Then, we can implement back substitution

8 4  1   x1   1 
0 4 0.75   x    0.25 
  2   
0 0 6.625  x3   0.125
  

to yield the first column of the inverse


 0.0896 
 
{X 1}   0.0660 
- 0.0189
 
For the second column use {b}T = {0 1 0} which gives {d}T = {0 1 0.5}.
Back substitution then gives {X2}T = {0.1085 0.2358 0.0755}.

For the third column use {b}T = {0 0 1} which gives {d}T = {0 0 1}.
Back substitution then gives {X3}T = { 0.0330 0.0283 0.1509}.

Therefore, the matrix inverse is


 0.0896  0.1085 0.0330 
[ A]   0.0660
1
0.2358  0.0283
 0.0189 0.0755 0.1509 

We can verify that this is correct by multiplying [A][A]–1 (in MATLAB) to yield the identity matrix.

>> A=[8,4,-1;-2,3,1;2,-1,6]

A =
8 4 -1
-2 3 1
2 -1 6

>> AI = inv(A)

AI =
0.0896 -0.1085 0.0330
0.0660 0.2358 -0.0283
-0.0189 0.0755 0.1509

>> A*AI
ans =
1.0000 -0.0000 0
0.0000 1.0000 -0.0000
-0.0000 0 1.0000
3. Use least-squares regression to fit a straight line to

x 1 2 3 4 5 6 7 8 9
y 1 1.5 2 3 4 5 8 10 13

(a) (10 pts) Along with the slope and intercept, compute Sr, St, and the correlation coefficient r as
described in lecture slides (slide#9 in Chap-17e.ppt).
Plot the data and the straight line. Assess the fit.

(b) (10 pts) Recompute (a), but use polynomial regression to fit a parabola to the data. Compare
the results with those of (a) using the correlation coefficient r.
Which one (a line or a parabola?) does provide a better fit?

Solution:
(* Details are in the sumplemental excel file *)

(a) The results can be summarized as y  2.0139  1.4583x (r  0.956)


16
y = 1.4583x - 2.0139
12 R2 = 0.9144

0
0 2 4 6 8

Although the correlation coefficient appears to be close to 1, the straight line does not
describe the data as well as a parabola (as will be seen in part (b)).

(b) The results can be summarized as

y  1.4881  0.45184 x  0.191x 2 (r  0.997)

Comparison of the correlation coefficients (line vs. parabola) indicates that the quadratic fit
does a much better job of fitting the data.
16
y = 0.191x2 - 0.4518x + 1.4881
12 R2 = 0.9949

0
0 2 4 6 8
4. Given the data

x 5 10 15 20 25 30 35 40 45 50
y 17 24 31 33 37 37 40 40 42 41

use least-squares regression to fit


(a) (10 pts) a power equation, and
(b) (10 pts) a saturation-growth-rate equation.
Plot the data along with all the curves. Is any one of the curves superior? If so, justify by using the
correlation coefficient r.
(* Note that correlation coefficient r must be calculated using the original data and the
fitted non-linear function. In other words, do not use the transformed values for the
calculation of the correlation coefficient *)

(*Important Note: Do not use package programs to find the answers. However, you may use the
MATLAB programs to check the correctness of your answers.*)

Solution:

(a) Power equation: We regress log10y versus log10x to give log10 y  0.998  0.3851log10 x

Therefore, 2 = 100.998 = 9.953 and 2 = 0.3851, and the power model is


y  9.953x 0.3851

The model and the data can be plotted as


50
40
30
20 y = 9.9529x0.3851
10 R2 = 0.9553
0
0 10 20 30 40 50 60

(b) Saturation-growth-rate: We regress 1/y versus 1/x to give

1 1
 0.02  0.1974
y x

Therefore, 3 = 1/0.01996 = 50.092 and 3 = 0.1974*(50.092) = 9.89, and the saturation-


growth-rate model is

x
y  50.092
9.89  x
The model and the data can be plotted as

50
40
30 x
y = 50.092
20 x + 9.891369
10 R2 = 0.98919
0
0 10 20 30 40 50 60

Comparison of fits: The linear fit is obviously inadequate. Although the power fit follows
the general trend of the data, it is also inadequate because (1) the residuals do not appear to
be randomly distributed around the best fit line and (2) it has a lower r2 than the saturation
model.

Using the r values, we can conclude that the saturation model represents the best fit.

5. (15 pts) Given the data

x 1 2 3 5 7 8
f (x) 3 6 19 99 291 444

Calculate f(4) using Newton’s interpolating polynomials of order 1 through 4, i.e. you will calculate
f1(4), f2(4), f3(4), and f4(4). In each case, choose your base points to attain good accuracy (Hint:
around the point of interest, i.e. 4). What do your results indicate regarding the order of the
polynomial used to generate the data in the table?

Solution:
First, order the points so that they are as close to and as centered about the unknown as possible

x0 = 3 f(x0) = 19
x1 = 5 f(x1) = 99
x2 = 2 f(x2) = 6
x3 = 7 f(x3) = 291
x4 = 1 f(x4) = 3

Next, the divided differences can be computed and displayed in the format of Fig. 18.5,

i xi f(xi) f[xi+1,xi] f[xi+2,xi+1,xi] f[xi+3,xi+2,xi+1,xi] f[xi+4,xi+3,xi+2,xi+1,xi]


0 3 19 40 9 1 0
1 5 99 31 13 1
2 2 6 57 9
3 7 291 48
4 1 3
The first through fourth-order interpolations can then be implemented as

f1 (4)  19  40(4  3)  59
f 2 (4)  59  9(4  3)(4  5)  50
f 3 (4)  50  1(4  3)(4  5)(4  2)  48
f 4 (4)  48  0(4  3)(4  5)(4  2)(4  7)  48

Clearly this data was generated with a cubic polynomial since the difference between the 4th
and the 3rd-order versions is zero.

6. (15 pts) Repeat the question above (Q.5) using Lagrange polynomials of order 1 through 3.

Solution:
First order:
x0 = 3 f(x0) = 19
x1 = 5 f(x1) = 99
45 43
f1 (10)  19  99  59
35 53

Second order:
x0 = 3 f(x0) = 19
x1 = 5 f(x1) = 99
x2 = 2 f(x2) = 6
(4  5)(4  2) (4  3)(4  2) (4  3)(4  5)
f 2 (10)  19  99  6  50
(3  5)(3  2) (5  3)(5  2) (2  3)(2  5)

Third order:
x0 = 3 f(x0) = 19
x1 = 5 f(x1) = 99
x2 = 2 f(x2) = 6
x3 = 7 f(x3) = 291
(4  5)(4  2)(4  7) (4  3)(4  2)(4  7)
f 3 (10)  19  99
(3  5)(3  2)(3  7) (5  3)(5  2)(5  7)
(4  3)(4  5)(4  7) (4  3)(4  5)(4  2)
 6 291  48
(2  3)(2  5)(2  7) (7  3)(7  5)(7  2)

You might also like