You are on page 1of 5

# CMPUT 340—Introduction 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 analysis—40%)
(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

) If there is no solution return flag=-1.flag] = SolveGeneralSOR(A.y) which computes a vector x that solves the linear system U x = y. (See help elmat for a description of these functions. where D is a diagonal matrix whose diagonal vector is d. Submit the function in a ﬁle called “SolveUpperTri. Submit the function in a ﬁle called “SolveLowerTri. if not return flag=1. (See Page 69 in the text. x(k) . Part 3 (Solving general linear constraint systems—27%) (a) (13%) Write a Matlab function [L.) This is an iterative solution technique where the kth iterate. Submit the function in a ﬁle called “LUfactor. Submit the function in a ﬁle called “SolveDiag.TOL) which uses Successive Over-Relaxation iteration. Also remember that for convegence β needs to be set 2 .) If there is no solution return flag=-1.beta. if there is one solution return flag=0.flag] = SolveDiag(d. (See Page 65 in the text. if there is one solution return flag=0. (c) (7%) Write a Matlab function [x. starting at x(0) .y) which uses LU factorization. where L is a lower triangular matrix. If there is no solution return flag=-1. (b) (7%) Write a Matlab function [x. and U=triu(A)-D.m”. where U is an upper triangular matrix. (Note that the parameter β used here and in the course notes corresponds to 1/ω used in the text. to compute a solution vector x such that Ax = y. Submit the function in a ﬁle called “SolveGeneralLU. (See Page 66 in the text. if there are inﬁnitely many solutions return flag=1.U. Return flag=0 if your LU factorization succeeds. (b) (7%) Write a Matlab function [x.flag] = SolveLowerTri(L.y) which computes a vector x that solves the linear system Dx = y. forward subtitution.x0. if there are inﬁnitely many solutions return flag=1.flag] = SolveUpperTri(U.m”.m”.m”. If your function computes a valid solution return flag=0. if there are inﬁnitely many solutions return flag=1.flag] = LUfactor(A) which computes a lower triangular matrix L and an upper triangular matrix U such that A = LU . and back substitution to compute a solution vector x such that Ax = y.y. if there is one solution return flag=0.m”.Part 2 (Solving simple linear constraint systems—20%) (a) (6%) Write a Matlab function [x. is given by the solution to (βD + L)x(k) = y − [U + (1 − β)D]x(k−1) where D = diag(diag(A)).flag] = SolveGeneralLU(A. L=tril(A)-D.) You do not need to employ any sort of pivoting. Successive Over-Relaxation iteration is described on Page 471 of the text. (c) (7%) Write a Matlab function [x.) Stop the iteration when the norm of the residual vector r = y − Ax(k) is less than T OL.y) which computes a vector x that solves the linear system Lx = y. flag=1 if it fails.

to β > 1/2. (b) (6. Give an example of a linear system that makes each solver look good and bad relative to the rest (if possible). to compute a solution vector x such that Ax = y. if there are inﬁnitely many solutions return flag=1.5%) Write a Matlab function [x. Bonus! (Alternative solution techniques—13%) Implement the following solution methods and include them in the comparison of Part 4.P] = LUfactorPPivot(A) which computes a lower triangular matrix L. Submit the function in a ﬁle called “SolveGeneralSOR. if there is one solution return flag=0. (a) (6. an upper triangular matrix U .flag] = SolvePosSemiDefCG(A. and slash (Matlab’s built in solver). starting at x(0) . 3 . where A is positive semi-deﬁnite.y). if divergence is detected return flag=1.y. SolveGeneralSOR. Use this procedure to implement a Matlab function [x. If your function computes a valid solution return flag=0. Part 4 (Comparing solution techniques—13%) You now have three diﬀerent functions available for solving a system of linear equality constraints: SolveGeneralLU. if divergence is detected return flag=1. including a range of values for β > 1/2 for SolveGeneralSOR. and a permutation matrix P such that P A = LU using partial pivoting described on Page 73 of the text.U. If your function computes a valid solution return flag=0.TOL) which uses conjugate gradient iteration.flag] = SolveGeneralLUPP(A.x0.pdf”. Be sure to submit your answers in the ﬁle “A1Answers. If there is no solution return flag=-1.m”. Compare the speed and accuracy of each of these solvers.5%) Write a Matlab function [L. Conjugate gradient iteration is described on Page 473 of the text.

Exercise 1. the propagated data error.. sin(x) ≈ x − x3 /6.4. (d) For what values of the argument x is this problem highly sensitive? Exercise 1. and 1.12.5. (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.e. the computation is more accurate in ﬂoating-point arithmentic. (Page 42) The sine function is given by the inﬁnite series x3 x 5 x7 + − + ···. (Page 42) Consider the problem of evaluating the function sin(x)..e. for x = 0. (a) Estimate the absolute error in evaluating sin(x). for x = 0. is there a substantial diﬀerence in the accuracy of the two expressions? 4 . (b) Estimate the relative error in evaluating sin(x).e. 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.Appendix: Exercise descriptions from the textbook Exercise 1. 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. 0. for the range of x in part a.0? Exercise 1.6.10.5. i..1. (a) For what range of values x is it diﬃcult to compute this expression accurately in ﬂoating-point arithmetic? (b) Give a rearrangement of the terms such that. the error in the function value due to a perturbation h in the argument x. i. (Page 43) Consider the expression 1 1 − 1−x 1+x assuming x = ±1. i.1. 0. in particular. relative to each other. (c) Estimate the condition number for this problem. sin(x) ≈ x.

(d) Can you use the series in this form to get accurate results for x < 0? (Hint: e−x = 1/ex .9. 2! 3! (b) Summing in the natural order.Computer Problem 1. ±20. ±10.) (e) Can you rearrange the series or regroup the terms in any way to get more accurate results for x < 0? 5 . ±15. and compare your results with the built-in function exp(x). what stopping criterion should you use? (c) Test your program for x = ±1. ±5. (Page 46) (a) Write a program to compute the exponential function ex using the inﬁnite series ex = 1 + x + x 2 x3 + + ···.