Computational Engineering Numerical Methods Dr. Kalidas Yeturu Computation Engineering course, IIT Tirupati - Dr. Kalidas Yeturu Roots of functions • Motivation – Very generic, applicable in different engineering contexts – Solving gradient function equals 0 – Plotting of contours • Some of the popular examples – Matlab – roots() and fzero() functions – Python – scipy.optimize.root() Computation Engineering course, IIT Tirupati - Dr. Kalidas Yeturu Definition and examples • • Given π π₯ Roots are a set of values of x, ∃π₯ βΆ π₯: π π₯ = 0 • Simple equations – – π π₯ = ππ₯ + π π π₯ = ππ₯ 2 + ππ₯ + π → π₯= −π − π 2 − 4ππ −π + π 2 − 4ππ , 2π 2π Need sophisticated methods for finding roots of higher order and transcendental functions • Higher order polynomials – • Transcendental equations – – • π π₯ = 5x 2 − x 3 + 7x 6 π π₯ = ln (x 2 ) − 1 π π₯ = e−0.2π₯ sin(3x − 0.5) Types of methods – – Single real root detection Determining all roots both real and complex Computation Engineering course, IIT Tirupati - Dr. Kalidas Yeturu Root detection by visual inspection of plots • π π = ππ π 1−π −π ∗π‘ π −π£ • π‘ = 10, π = 9.8, π£ = 40, π = 68.1 • Plot and examine where it intersects X axis • Root ~ 14.75 Issues • • • • Error prone Subjective Not scalable (imagine, we need it as a routine invoked 1 million times) Difficult for multivariate scenarios Computation Engineering course, IIT Tirupati - Dr. Kalidas Yeturu Algorithms for root finding • Bracketing methods – Where root exists in an interval – Bisection – Regula Falsi • Open methods – Where starting from a single point, a sequence of steps will lead to root – Simple Fixed Point – Newton-Raphson – Secant Method • Iterative in nature – New value at time step t+1 depends of value at time step t • Special cases and convergence aspects Computation Engineering course, IIT Tirupati - Dr. Kalidas Yeturu Root finding – Bracketing methods • Given an interval of coordinates on X axis • Determine a point in the closed interval as root • Case (a) – Root does not exist in the interval • Case (b) – Single root exists • Case (c) – Two roots exist • Case (d) – Multiple roots exist Computation Engineering course, IIT Tirupati - Dr. Kalidas Yeturu Bracketing methods… Same sign 0 roots Same sign Even number of roots Opposite sign 1 roots Same sign Opposite sign 2 roots Odd number of roots Opposite sign 3 roots Computation Engineering course, IIT Tirupati - Dr. Kalidas Yeturu Bracketing methods… • Opposite signs – Case (e): Function is tangential to X axis :Two roots – Case (f): Function is discontinuous: Two roots (e) • Same sign, function touches X axis – Case (g) : One root • These are special cases, need to handled separately (f) Computation Engineering course, IIT Tirupati - Dr. Kalidas Yeturu (g) Bracketing methods – Bisection/Bolzano method 1. 2. 3. Choose xl and xu such that function changes sign f xl ∗ f xu < 0 π₯ +π₯ Determine mid point xr = π π’ 2 Evaluate cases – If f xl ∗ f xr < 0 Root lies between [xl and xr] Set xu = xr Goto Step 2 f(xu) f(xl) – If f xl ∗ f xr > 0 xl Root lies between [xr and xu] Set xl = xr Goto Step 2 π₯π = – If f xl ∗ f xr = 0 xr is the root Break from loop • Report xr Computation Engineering course, IIT Tirupati - Dr. Kalidas Yeturu xu π₯π + π₯π’ 2 Bisection method… Input initial xr Avoid divide by 0 π₯ππππ€ − π₯ππππ ππ = ∗ 100% π₯ππππ€ π‘ππ’π − π₯ππππ€ ππ‘ = ∗ 100% π‘ππ’π Set error to 0 Error is small Number of iterations is over Computation Engineering course, IIT Tirupati - Dr. Kalidas Yeturu Bisection method…! Improved Bisection method n+1 evaluations of f(x) 2n evaluations of f(x) One time evaluation before the loop Single invocation of f(x) inside the loop Two times f(x) evaluations Remember the value Computation Engineering course, IIT Tirupati - Dr. Kalidas Yeturu Bracketing methods – Regula Falsi Method • Consider triangles βπ΄π΅πΆ and βπΆπ·πΈ E • Vertical angles are same β«= π΅πΆπ΄Ϋ¦β¬ β«π·πΆπΈΫ¦β¬ • Ratio of opposite side by adjacent πΈπ· π΄π΅ sides are same ο πΆπ· = π΄πΆ • Translating the above ratio to function values and X coordinates π(π₯π’ ) −π π₯π = (π₯π’ − π₯π ) (π₯π − π₯π ) • After rearranging terms, we have π π₯π’ (π₯π − π₯π’ ) π₯π = π₯π’ − π π₯π − π π₯π’ C A D B Idea – As the bracket is close to the root, curve segment is nearly a straight line… Computation Engineering course, IIT Tirupati - Dr. Kalidas Yeturu Regula Falsi method… 1. 2. 1. Choose xl and xu such that function changes sign f xl ∗ f xu < 0 Determine mid point π π₯π’ (π₯π − π₯π’ ) π₯π = π₯π’ − π π₯π − π π₯π’ Evaluate cases – If f xl ∗ f xr < 0 Root lies between [xl and xr] Set xu = xr Goto Step 2 – If f xl ∗ f xr > 0 Root lies between [xr and xu] Set xl = xr Goto Step 2 – If f xl ∗ f xr = 0 xr is the root Break from loop • Report xr Computation Engineering course, IIT Tirupati - Dr. Kalidas Yeturu Regula Falsi method… Computation Engineering course, IIT Tirupati - Dr. Kalidas Yeturu Regula Falsi method… Different curves have different convergence Computation Engineering course, IIT Tirupati - Dr. Kalidas Yeturu Regula Falsi method… Enhanced approach Naïve approach 1. 2. 1. Choose xl and xu such that function changes sign f xl ∗ f xu < 0 Determine mid point π π₯π’ (π₯π − π₯π’ ) π₯π = π₯π’ − π π₯π − π π₯π’ Evaluate cases – If f xl ∗ f xr < 0 Root lies between [xl and xr] Set xu = xr Goto Step 2 – If f xl ∗ f xr > 0 Root lies between [xr and xu] Set xl = xr Goto Step 2 – If f xl ∗ f xr = 0 xr is the root Break from loop • Report xr Computation Engineering course, IIT Tirupati - Dr. Kalidas Yeturu Regula Falsi method… ππ is not moving … … … … π π₯π < 0 π π₯π’ (π₯π − π₯π’ ) π π₯π − π π₯π’ π(π₯π’ ) (π₯π − π₯π’ ) π₯′π = π₯π’ − 2 π π₯ π π₯π − 2π’ Let π₯π − π₯π′ > 0 → π π₯π > 0 Ends up in a contradiction! ⇒ π₯π < π₯π′ π₯π = π₯π’ − xr xl xu i.e. root estimate moves right after π π₯π’ ← Computation Engineering course, IIT Tirupati - Dr. Kalidas Yeturu π(π₯π’ ) 2 Bracketing interval detection Upper bound Lower bound Computation Engineering course, IIT Tirupati - Dr. Kalidas Yeturu