Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Math 3607: Homework 3

2021

1.  (Roots of unity revisited:  adapted from LM 3.1–24)  In Problem 2 of Homework 1, we calculated all five roots of x5 ` 1, but it required a number of lines of code. In this problem, we will do this more compactly for a more general xn ` 1. Recall that

´1  eπi  e3πi  e5πi  “ ¨ ¨ ¨ ,

but, if we take the nth root, we obtain n distinct values

eπi{n, e3πi{n, e5πi{n , . . . ,

due to the periodicity of the complex exponentials; think Euler.

(a) Write a script which, given a positive integer n, finds all n roots of xn ` 1 at once, using ONE statement. It must also print out all n of these roots neatly using either disp or fprintf in tabular form. A loop may be used for printing results, but is not allowed

in the calculation of the roots.

(b) Run the script with n “ 3, 5, 7, and 11.

Note.  Print out the contents of your script m-file using type.

2.  (Catastrophic cancellation; LM 9.3– 10) Consider the function

f pxq  $&    if x  0

 

Here we explore the catastrophic cancellation which occurs as x Ñ 0 since ex Ñ 1 as x Ñ 0.

(a)  Use the Taylor series expansion of ex  to prove that f is continuous at 0.

(b)  Now calculate f pxq numerically for x  “ 10´k  where k P Nr1, 20s in three slightly different ways:

i. Calculate f pxq as written.

f1 pxq “  ,    for x ‰ 0.

(You and I know that analytically f1 pxq f pxq for all nonzero x – but MATLAB doesn’t.)

iii. MATLAB has a function which analytically subtracts 1 from the exponential to avoid catastrophic cancellation before the result is calculated numerically.  So define the function f2 pxq to be the same as f pxq except that ex ´ 1 is replaced by expm1(x).

Tabulate the results using disp or fprintf. The table should have four columns with the first being x, the second using f pxq, the third using f1 pxq, and the fourth using f2 pxq, with all shown to full accuracy. Do it as efficiently as you can, without using a loop.

Note. Write your code for this part in a single code block. No external script needs to be written.

(c)  Comment on the results obtained in the previous part. Explain why certain methods work well while others do not.

3.  (Improved triangular substitutions; adapted from FNC 2.3.5)  If B P Rnˆp  has columns b1 , . . . , bp , then we can pose p linear systems at once by writing AX “ B , where X P Rnˆp whose jth column xj  solves Axj  “ bj  for j “ 1, . . . , p:

»                          fi      »                          fi

x1    x2     ¨ ¨ ¨   xp  ffi “  b1    b2     ¨ ¨ ¨   bp  ffi .

 

“X                                              “B

(a) Modify backsub .m and forelim .m from lecture so that they solve the case where the second input is an n ˆ p matrix, for p ě 1. Include the programs at the end of your live script.

(b) If AX  “ I , then X  “ A´1 .  Use this fact to write a MATLAB function ltinverse that uses your modified forelim to compute the inverse of a lower triangular matrix. Include the program at the end of your live script.  Then test your function using the following matrices, that is, compare your numerical solutions against the given exact solutions.

 

L1  »—8(2) 4

L2  

 

0

´7

9

0fi

0 ffi

´27fl ,

 

0

1

1

3

0

0

0

1

1

3

0fi

0 ffi ffi

0 ffi , 1fl

 

 

L 1(´)1     7

 189

»

L2(´)1   ´  ´

0

´ 

´ 

0

1

´ 

1

9

0fi

0 ffi

´  fl   0   0fi

0   0 ffi

ffi

1   0 ffi ´    1fl


4.  (Vectorizing mylu .m; FNC 2.4.7) Below is an instructional version of LU factorization code presented in lecture.


function   [L ,U ]  =  mylu (A )

%  MYLU       LU  factorization   (demo  only--not  stable !) .

%  Input :

%      A         square  matrix

%  Output :

%       L ,U    unit  lower  triangular  and  upper  triangular  such  that  LU=A

n  =  length (A );

L  =  eye (n);       %  ones  on  diagonal

%  Gaussian  elimination

for  j  =  1:n- 1

for  i  =  j+1:n

L (i , j )  =  A (i , j)  /  A (j , j);       %  row  multiplier

A (i , j :n)  =  A (i , j :n)  -  L (i , j ) *A (j , j :n);

end

end

U  =  triu (A );

end

Consider the innermost loop.   Since the different iterations in i are all independent, it is possible to vectorize this group of operations, that is, rewrite it without a loop. In fact, the necessary changes are to delete the keyword for in the inner loop, and delete the following end line.   (You should also put a semicolon at the end of  i  =  j+1:n to suppress extra output.)

(a)  Make the changes as directed and verify that the function works properly.

(b)      Write out symbolically (i. e., using ordinary elementwise vector and matrix notation) what the new version of the function does in the case n “ 5 for the iteration with j “ 3.

5.  (Application of LU factorization: FNC 2.4.6) When computing the determinant of a matrix by hand, it is common to use cofactor expansion and apply the definition recursively.  But this is terribly inefficient as a function of the matrix size.

(a)  Explain why, if A “ LU is an LU factorization,

n

detpAq  u11u22 ¨ ¨ ¨ unn  ź uii .

This part is an analytical question. Do it by hand.

(b)  Using the result of part (a), write a MATLAB function determinant that computes the determinant of a given matrix A using mylu from lecture.  Include the function at the end of your live script.  Use your function and the built-in det on the matrices magic(n) for n “ 3, 4, . . . , 7, and make a table (using disp or fprintf) showing n, the value from your function, and the relative error when compared to det.