Вы находитесь на странице: 1из 5

CMPUT 340Introduction to Numerical Methods

Assignment 1
Fall 2011 Department of Computing Science University of Alberta Due: Friday, October 7 at 23:59:59 local time Worth: 12.5% of nal grade Instructor: Nilanjan Ray, Ath406, nray1@ualberta.ca Note: This assignment is to be submitted electronically by using moodle site (https: //eclass.srv.ualberta.ca/course/view.php?id=337). It needs to be compressed in a single zipped folder. Matlab functions need to be submitted in .m les, such as SolveDiag.m (all of the Matlab functions required are discussed below) so that we can verify them.

Part 1 (Approximation and error analysis40%)


(a) (7%) Exercise 1.4 on Page 42. (b) (7%) Exercise 1.6 on Page 42. (c) (7%) Exercise 1.10 on Page 43. (d) (7%) Exercise 1.12 on Page 43. (e) (12%) Computer Exercise 1.9 on Page 46. Submit a Matlab function [y] = myexp(x) in a le myexp.m. Remember to submit all written answers in the le A1Answers.pdf. A note on Matlab programming style Matlab is a vector and matrix based programming environment. Matrix and vector operations are built in and heavily optimized. As much as possible you must avoid the use of for loops. Most for loops can be replaced by vectorizing the programs you write. You will lose marks by simply implementing the nested loops given in the textbook. Also, functions in help elmat can be used below, but not those in help matfun. (In particular, you cannot use the slash operator, which is listed in help matfun. Tip: consult help elmat for some really useful for-loop-avoiding functions.) 1

Part 2 (Solving simple linear constraint systems20%)


(a) (6%) Write a Matlab function [x,flag] = SolveDiag(d,y) which computes a vector x that solves the linear system Dx = y, where D is a diagonal matrix whose diagonal vector is d. If there is no solution return flag=-1; if there is one solution return flag=0; if there are innitely many solutions return flag=1. Submit the function in a le called SolveDiag.m. (b) (7%) Write a Matlab function [x,flag] = SolveUpperTri(U,y) which computes a vector x that solves the linear system U x = y, where U is an upper triangular matrix. (See Page 66 in the text.) If there is no solution return flag=-1; if there is one solution return flag=0; if there are innitely many solutions return flag=1. Submit the function in a le called SolveUpperTri.m. (c) (7%) Write a Matlab function [x,flag] = SolveLowerTri(L,y) which computes a vector x that solves the linear system Lx = y, where L is a lower triangular matrix. (See Page 65 in the text.) If there is no solution return flag=-1; if there is one solution return flag=0; if there are innitely many solutions return flag=1. Submit the function in a le called SolveLowerTri.m.

Part 3 (Solving general linear constraint systems27%)


(a) (13%) Write a Matlab function [L,U,flag] = LUfactor(A) which computes a lower triangular matrix L and an upper triangular matrix U such that A = LU . (See Page 69 in the text.) You do not need to employ any sort of pivoting. Return flag=0 if your LU factorization succeeds; flag=1 if it fails. Submit the function in a le called LUfactor.m. (b) (7%) Write a Matlab function [x,flag] = SolveGeneralLU(A,y) which uses LU factorization, forward subtitution, and back substitution to compute a solution vector x such that Ax = y. If your function computes a valid solution return flag=0; if not return flag=1. Submit the function in a le called SolveGeneralLU.m. (c) (7%) Write a Matlab function [x,flag] = SolveGeneralSOR(A,y,x0,beta,TOL) which uses Successive Over-Relaxation iteration, starting at x(0) , to compute a solution vector x such that Ax = y. Successive Over-Relaxation iteration is described on Page 471 of the text. (Note that the parameter used here and in the course notes corresponds to 1/ used in the text.) This is an iterative solution technique where the kth iterate, x(k) , is given by the solution to (D + L)x(k) = y [U + (1 )D]x(k1) where D = diag(diag(A)), L=tril(A)-D, and U=triu(A)-D. (See help elmat for a description of these functions.) Stop the iteration when the norm of the residual vector r = y Ax(k) is less than T OL. Also remember that for convegence needs to be set 2

to > 1/2. If your function computes a valid solution return flag=0; if divergence is detected return flag=1. Submit the function in a le called SolveGeneralSOR.m.

Part 4 (Comparing solution techniques13%)


You now have three dierent functions available for solving a system of linear equality constraints: SolveGeneralLU, SolveGeneralSOR, and slash (Matlabs built in solver). Compare the speed and accuracy of each of these solvers, including a range of values for > 1/2 for SolveGeneralSOR. Give an example of a linear system that makes each solver look good and bad relative to the rest (if possible). Be sure to submit your answers in the le A1Answers.pdf.

Bonus! (Alternative solution techniques13%)


Implement the following solution methods and include them in the comparison of Part 4. (a) (6.5%) Write a Matlab function [L,U,P] = LUfactorPPivot(A) which computes a lower triangular matrix L, an upper triangular matrix U , and a permutation matrix P such that P A = LU using partial pivoting described on Page 73 of the text. Use this procedure to implement a Matlab function [x,flag] = SolveGeneralLUPP(A,y). If there is no solution return flag=-1; if there is one solution return flag=0; if there are innitely many solutions return flag=1. (b) (6.5%) Write a Matlab function [x,flag] = SolvePosSemiDefCG(A,y,x0,TOL) which uses conjugate gradient iteration, starting at x(0) , to compute a solution vector x such that Ax = y, where A is positive semi-denite. Conjugate gradient iteration is described on Page 473 of the text. If your function computes a valid solution return flag=0; if divergence is detected return flag=1.

Appendix: Exercise descriptions from the textbook


Exercise 1.4. (Page 42) Consider the problem of evaluating the function sin(x), in particular, the propagated data error, i.e., the error in the function value due to a perturbation h in the argument x. (a) Estimate the absolute error in evaluating sin(x). (b) Estimate the relative error in evaluating sin(x). (c) Estimate the condition number for this problem. (d) For what values of the argument x is this problem highly sensitive? Exercise 1.6. (Page 42) The sine function is given by the innite series x3 x 5 x7 + + . 3! 5! 7! (a) What are the forward and backward errors if we approximate the sine function by using only the rst term in the series, i.e., sin(x) x, for x = 0.1, 0.5, and 1.0? sin(x) = x (b) What are the forward and backward errors if we approximate the sine function by using the rst two terms in the series, i.e., sin(x) x x3 /6, for x = 0.1, 0.5, and 1.0? Exercise 1.10. (Page 43) Consider the expression 1 1 1x 1+x assuming x = 1. (a) For what range of values x is it dicult to compute this expression accurately in oating-point arithmetic? (b) Give a rearrangement of the terms such that, for the range of x in part a, the computation is more accurate in oating-point arithmentic. Exercise 1.12. (Page 43) (a) Which of the two mathematically equivalent expressions x2 y 2 and (x y)(x + y)

can be evaluated more accurately in oating-point arithmetic? Why? (b) For what values of x and y, relative to each other, is there a substantial dierence in the accuracy of the two expressions? 4

Computer Problem 1.9. (Page 46) (a) Write a program to compute the exponential function ex using the innite series ex = 1 + x + x 2 x3 + + . 2! 3!

(b) Summing in the natural order, what stopping criterion should you use? (c) Test your program for x = 1, 5, 10, 15, 20, and compare your results with the built-in function exp(x). (d) Can you use the series in this form to get accurate results for x < 0? (Hint: ex = 1/ex .) (e) Can you rearrange the series or regroup the terms in any way to get more accurate results for x < 0?

Вам также может понравиться