Some Comments on Root finding
Avoid subtraction of nearly equal numbers Some Comments on Root finding Consider the following two numbers a= 0.123456789012345678; b= 0.123456789012345677; c = a - b 0.123456789012345678 - 0.123456789012345677 ans = 0.000000000000000001 Avoid subtraction of nearly equal numbers a=1e-16; b=1; c=-1; a+(b+c) (a+b)+c Associative property 𝑎+ 𝑏+𝑐 = 𝑎+𝑏 +𝑐 ans = 1.000000000000000e-16 why
Avoid subtraction of nearly equal numbers Some Comments on Root finding Avoid subtraction of nearly equal numbers % solve x^2 + b*x + 1 = 0 f = @(x) x*(a*x+b)+c; xeps = 1e-8; b = (2/xeps) + xeps; a=1; c=1; f = @(x) a*x^2 + b*x +c x1 = (-b+sqrt(b^2-4*a*c))/(2*a) y1=(-2*c)/(b+sqrt(b^2-4*a*c)) f(x1) f(y1) abs((x1-y1)/y1)*100 Solve: 𝑥 2 +𝑏 𝑥+1=0 𝑏=2× 10 8 𝑥 1 = −𝑏+ 𝑏 2 −4𝑎𝑐 2𝑎 rationalizing the numerator x1 = y1 = -5.000000000000000e-09 ans = 100 𝑥 1 = −2𝑐 𝑏+ 𝑏 2 −4𝑎𝑐
Avoid subtraction of nearly equal numbers Some Comments on Root finding Avoid subtraction of nearly equal numbers The Secant Method Algebraically equivalent formula 𝑥 𝑛+1 = 𝑥 𝑛 − 𝑓( 𝑥 𝑛 )( 𝑥 𝑛 − 𝑥 𝑛−1 ) 𝑓 𝑥 𝑛 −𝑓( 𝑥 𝑛−1 ) 𝑥 𝑛+1 = 𝑥 𝑛−1 𝑓 𝑥 𝑛 − 𝑥 𝑛 𝑓 𝑥 𝑛−1 𝑓 𝑥 𝑛 −𝑓( 𝑥 𝑛−1 ) in general, this iteration equation is likely to be less accurate than the one given in left box. why Here, we subtract two nearly equal numbers in both numerator and denominator.
Formula Error Equation guess Some Comments on Root finding Formula Error Equation guess x0 suff close to x* Any 𝑥 𝑛+1 = 𝑥 𝑛 − 𝑓( 𝑥 𝑛 ) 𝑓′( 𝑥 𝑛 ) 𝑥 ∗ − 𝑥 𝑛+1 = 𝑓 2 𝜉 2 𝑓 ′ 𝑥 𝑛 𝑥 ∗ − 𝑥 𝑛 2 Newton’s Secant 𝑥 𝑛+1 = 𝑥 𝑛 − 𝑓( 𝑥 𝑛 )( 𝑥 𝑛 − 𝑥 𝑛−1 ) 𝑓 𝑥 𝑛 −𝑓( 𝑥 𝑛−1 ) 𝑥 𝑛+1 = 𝑎 𝑛 + 𝑏 𝑛 2 𝑥 ∗ − 𝑥 𝑛 ≤ 𝑏−𝑎 2 𝑛 Bisection Fixed-point 𝑥 𝑛+1 =𝑓( 𝑥 𝑛 ) 𝑥 ∗ − 𝑥 𝑛 ≤ 𝑘 𝑛 𝑥 ∗ − 𝑥 𝑛−1 𝑔′(𝑥) ≤𝑘 ∀𝑥 False position Secant +test
Some Comments on Root finding Cq1 solution Cq2 solution
Apply Newton’s method to find a solution to 𝑥−6𝑐𝑜𝑠𝑥=0, 𝑥 0 =2 Some Comments on Root finding Apply Newton’s method to find a solution to 𝑥−6𝑐𝑜𝑠𝑥=0, 𝑥 0 =2 In the interval [0,𝜋] that is accurate to within 10 −3 𝑥 𝑛+1 = 𝑥 𝑛+1 − 𝑥−6𝑐𝑜𝑠𝑥 1+6sin(𝑥) Newton’s method: 𝑛 𝑥 𝑛 𝑥 𝑛+1 − 𝑥 𝑛 0 2.0000000000 1 1.3034338465 6.9656615347e-01 2 1.3449403509 4.1506504376e-02 3 1.3447510489 1.8930200914e-04 4 1.3447510454 3.5169049895e-09
Multiple Root Some Comments on Root finding 𝑥=1 is a zero of multiplicity 2 of the the function 𝑓 𝑥 = (𝑥−1) 2 (𝑥−3) 2 Note that no change of sign close to the root. (bisection ,FP ??) Definition: Theorem p of f (x) = 0 is a zero of multiplicity m of f if for 𝑥=𝑝, we can write 𝑓 𝑥 = (𝑥−𝑝) 𝑚 𝑞 𝑥 where 𝑞(𝑝)≠0 p is a zero of multiplicity m of f if and only if 𝑓 𝑝 = 𝑓 (1) 𝑝 =⋯= 𝑓 𝑚−1 𝑝 =0 and 𝑓 (𝑚) 𝑝 ≠0
Multiple Root Some Comments on Root finding Example: Show that f has a zero of multiplicity 2 at x = 0. 𝑓 𝑥 = 𝑒 𝑥 −𝑥−1 Newton’s method are shown in Table The sequence is clearly converging to 0, but not quadratically.
modification of Newton’s method Some Comments on Root finding modification of Newton’s method Example: Show that f has a zero of multiplicity 2 at x = 0. 𝑓 𝑥 = 𝑒 𝑥 −𝑥−1 If p is a zero of f of multiplicity m 𝑓 𝑥 = (𝑥−𝑝) 𝑚 𝑞 𝑥 then p is a zero of 𝑓′ of multiplicity m-1 Hence p is a simple zero of μ 𝑥 = 𝑓(𝑥) 𝑓′(𝑥) Newton’s method can then be applied to μ 𝑥 The only drawback to this method is the additional calculation of
modification of Newton’s method Some Comments on Root finding modification of Newton’s method Example: Show that f has a zero of multiplicity 2 at x = 0. 𝑓 𝑥 = 𝑒 𝑥 −𝑥−1 If p is a zero of f of multiplicity m 𝑓 𝑥 = (𝑥−𝑝) 𝑚 𝑞 𝑥 then p is also a fixed point of 𝑔 𝑥 =𝑥−𝑚 𝑓(𝑥) 𝑓′(𝑥) 𝑥 𝑛+1 = 𝑥 𝑛 −𝑚 𝑓( 𝑥 𝑛 ) 𝑓′( 𝑥 𝑛 )
Nonlinear system of equations: Some Comments on Root finding Nonlinear system of equations: 𝑓 1 =𝑥 1 2 + 𝑥 2 + 𝑥 3 2 −8 𝑓 2 =2𝑥 1 + 3𝑥 2 2 − 𝑥 3 −11 𝑓 3 =𝑥 1 2 + 𝑥 2 2 + 𝑥 3 2 −14 𝑥 1 2 + 𝑥 2 + 𝑥 3 2 =8 2𝑥 1 + 3𝑥 2 2 − 𝑥 3 =11 𝑥 1 2 + 𝑥 2 2 + 𝑥 3 2 =14 Convert into 𝐹 𝑋 =0 𝐹 𝑋 = 𝑥 1 2 + 𝑥 2 + 𝑥 3 2 −8 2𝑥 1 + 3𝑥 2 2 − 𝑥 3 −11 𝑥 1 2 + 𝑥 2 2 + 𝑥 3 2 −14 𝑥 1 2 + 𝑥 2 + 𝑥 3 2 −8=0 2𝑥 1 + 3𝑥 2 2 − 𝑥 3 −11=0 𝑥 1 2 + 𝑥 2 2 + 𝑥 3 2 −14=0 𝑋= 𝑥 1 𝑥 2 𝑥 3
Some Comments on Root finding Def: Jacobian Matrix 𝐹 𝑋 = 𝑥 1 2 + 𝑥 2 + 𝑥 3 2 −8 2𝑥 1 + 3𝑥 2 2 − 𝑥 3 −11 𝑥 1 2 + 𝑥 2 2 + 𝑥 3 2 −14 𝐽 𝑋 = 𝜕 𝑓 1 𝑥 1 𝜕 𝑓 1 𝑥 2 𝜕 𝑓 1 𝑥 3 𝜕 𝑓 2 𝑥 1 𝜕 𝑓 2 𝑥 2 𝜕 𝑓 2 𝑥 3 𝜕 𝑓 3 𝑥 1 𝜕 𝑓 3 𝑥 2 𝜕 𝑓 3 𝑥 3 𝑋= 𝑥 1 𝑥 2 𝑥 3 Example: 𝐽 𝑋 = 2 𝑥 1 1 2 𝑥 3 2 6 𝑥 2 2 −1 2 𝑥 1 2 𝑥 2 2 𝑥 3 Example: 𝑋 (0) = 1 −1 1 𝐽 𝑋 (0) = 2 1 2 2 6 −1 2 −2 2
𝑋 (𝑛+1) = 𝑋 (𝑛) − 𝐽 ( 𝑋 (𝑛) ) −𝟏 𝑓( 𝑋 (𝑛) ) Some Comments on Root finding Newton’s method for single variable 𝑥 𝑛+1 = 𝑥 𝑛 − 𝑓( 𝑥 𝑛 ) 𝑓′( 𝑥 𝑛 ) 𝑥 𝑛+1 = 𝑥 𝑛 − 𝑓′( 𝑥 𝑛 ) −𝟏 𝑓( 𝑥 𝑛 ) Newton’s method for system of nonlinear equations 𝐹 𝑋 =0 𝑋 (𝑛+1) = 𝑋 (𝑛) − 𝐽 ( 𝑋 (𝑛) ) −𝟏 𝑓( 𝑋 (𝑛) ) 𝑋 (𝑛) = 𝑥 1 (𝑛) 𝑥 2 (𝑛) 𝑥 3 (𝑛)
𝑨𝑌 𝑛 =𝑏 Newton’s method for system of nonlinear equations 𝐹 𝑋 =0 Some Comments on Root finding Newton’s method for system of nonlinear equations 𝐹 𝑋 =0 𝑋 (𝑛+1) = 𝑋 (𝑛) − 𝐽 ( 𝑋 (𝑛) ) −𝟏 𝑓( 𝑋 (𝑛) ) 𝑋 (𝑛) = 𝑥 1 (𝑛) 𝑥 2 (𝑛) 𝑥 3 (𝑛) 𝑋 (𝑛+1) − 𝑋 𝑛 =− 𝐽 ( 𝑋 (𝑛) ) −𝟏 𝑓( 𝑋 (𝑛) ) 𝑋 (𝑛+1) − 𝑋 𝑛 = 𝐽 𝑋 𝑛 −𝟏 (−𝑓 𝑋 𝑛 ) 𝑌 𝑛 = 𝐽 𝑋 𝑛 −𝟏 (−𝑓 𝑋 𝑛 ) 𝐽 𝑋 𝑛 𝑌 𝑛 = −𝑓 𝑋 𝑛 𝑨𝑌 𝑛 =𝑏 This is a linear system of n-equations in n-unknowns where 𝐴=𝐽 𝑋 𝑛 𝒂𝒏𝒅 𝑏= −𝑓 𝑋 𝑛
ALGORITHM: (Newton’s method for system of nonlinear equations) Some Comments on Root finding ALGORITHM: (Newton’s method for system of nonlinear equations) 𝐹= 𝑥 1 2 + 𝑥 2 + 𝑥 3 2 −8 2𝑥 1 + 3𝑥 2 2 − 𝑥 3 −11 𝑥 1 2 + 𝑥 2 2 + 𝑥 3 2 −14 𝐽 𝑋 = 2 𝑥 1 1 2 𝑥 3 2 6 𝑥 2 2 −1 2 𝑥 1 2 𝑥 2 2 𝑥 3 𝑿 (𝟎) = 𝒙 𝟏 (𝟎) 𝒙 𝟐 (𝟎) 𝒙 𝟑 (𝟎) 𝑋 (0) = 1 −1 1 Given initial guess 𝐽 𝑋 (0) = 2 1 2 2 6 −1 2 −2 2 1) Calculate Jacobian Matrix 𝑨= 𝑱 𝑿 (𝟎) 𝑏= −7 −7 −13 2) Calculate right-hand-side vector 𝒃= −𝒇 𝑿 𝟎 2 1 2 2 6 −1 2 −2 2 𝑦 1 (0) 𝑦 2 (0) 𝑦 3 (0) = −7 −7 −13 → 𝑦 1 (0) 𝑦 2 (0) 𝑦 3 (0) = 19/18 −4/3 28/9 3) Solve the linear system: 𝑨𝒀 𝟎 =𝒃 4) update 𝑋 (1) = 2.0556 −2.333 4.111 𝑿 (𝟎) = 𝑿 (𝟎) + 𝒀 (𝟎) Repeat (1 - 4)
Some Comments on Root finding clear; clc f = @(x) [ x(1)^2+x(2)+… x(3)^2-8;2*x(1)+3*x(2)^2-x(3)-11; x(1)^2+x(2)^2+x(3)^2-14]; x=[1;-1;1]; format short for n=1:10 [J] = jacob(x); F = - f(x); y = J\F x = x + y end x function [J] = jacob(x); J=[ 2*x(1) 1 2*x(3); ... 2 6*x(2) -1 ; … 2*x(1) 2*x(2) 2*x(3)]; 1 2 3 4 n 𝒙 𝟏 (𝒏) 𝒙 𝟐 (𝒏) 𝒙 𝟑 (𝒏) 2.0556 -2.3333 4.1111 1.1387 -2.0196 3.2187 1.0045 -2.0001 3.0088 1.0000 -2.0000 3.0000 𝑥 1 2 + 𝑥 2 + 𝑥 3 2 =8 2𝑥 1 + 3𝑥 2 2 − 𝑥 3 =11 𝑥 1 2 + 𝑥 2 2 + 𝑥 3 2 =14