Finding Roots of Equations
Solution Methods Overview Bisection/Half-interval Search Method of false position/regula Falsi Secant Method Newton Raphson Iteration Method Many more. Open Methods Bracketing Methods 2 Algebraic equation: a b c 0 Transcendental equation: 1 cos 5 0 CE 206: Engg. Computation sessional
Bracketing Methods: Bisection a new new old 100% Location of the root lies within the interval where the sign change occurs The absolute error is reduced by a factor of 2 for each iteration. CE 206: Engg. Computation sessional
MATLAB code: bisection method function [root,f,ea,iter]=bisect(func,l,u,es,mait) % [root,f,ea,iter]=bisect('func',l,u,es,mait): % uses bisection method to find the root of func % input: % func = name of function % l, u = lower and upper guesses % es = desired relative error (default = 0.0001) % mait = maimum allowable iterations (default = 50) % output: % root = real root % f = function value at root % ea = approimate relative error (%) % iter = number of iterations Comment lines if nargin<3,error('at least 3 input arguments required'),end test = feval('func',l)*feval('func',u); if test>0,error( roots cannot be found'),end if nargin<4 isempty(es), es=0.0001;end if nargin<5 isempty(mait), mait=50;end Checking whether the roots are within these bounds or not CE 206: Engg. Computation sessional
MATLAB code: bisection method (ctd) iter = 0; r = l; ea = 100; while (1) rold = r; r = (l + u)/2; iter = iter + 1; if r ~= 0,ea = abs((r - rold)/r) * 100;end test = feval('func',l)*feval('func',r); if test < 0 u = r; elseif test > 0 l = r; else ea = 0; end if ea <= es iter >= mait,break,end end root = r; f = feval('func',r); The variables that change their values within the code needs to be initialized Calculating appro. error a new new old 100% Stopping criteria CE 206: Engg. Computation sessional
func.m file definition function [value] = func() value = ^3-10*^2+5; Evaluating the function 3 10 2 5 [root,f,ea,iter]=bisect('func',0.6,0.8) root = 0.7346 f = 0.4430 ea = 5.1929e-005 iter = 19 Root of the function after 19 iterations ea<0.0001 was the stopping criterion CE 206: Engg. Computation sessional
Bracketing Methods: Regula Falsi the location of the intercept of the straight line ( r ): r u f ( u )( l u ) f ( l ) f ( u ) Try yourself: Modify the bisect.m function file to incorporate the regula falsi method bisection r = (l + u)/2; Regula Falsi r = u-feval( func,u)*(l-u) /(feval( func,l)-feval( func,u));
Newton-Raphson Method Based on forming the tangent line to the f() curve at some guess, then following the tangent line to where it crosses the -ais. ) ( ) ( 0 ) ( ) ( 1 1 i i i i i i i i f f f f a i1 i i1 100%
MATLAB code: newtraph.m function [root,ea,iter]=newtraph(func,dfunc,r,es,mait) if nargin<3, error('at least 3 input arguments required') end if nargin<4 isempty(es),es=0.0001;end if nargin<5 isempty(mait),mait=50;end iter = 0; while (1) rold = r; Derivative of the function (needs to be defined) r = r - feval('func',r)/feval('dfunc',r); iter = iter + 1; if r ~= 0, ea = abs((r - rold)/r) * 100; end if ea <= es iter >= mait, break, end end root = r; i1 i f ( i ) f ( ) i
Secant Method the derivative approimated by a backward finite divided difference: f ' ( i ) f ( i1 ) f ( i ) i1 i i1 i f ( ) i i1 i f ( i1 ) f ( i ) Note: Requires two initial guesses for f() f( i ) f( i-1 ) i-1 i Eercise: Can you modify newtraph.m function file and make it applicable for the secant method?
Secant Method: Eercise Apply the developed code for the secant method to find the first positive root of f() = sin + cos(1+ 2 ) 1 where is in radians. Use four iterations with initial guesses of (i) 1.0 and 3.0 (ii) 1.5 and 2.5, and (iii) 1.5 and 2.25
MATLAB s fzero function MATLAB s fzero provides the best qualities of both bracketing methods and open methods. Using an initial guess: = fzero(function, 0) [, f] = fzero(function, 0) Location of the root Evaluated at the root Function handle to the function being evaluated Using an initial bracket: Initial guess = fzero(function, [0 1]) [, f] = fzero(function, [0 1]) Initial guesses that bracket the root Using create or edit fzero options using optimset options = optimset('param1',value1,'param2',value2,...)
fzero eample options = optimset( display, iter ); Sets options to display each iteration of root finding process. Other optimset properties: MaIter, TolX [, f] = fzero(@() ^10-1, 0.5, options) Uses fzero to find roots of f()= 10-1 starting with an initial guess of =0.5. MATLAB reports = 1, f = 0 after 35 function counts
MATLAB s roots function Roots of polynomials: For an nth order equation, there are n real or comple roots. n = roots(c) f ( ) a 0 a a a is a column vector containing the roots 1 c is a row vector containing the polynomial coefficients 2 2 n n Eample: Find the roots of f()= 5-3.5 4 + 2.75 3 + 2.125 2-3.875 + 1.25 = roots([1-3.5 2.75 2.125-3.875 1.25])
Eercise 1 20 kips/ft 150 kip-ft Using singularity functions, the shear, bending moment and displacement along the simply supported beam can be epressed as follows: V( ) 20 15 kips 5 ft 2 ft 1 ft 2 ft 0 2 1 20 5 2 15 8 57 M( ) 10 0 10 5 15 8 150 7 57 5 4 5 4 15 2 2 57 3 u( ) 0 5 8 75 7 238. 25 6 6 6 6 Using any root location technique, find the points where shear and bending moment equal to zero and where the displacement is maimum. Also draw the shear force, bending moment and displacement diagrams 1 1 0 0
Eercise 1: solution hints 20 kips/ft 150 kip-ft 15 kips 5 ft 2 ft 1 ft 2 ft V( ) 20 0 1 20 5 1 15 8 0 57 function f = V() f=20*(sing(,0,1)-sing(,5,1))-15*sing(,8,0)-57; In addition, the singularity function can be set up as function s = sing(, a, n) if > a s = ( - a) ^ n; else s = 0; end a n ( a) 0 n when when a a Solution: V() = 0 at = 2.85 ft, M() = 0 at = 5.814 ft, U() = 0 at = 3.9357 ft
Eercise 2 The saturation concentration of dissolved oygen in freshwater can be calculated with the equation: ln O sat 1.57570110 139.34411 T a 5 6.64230810 T O sat = Saturation concentration of DO (mg/l) T a = T + 273.15 where T = temperature ( C) 2 a 7 1.243810 T 3 a 10 8.62194910 T 4 a 11 Fill up the following table (use bisection method): epress your results for an absolute error of 0.05 C O sat (mg/l) Temperature ( C) 8?? 10?? 12??
Eercise 2: solution hints First determine how many iterations you need for the specified absolute error using the formula (see your CE 205 notes): Bracket range 0 40 log n log 2 E a, d 0.05 2 Absolute error 9.6439 10 iterations should be enough Use bisect.m to compute the roots for maimum 10 iterations and also find the real roots using fzero function. Compare results. O sat Approimation Eact Error 8 26.75781 26.78017 0.0224 10 15.35156 15.38821 0.0366 12 7.46094 7.46519 0.0043 Less than 0.05
Concluding remarks Need to be judicious in choosing your initial guess (or initial brackets) of the root. A physical insight into the problem may help Apply common logic Some eamples: - Water level in a spherical tank cannot eceed the diameter - Temperature in natural water bodies usually within a specific range, (e.g. cannot be negative) - deflection of a beam is maimum where the du/d is zero - Water level in a tank cannot eceed tank depth (or cannot be negative) - etc.
Eercise Water is flowing in a trapezoidal channel at a rate of Q = 20 m 3 /s. The critical depth y for such a channel must satisfy the equation: Where g = 9.81 m 2 /s, A c = channel cross-sectional area (m 2 ) = 3y + y 2 /2 and B = width of the channel at the surface (m) = 3 + y. Solve for the critical depth (y) using both bisection and method of false position using the initial guesses 0.5 and 2.5 and iterate until the approimate error falls below 1% or the number of iterations eceed 10. Compare your results from both methods