MATH 4513 Numerical Analysis Chapter 2. Solutions of Equations in One Variable
Total Page:16
File Type:pdf, Size:1020Kb
MATH 4513 Numerical Analysis Chapter 2. Solutions of Equations in One Variable Xu Zhang Assistant Professor Department of Mathematics Oklahoma State University Text Book: Numerical Analysis (10th edition) R. L. Burden, D. J. Faires, A. M. Burden Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 1 / 70 Chapter 2. Solutions of Equations in One Variable Chapter 2. Solutions of Equations in One Variable Contents 2.1 The Bisection Method 2.2 Fixed-Point Iteration 2.3 Newton’s Method and Its Extensions 2.4 Error Analysis for Iterative Methods Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 2 / 70 Chapter 2. Solutions of Equations in One Variable 2.1 The Bisection Method Section 2.1 The Bisection Method Starting from this section, we study the most basic mathematics problem: root-finding problem f(x) = 0: The first numerical method, based on the Intermediate Value Theorem (IVT), is called the Bisection Method. Suppose that f(x) is continuous on [a; b]. f(a) and f(b) have opposite sign. By IVT, there exists a number p 2 (a; b) such that f(p) = 0. That is, f(x) has a root in (a; b). Idea of Bisection Method: repeatedly halve the subinterval of [a; b], and at each step, locating the half containing the root. Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 3 / 70 Chapter 2. Solutions of Equations in One Variable 2.1 The Bisection Method 2.1 The Bisection Method 49 a1+b1 FigureSet 2.1a1 a, b1 b. Calculate the midpoint p1 2 . y f (b) y f (x) f (p1) p3 p x a a1 p2 p1 b b1 f (p2) f (a) a1 p1 b1 If f(p1) = 0, then p p1, done. a2 p2 b2 If f(p ) 6= 0, then f(p ) has the same sign as either f(a) or f(b). 1 1 a3 p3 b3 If f(p1) and f(a) have the same sign, then p 2 (p1; b1). Set a2 p1, and b2 b1. If f(p1) and f(b) have the same sign, then p 2 (a1; p1). ALGORITHM Bisection 2.1 Set a2 a1, and b2 p1. To find a solution to f(x) = 0 given the continuous function f on the interval [a, b], where f(a) and f(b) have opposite signs: Repeat the process on [a2; b2]. INPUT endpoints a, b; tolerance TOL; maximum number of iterations N0. Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 4 / 70 OUTPUT approximate solution p or message of failure. Step 1 Set i = 1; FA = f(a). Step 2 While i ≤ N0 do Steps 3–6. Step 3 Set p = a + (b − a)/2; (Compute pi.) FP = f(p). Step 4 If FP = 0or(b − a)/2 < TOL then OUTPUT (p); (Procedure completed successfully.) STOP. Step 5 Set i = i + 1. Step 6 If FA · FP > 0 then set a = p;(Compute ai, bi.) FA = FP else set b = p.(FA is unchanged.) Step 7 OUTPUT (‘Method failed after N0 iterations, N0 =’, N0); (The procedure was unsuccessful.) STOP. Other stopping procedures can be applied in Step 4 of Algorithm 2.1 or in any of the iterative techniques in this chapter. For example, we can select a tolerance ε>0 and generate p1, ..., pN until one of the following conditions is met: Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. Chapter 2. Solutions of Equations in One Variable 2.1 The Bisection Method ALGORITHM – Bisection (Preliminary Version) USAGE: to find a solution to f(x) = 0 on the interval [a; b]. p = bisect0 (f; a; b) For n = 1; 2; 3; ··· ; 20, do the following Step 1 Set p = (a + b)=2; Step 2 Calculate FA = f(a), FB = f(b), and FP = f(p). Step 3 If FA · F P > 0, set a = p If FB · F P > 0, set b = p. Go back to Step 1. Remark This above algorithm will perform 20 times bisection iterations. The number 20 is artificial. Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 5 / 70 Chapter 2. Solutions of Equations in One Variable 2.1 The Bisection Method Example 1. Show that f(x) = x3 + 4x2 − 10 = 0 has a root in [1; 2] and use the Bisection method to find the approximation root. Solution. Because f(1) = −5 and f(2) = 14, the IVT ensures that this continuous function has a root in [1; 2]. To proceed with the Bisection method, we write a simple MATLAB code. Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 6 / 70 Chapter 2. Solutions of Equations in One Variable 2.1 The Bisection Method 8/21/19 5:28 PM /Users/xuzhang/Dropbox/Teachi.../bisect0.m 1 of 1 Matlab Code for Bisection (Preliminary Version) function p = bisect0(fun,a,b) % This is a preliminary version of Bisection Method for n = 1:20 % Set max number of iterations to be 20 p = (a+b)/2; FA = fun(a); FB = fun(b); FP = fun(p); if FA*FP > 0 a = p; elseif FB*FP > 0 b = p; end end A8/21/19 “Driver”5:28 File PM /Users/xuzhang/Dropbox/Teachi.../ex2_1_0.m 1 of 1 % Driver File: Example 2.1.1 in the Textbook %% Inputs fun = @(x) x^3+4*x^2-10; a = 1; b = 2; %% Call the subroutine: bisect0.m p = bisect0(fun,a,b) Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 7 / 70 Chapter 2. Solutions of Equations in One Variable 2.1 The Bisection Method After 20 iterations, we obtain the solution p ≈ 1:365229606628418. To display more information from the whole iteration process, we modify the MATLAB subroutine file. Matlab8/21/19 Code5:39 forPM Bisection/Users/xuzhang/Dropbox/Teachi.../bisect1.m (Preliminary Version with more outputs) 1 of 1 function p = bisect1(fun,a,b) % This is a preliminary version of Bisection Method % This version displays intermediate outputs nicely disp('Bisection Methods') disp('-----------------------------------------------------------------') disp(' n a_n b_n p_n f(p_n)') disp('-----------------------------------------------------------------') formatSpec = '%2d % .9f % .9f % .9f % .9f \n'; for n = 1:20 % Set max number of iterations to be 20 p = (a+b)/2; FA = fun(a); FB = fun(b); FP = fun(p); fprintf(formatSpec,[n,a,b,p,fun(p)]) % Printing output if FA*FP > 0 a = p; elseif FB*FP > 0 b = p; end end Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 8 / 70 Chapter 2. Solutions of Equations in One Variable 2.1 The Bisection Method Some Remarks on Bisection Method To start, an interval [a; b] must be found with f(a) · f(b) < 0. Otherwise, there may be no solutions in that interval. It is good to set a maximum iteration number“ maxit”, in case the the iteration enters an endless loop. It is good to set a tolerance or stopping criteria to avoid unnecessary computational effort, such as bn − an 1 < tol 2 2 jpn − pn+1j < tol jpn − pn+1j 3 < tol jpnj 4 jf(pn)j < tol Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 9 / 70 Chapter 2. Solutions of Equations in One Variable 2.1 The Bisection Method A8/27/19 more12:00 robust AM Matlab/Users/xuzhang/Dropbox/Teachi.../bisect.m code for Bisection method 1 of 1 function [p,flag] = bisect(fun,a,b,tol,maxIt) %% This is a more robust version of Bisection Method than bisect1.m flag = 0; % Use a flag to tell if the output is reliable if fun(a)*fun(b) > 0 % Check f(a) and f(b) have different sign error('f(a) and f(b) must have different signs'); end disp('Bisection Methods') disp('-----------------------------------------------------------------') disp(' n a_n b_n p_n f(p_n)') disp('-----------------------------------------------------------------') formatSpec = '%2d % .9f % .9f % .9f % .9f \n'; for n = 1:maxIt p = (a+b)/2; FA = fun(a); FP = fun(p); fprintf(formatSpec,[n,a,b,p,fun(p)]) % Printing output if abs(FP) <= 10^(-15) || (b-a)/2 < tol flag = 1; break; % Break out the for loop. else if FA*FP > 0 a = p; else b = p; end end end Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 10 / 70 Chapter 2. Solutions of Equations in One Variable 2.1 The Bisection Method Example 2. Use Bisection method to find a root of f(x) = x3 + 4x2 − 10 = 0 in the interval [1; 2] that is accurate to at least within 10−4. Solution. 8/14/18 2:17 PM /Users/zhang/Dropbox/Teaching.../ex2_1_1.m 1 of 1 We write a Matlab driver file for this test problem % Example 2.1.1 in the Textbook fun = @(x) x^3+4*x^2-10; a = 1; b = 2; tol = 1E-4; maxIt = 40; [p,flag] = bisect(fun,a,b,tol,maxIt); In this driver file, we specify all five inputs: fun, a, b, tol, maxIt call the Bisection method code bisect.m. Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 11 / 70 Chapter 2. Solutions of Equations in One Variable 2.1 The Bisection Method Outputs8/27/19 from12:08 the AM MatlabMATLAB Command Command Window Window 1 of 1 >> ex2_1_1 Bisection Methods ----------------------------------------------------------------- n a_n b_n p_n f(p_n) ----------------------------------------------------------------- 1 1.000000000 2.000000000 1.500000000 2.375000000 2 1.000000000 1.500000000 1.250000000 -1.796875000 3 1.250000000 1.500000000 1.375000000 0.162109375 4 1.250000000 1.375000000 1.312500000 -0.848388672 5 1.312500000 1.375000000 1.343750000 -0.350982666 6 1.343750000 1.375000000 1.359375000 -0.096408844 7 1.359375000 1.375000000 1.367187500 0.032355785 8 1.359375000 1.367187500 1.363281250 -0.032149971 9 1.363281250 1.367187500 1.365234375 0.000072025 10 1.363281250 1.365234375 1.364257812 -0.016046691 11 1.364257812 1.365234375 1.364746094 -0.007989263 12 1.364746094 1.365234375 1.364990234 -0.003959102 13 1.364990234 1.365234375 1.365112305 -0.001943659 14 1.365112305 1.365234375 1.365173340 -0.000935847 >> The approximation pn converges to the true solution p = 1:365230013::: Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 12 / 70 Chapter 2.