Math 551 Homework Assignment 3 Page 1 of 6 Name and section: ID number: E-mail: 1. Consider Newton s method for finding + α with α > 0 by finding the positive root of f(x) = x 2 α = 0. Assuming that x 0 > 0, show the following: (a) x k+1 = 1 2 (x k + αxk ). (b) ( ) x x 2 2 2 k+1 α = k α, for k 0 and therefore x k > α for k 1. Solution: (a) Let us apply Newton s method to f(x). In particular, we have that x k+1 = x k f(x k) f (x k ) = x k x2 k α = x2 k + α x k+1 = 1 ) (x k + αxk. 2x x 2 (b) Starting from the left-hand-side (lhs) of part (b) and utilizing the result from part (a), we obtain x 2 k+1 α = 1 4 ( ) x 2 2 = k α. ( x k + α ) 2 α = 1 (x 2k + α2 x k 4 x 2 k ) 2α = 1 4 ( x 2 k α Therefore, if x 2 k α, then ( x 2 k α ) 2 > 0 which implies x 2 k+1 > α, or x k+1 > α for k 0. It then follows that xk > α for k 1. x k ) 2 2. Implement Newton s method in MATLAB. In particular, create a function, which utilizes the method, and store it as an m-file. Then, use your function to estimate 7 by finding the positive root of f(x) = x 2 7. Try two different initial guesses: (i)
Math 551 Homework Assignment 3 Page 2 of 6 x 0 = 2 and (ii) x 0 = 500 and consider x k+1 x k < 10 10 as a convergence criterion. Attach your codes and provide MATLAB output for both cases. Solution: An implementation of Newton s method is given in the following script named as newton.m : function [ x, k, fout ] = newton( func, dfunc, x0, atol, nmax ) On input: a) func: is the nonlinear function f(x). b) dfunc: the derivative of f(x). c) x0 : initial guess. d) atol : absolute tolerance. e) nmax : maximum number of iterations allowed. On output: a) x : is the root of f(x). b) k : number of iterations required to achieve atol. c) fout: Residuals store at each k. xk = rand; Just to start the while loop. res = xk - x0; Compute the absolute error. k = 0; Iteration index. iflag = 0; For stopping purposes. while abs(res)>atol Do-while loop. fxk = feval(func,x0); Compute the residual. fout(k+1) = fxk; Store the residual. dfxk = feval(dfunc,x0); Evaluate the derivative of f(x). xk = x0 - fxk / dfxk; Newton step. res = xk - x0; Compute the "new" absolute error. x0 = xk; Store it. k = k + 1; Increase iteration index. if(k == nmax) Check whether we reached the maximum iflag = -1; number of iterations! break; If yes, then we stop Newton. if(iflag==0) On successful exit, return the root! k = k + 1; fout(k) = feval(func,xk); xout(k) = xk; x = xk; else disp( maximum number of iterations reached! ); x = []; k = [];
Math 551 Homework Assignment 3 Page 3 of 6 Then, the main driver consists of the following code which calls newton.m : func = @(x) x^2-7; dfunc = @(x) 2 * x; x0 = 2; atol = 1e-10; nmax = 100; [ xstar, k, fout ] = newton( func, dfunc, x0, atol, nmax ); and the corresponding MATLAB output for x 0 = 2 is given below: f() 2.000000000000000-3.000000000000000e+00 2.750000000000000 0.562500000000000e+00 2.647727272727273 0.010459710743802e+00 2.645752048380804 3.901511218984410e-06 2.645751311064693 5.435651928564766e-13 2.645751311064591 8.881784197001252e-16 Similarly, for x 0 = 500: f() 5.000000000000000e+02 2.499930000000000e+05 2.500070000000000e+02 6.249650004900000e+04 1.250174996080110e+02 1.562237520823903e+04 6.253674588464200e+01 3.903844585840287e+03 3.132434003735431e+01 9.742142787757983e+02 1.577390421629556e+01 2.418160542248670e+02 8.108837568589735e+00 5.875324671377228e+01 4.486046618789610e+00 1.312461426595369e+01 3.023220284018386e+00 2.139860885700212e+00 2.669316055303705e+00 1.252482031021342e-01 2.645855325943224e+00 5.504058221266206e-04 2.645751313109127e+00 1.081866862762126e-08 2.645751311064591e+00 8.881784197001252e-16 3. For x > 0, consider the equation x + ln x = 0. Implement the secant method and find the root of the above equation. Use x 0 = 0.5, x 1 = 0.6 and x k+1 x k < 10 10 as a convergence criterion. In addition, use your
Math 551 Homework Assignment 3 Page 4 of 6 function from Question 2 employing Newton s method and repeat the calculation with same initial guess x 0 and convergence criterion as before. Finally, attach your code for the secant method and provide MATLAB outputs for both cases. Which method converges faster? Briefly explain. Solution: An implementation of secant s method is given in the following script named as secant.m : function [ x, k, fout ] = secant( func, x0, x1, atol, nmax ) On input: a) func: is the nonlinear function f(x). b) x0 : initial guess. c) x1 : >>. d) atol : absolute tolerance. e) nmax : maximum number of iterations allowed. On output: a) x : is the root of f(x). b) k : number of iterations required to achieve atol. c) fout : residuals stored at each k. k = 0; Iteration index. iflag = 0; For stopping purposes. xk = rand; Just to start the while loop. res = xk - x0; Compute the absolute error. fx0 = feval(func,x0); Evaluate the function at x_{0}. fx1 = feval(func,x1); Evaluate the function at x_{1}. while abs(res)>atol Do-while loop. fout(k+1) = fx0; Store the residual in each step. xk = x1 - fx1 * ( x1 - x0 ) / ( fx1 - fx0 ); Secant method. res = xk - x0; Compute the "new" absolute error. x0 = x1; Substitutions. x1 = xk; >>. fx0 = feval(func,x0); Evaluate the function at x_{0}. fx1 = feval(func,x1); Evaluate the function at x_{1}. k = k + 1; Increase iteration index. if(k == nmax) Check whether we reached the maximum iflag = -1; number of iterations! break; If yes, then we stop secant. if(iflag==0) On successful exit, return the root! fout(k) = feval(func,xk); xout(k) = xk; x = xk;
Math 551 Homework Assignment 3 Page 5 of 6 else disp( maximum number of iterations reached! ); x = []; k = []; Then, the main driver consists of the following code which calls secant.m : func = @(x) x+log(x); x0 = 0.5; x1 = 0.6; atol = 1.e-10; nmax = 100; [ xstar, k, fout ] = secant( func, x0, x1, atol, nmax ); and the corresponding MATLAB output follows: f() 0.500000000000000-0.193147180559945e+00 0.600000000000000 0.089174376234009e+00 0.568413897526397 0.003508464725863e+00 0.567120282313471-6.357732002681971e-05 0.567143306843229 4.540927101004399e-08 0.567143290409997 5.876410469340954e-13 0.567143290409784-1.110223024625157e-16 On the other hand, the MATLAB output using Newton s method follows: f() 0.500000000000000-0.193147180559945e+00 0.564382393519982-0.007640861009083e+00 0.567138987715060-1.188933308848839e-05 0.567143290399369-2.877842408821607e-11 0.567143290409784-1.110223024625157e-16 From the above results it can be discerned that Newton s method converges faster. Note that 7 iterations required for secant method while 5 for Newton s one. 4. Assume that a function f is twice differentiable, that is, f C 2 [a, b] and there is a root x [a, b] such that f(x ) = 0 and f (x ) 0. We saw in class that Newton s method converges quadratically. Prove this is indeed the case. Hint: Let e k = x k x denotes the error in the kth iterate. Do a Taylor expansion about of f(x) about x k and then evaluate it at x k+1. Use this expansion in Newton s algeorithm to compare e k and e k+1. l
Math 551 Homework Assignment 3 Page 6 of 6 Solution: At first, we note that x k = e k + x and, similarly, x k+1 = e k+1 + x. Then, from Newton s method we have that x + e k+1 = x + e k f(x + e k ) f (x + e k ). Next, by Taylor expanding f(x) about x + e k up to second order we arrive at x + e k+1 = x + e k f(x ) + e k f (x ) + 1 2 e2 k f (x ). (1) f (x ) + e k f (x ) However, based on our assumption f (x ) 0 we note that and since f(x ) = 0, Eq. (1) yields to 1 1 + e k f (x )/f (x ) 1 e f (x ) k f (x ), e k+1 = f (x ) 2f (x ) e2 k e k+1 = 1 f (x ) 2 f (x ) e2 k, (2) that is, Newton s method converges quadratically.