Numerical Methods Lecture 3 Nonlinear Equations by Pavel Ludvík
Introduction Definition (Root or zero of a function) A root (or a zero) of a function f is a solution of an equation f (x) = 0. We learn several root-finding methods: the bisection method, false position, the secant method, the Newton Raphson method. Numerical Methods Lecture 3 by Pavel Ludvík 2 / 24
Introduction Definition (Root or zero of a function) A root (or a zero) of a function f is a solution of an equation f (x) = 0. We learn several root-finding methods: the bisection method, false position, the secant method, the Newton Raphson method. Numerical Methods Lecture 3 by Pavel Ludvík 2 / 24
Introduction General Root-Finding Strategy 1. Bracket the roots, i.e. find the disjoint intervals containing the individual roots. 2. Set the initial approximation(s). 3. Repeat applying the iterative formula until the stopping criterion is met. Numerical Methods Lecture 3 by Pavel Ludvík 3 / 24
Introduction General Root-Finding Strategy 1. Bracket the roots, i.e. find the disjoint intervals containing the individual roots. 2. Set the initial approximation(s). 3. Repeat applying the iterative formula until the stopping criterion is met. Differences Between Methods the number of starting values, the guarantee, or otherwise, of convergence, the speed of convergence, the cost, in terms of the work to be done per iteration. Numerical Methods Lecture 3 by Pavel Ludvík 3 / 24
Bisection Method - Description The bisection method is the simplest of all the methods for finding a root of a nonlinear equation. We start with an interval containing a root and divide it into a left and a right half.we decide which of the two halves contains a root and proceed with a further division of that half. We do this repeatedly until the interval containing a root is sufficiently narrowed down to meet the level of accuracy required. If we take the mid-point of the latest interval to be an approximation to a root, we can say that this is accurate to within ± half the width of the interval. Numerical Methods Lecture 3 by Pavel Ludvík 4 / 24
Bisection Method - Algorithm Theorem Let f be a continuous function on an interval [a, b] and f (a)f (b) < 0. Then there exists a number c (a, b) that f (c) = 0. Algorithm for solving f (x) = 0 by Bisection Method 1. Find an interval [a, b] in which f (x) = 0. 2. Set x to a+b 2. 3. Test for convergence: Find out if b a 2 ε. 4. Calculate f (x ). 5. If f (x ) and f (b) have opposite sign set a to x, otherwise set b to x. 6. Repeat from step 2. Numerical Methods Lecture 3 by Pavel Ludvík 5 / 24
Example Find a root of the equation 8 4.5(x sin x) = 0 with a tolerance less than ε = 0.01. Solution 1. We define a function f (x) = 8 4.5(x sin x), plot its graph and find out the solution is between x = 2 and x = 3. Hence we choose a 0 = 2 and b 0 = 3. 2. We compute the midpoint c 0 = a 0+b 0 2 = 2.5 and the current error value Error 0 = b 0 a 0 2 = 0.5 which is bigger than the tolerance 0.01. 3. We have to carry out a next step. We need to answer the question: "Is the root in the interval [a 0, c 0 ] = [2, 2.5] or [c 0, b 0 ] = [2.5, 3]?"If f (2)f (2.5) < 0 then the root is in [2, 2.5], if f (2.5)f (3) < 0 then in [2.5, 3], if none of it is truth then necessarily 2.5 is a root itself. We compute that f (2)f (2.5) < 0 and hence we set a 1 = 2, b 1 = 2.5. Numerical Methods Lecture 3 by Pavel Ludvík 6 / 24
Solution 4. We repeat the algorithm until the error is smaller or equal to ε. 5. See the results in a table: n a n c n b n Error 0 2 2.5 3 0.5 > 0.01 1 2 2.25 2.5 0.25 > 0.01 2 2.25 2.375 2.5 0.125 > 0.01 3 2.375 2.4375 2.5 0.0625 > 0.01 4 2.375 2.40625 2.4375 0.03125 > 0.01 5 2.40625 2.421875 2.4375 0.015625 > 0.01 6 2.421875 2.429688 2.4375 0.007813 0.01 6. The conclusion is: Root = 2.429688 ± 0.01 Numerical Methods Lecture 3 by Pavel Ludvík 7 / 24
Bisection Method - Matlab Exercise A Write an M-File for one step of the Bisection Method. Input: Function f and limitpoints of an interval [a n, b n ]. Output: Limitpoints of an interval [a n+1, b n+1 ]. Exercise B Create an M-File for the Bisection Method. The program should stop before doing 24 iterations. Numerical Methods Lecture 3 by Pavel Ludvík 8 / 24
Answers Answer A p = (a+b)/2; if f(a)*f(p)<0 b=p; elseif f(p)*f(b)<0 a=p else break end Numerical Methods Lecture 3 by Pavel Ludvík 9 / 24
Answer B f = ; % the function formula epsilon = ; % the precision maxn = ; % limitation on the number of steps a(1) = ; b(1) = ; % initial approximation for n = 1 : maxn p(n) = (a(n)+b(n))/2; if (b(n)-a(n))/2 <= epsilon, break elseif f(a(n))*f(p(n))<0 a(n+1) = a(n); b(n+1) = p(n); elseif f(p(n))*f(b(n))<0 a(n+1) = p(n); b(n+1) = b(n); else break end end disp( a(n) p(n) b(n) ) disp([a p b ]) Numerical Methods Lecture 3 by Pavel Ludvík 10 / 24
Exercises 1. Solve the equation e 0.2x e 0.8x 2 = 0 with an accuracy 10 3. 2. Show that a function f (x) = x sin x cos x has just one root in the interval [0, π/2] and find it to an accuracy of four decimal places. 3. Derive a formula for an error upper bound after n steps. 4. Try to use the Bisection Method with starting interval [ 2, 1] to find a root of the function f (x) = 1 x. What did happen? Explain why. 5. Can you tell the number of steps needed for Bisection Method to find the root to a given accuracy without carrying it out? How? Numerical Methods Lecture 3 by Pavel Ludvík 11 / 24
The Secant Method Description The Secant Method is a version of the bisection method that takes a more informed approach to reducing the interval containing a root. It is likely therefore to require a smaller number of iterations. The idea is to approximate the function by its secant and instead of looking for the root of the original function to use the root of the secant line as an approximation. A formula for a secant line of the function f connecting two points [a, f (a)] and [b, f (b)] is y = f (a) + f (b) f (a) (x a). b a Numerical Methods Lecture 3 by Pavel Ludvík 12 / 24
The Secant Method Algorithm Mathematical Formula x 0, x 1 = initial guesses, x x n = x n 1 f (x n 1 ) n 1 x n 2 f (x n 1 ) f (x n 2 ). Numerical Methods Lecture 3 by Pavel Ludvík 13 / 24
The Secant Method Algorithm Algorithm for solving f (x) = 0 by the Secant Method 1. Find an interval containing x such that f (x) = 0. 2. Set x to b f (b) b a f (b) f (a). 3. Test for convergence: Find out if b x ε. 4. Set a to b and b to x. 5. Repeat from step 2. Numerical Methods Lecture 3 by Pavel Ludvík 13 / 24
Example Find a root of the equation 8 4.5(x sin x) = 0 with a tolerance less than ε = 0.0001 = 10 4. Solution 1. We define a function f (x) = 8 4.5(x sin x). 2. From a graph of f we set the first two initial approximations, i.e. x 0 = 2 and x 1 = 3. The error is x 1 x 0 = 1 > ε. 3. The next approximation is x x 2 = x 1 f (x 1 ) 1 x 0 f (x 1 ) f (x 0 ) = 3 f (3) 3 2 f (3) f (2) = 2.3886 4. We evaluate the error which is x 3 x 2 = 0.6114 and the value is bigger then 10 4. Numerical Methods Lecture 3 by Pavel Ludvík 14 / 24
Solution x 2 x 1 5. We compute x 3 = x 2 f (x 2 ) f (x 2 ) f (x 1 ) = 2.4273, Error = x 3 x 2 = 0.0387. and continue in this manner until error is sufficiently small. 6. See the results in a table: 7. The conclusion is: n x n Error 0 2 1 3 1 > ε 2 2.3886 0.6114 > ε 3 2.4273 0.0386 > ε 4 2.4305 0.0032 > ε 5 2.4305 0.00002549598350 ε Root = 2.4305 ± 10 4 Numerical Methods Lecture 3 by Pavel Ludvík 15 / 24
The Secant Method Matlab f = ; % the function formula epsilon = ; % the precision maxn = ; % limitation on the number of steps x(1) = ; x(2) = ; % initial approximation for n = 3 : maxn x(n) = x(n-1) - f(x(n-1))*(x(n-1)-x(n-2))/(f(x( n-1))-f(x(n-2))); if abs(x(n)-x(n-1)) <= epsilon, break, end disp( x(n-2) x(n-1) x(n) Error ) disp([x(1:n-2) x(2:n-1) x(3:n) (x(3:n)-x(2:n-1)) ]) Numerical Methods Lecture 3 by Pavel Ludvík 16 / 24
Exercises 1. Solve the problems from the section about Bisection Method. 2. Use the Secant Method to calculate the 4 2 to an accuracy of six decimal places. 3. Think about the stopping criterion. Is it precise? If the consider error is small, does it necessarily mean we have a good approximation? Try to find an counterexample. 4. Look up the commands fzero and roots in the help of Matlab. Numerical Methods Lecture 3 by Pavel Ludvík 17 / 24
Newton-Raphson Method Description A feature of the secant method is its attempt to follow the graph of the function using a straight line. The Newton method pursues the idea by allowing the two points of the secant method to merge into one. In so doing it uses a single point and the tangent to the function at that point to construct a new estimate to the root. A formula for a zero point of a tangent line of the function f constructed at point a is x = a f (a) f (a). Numerical Methods Lecture 3 by Pavel Ludvík 18 / 24
Newton-Raphson Method Algorithm Mathematical Formula x 0 = initial guess, x n+1 = x n f (x n) f (x n ). Numerical Methods Lecture 3 by Pavel Ludvík 19 / 24
Newton-Raphson Method Algorithm Algorithm for solving f (x) = 0 by the Newton-Raphson Method 1. Find an interval containing x such that f (x) = 0. 2. Choose a starting point a. 3. If f (a) 0 replace a by x = a f f (a), otherwise restart using a different starting point a. 4. Test for convergence: Find out if x a ε. 5. Repeat from step 2. Numerical Methods Lecture 3 by Pavel Ludvík 19 / 24
Newton-Raphson Method Convergence Theorem (Convergence Theorem) Let e n denote the error after step n of the N-R Method. Furthermore, let f be twice continuously differentiable and f (p) = 0. If f (p) 0, then N-R Method is locally convergent to p and e lim n+1 n e 2 n = M (i. e., method is quadratically convergent), where Local Convergence M = f (p) 2f (p). A method is locally convergent if there exists an interval containing a root such that the sequence generated by this method converge to the root. Numerical Methods Lecture 3 by Pavel Ludvík 20 / 24
Example Find a root of the equation 8 4.5(x sin x) = 0 with a tolerance less than ε = 0.000001 = 10 6. Solution 1. We define a function f (x) = 8 4.5(x sin x) and compute its derivative f (x) = 4.5(1 cos x). 2. From a graph of f we set the initial approximation. We try x 0 = 2. 3. The first approximation will be x 1 = x 0 f (x 0) 8 4.5(2 sin(2)) f = 2 = 2.48517 (x 0 ) 4.5(1 cos(2)) 4. We evaluate the error which is x 1 x 0 = 0.48517 and the value is bigger then 10 6. Numerical Methods Lecture 3 by Pavel Ludvík 21 / 24
Solution 5. We compute x 2 = x 1 f (x 1) f (x 1 ), Error = x 2 x 1. and continue in this manner until error is sufficiently small. 6. See the results in a table: 7. The conclusion is: n x n Error 0 2 1 2.485172 0.485172 > ε 2 2.430987 0.054185 > ε 3 2.430465 0.000521 > ε 4 2.430465 0.000000 ε Root = 2.43046574172363 ± 10 6 Numerical Methods Lecture 3 by Pavel Ludvík 22 / 24
The Newton-Raphson Method Matlab f = @(x) ; % the function formula %%%%%%%%%%%%% SYMBOLIC TOOLBOX REQUIRED syms x ; % creating the symbolic variable x symdf = diff(f(x)); % the derivative df = matlabfunction(symdf); % conversion %%%%%%%%%%%%%OTHERWISE COMPUTE DERIVATIVE MANUALLY epsilon = ; % the precision maxn = ; % limitation on the number of steps clear x; x(1) = ; % clearing x as a symbolic variable; setting initial approximation for n = 2 : maxn x(n) = x(n-1) - f(x(n-1))/df(x(n-1)); if abs(x(n)-x(n-1)) <= epsilon, break, end end disp( x(n-1) x(n) Error ) disp([x(1:n-1) x(2:n) (x(1:n-1)-x(2:n)) ]) Numerical Methods Lecture 3 by Pavel Ludvík 23 / 24
Exercises 1. Solve the problems from the section about Bisection Method. 2. Use the Newton Method to calculate the 4 2 to an accuracy of six decimal places. 3. Think about the stopping criterion. Is it precise? If the consider error is small, does it necessarily mean we have a good approximation? Try to find an counterexample. 4. Prove that Newton Method applied to f (x) = ax + b converges in one step. 5. A 10-cm-high cone contains 60 cm 3 of ice cream, including a hemispherical scoop on top. Find the radius of the scoop to four correct decimal places. Numerical Methods Lecture 3 by Pavel Ludvík 24 / 24