Presentation is loading. Please wait.

Presentation is loading. Please wait.

Numerical Methods. Read xl and xu Define the function f(xl)f(xu) : 0 xu = xm Find xm=(xl+xu)/2 Read ε(limit) & max_iteration f(xl)f(xm):0 Ea = |(xm-xmold)*100/xm|

Similar presentations


Presentation on theme: "Numerical Methods. Read xl and xu Define the function f(xl)f(xu) : 0 xu = xm Find xm=(xl+xu)/2 Read ε(limit) & max_iteration f(xl)f(xm):0 Ea = |(xm-xmold)*100/xm|"— Presentation transcript:

1 Numerical Methods

2 Read xl and xu Define the function f(xl)f(xu) : 0 xu = xm Find xm=(xl+xu)/2 Read ε(limit) & max_iteration f(xl)f(xm):0 Ea = |(xm-xmold)*100/xm| xl = xm Ea : ε (limit) Iteration_count : max_iteration Iteration_count++ Print Count, xl, xu, xm, f(xl), f(xm) f(xl)f(xm) & error No convergence Start < > < > > < < > Stop 2 iteration_count=0 xmold = xl Xmold=xm =

3 % Bisection method for finding the root of a nonlinear equation clc; clear all; f + 6 * x^2 - 7 * x - 60); while 1 xl = input('Enter the value of xl: '); xu = input('Enter the value of xu: '); if (f(xl)*f(xu)<0) disp('The xl and xu encompass atleast one root'); break; end disp ('The xl and xu do not encompass the root'); disp ('Enter a new set'); end maxcount = input('Enter the value of maximum number of count: '); eps = input('Enter the value of epsilon (%): '); count=0; xmold=xl; disp( ' Count Xl Xu Xm f(Xl) f(Xm) fXl*fXm % Error ') 3

4 while counteps disp ('no convergence'); end

5 f(x) = x^3 – 4 * x ^2 – x + 4 x l =3.5 and x u = 4.8 RESULT Iteration x m % Error f(x m )

6  f(x) = 5 * exp ( -x) -2  Assume x l =0 and x u =2  Calculate x m, % error and f(x m ) for three iterations 6 Iteration X m % Error f(x m )

7 7 Figure 1

8  However, in the example shown in Figure 1, the bisection method may not be efficient because it does not take into consideration that f(x L ) is much closer to the zero of the function f(x) as compared to f(x U )  In other words, the next predicted root x r would be closer to x L (in the example as shown in Figure 1), than the mid-point between x L and x U  The false-position method takes advantage of this observation mathematically by drawing a secant from the function value at x L to the function value at x U and estimates the root as where it crosses the x-axis. 8

9  Based on two similar triangles, shown in figure 1, one gets (4)  From equation (4), one obtains  The above equation can be solved to obtain the next predicted root x r as (5) 9

10  The equation (5), through simple algebraic manipulations, can also be expressed as (6) or (7). 10

11 The steps to apply the false-position method to find the root of the equation f(x)=0 are as follows.  Step#1: Choose x L and x U as two guesses for the root such that or in other words f(x) changes sign bet n x L and x U  Step#2: Estimate the root x r of the equation f(x)=0 as  Step #3. Now check the following  If then the root lies between x L and x r  If then the root lies between x U and x r  If then the root is x r. Stop the algorithm. 11

12  Step #4: Find the new estimate of the root  Find the absolute relative approximate error as  where x r new = estimated root from present iteration x r old = estimated root from previous iteration 12

13  Step # 5. Compare the absolute relative approximate error with the pre-specified relative error tolerance.  If then go to step 3, else stop the algorithm. Note one should also check whether the number of iterations is more than the maximum number of iterations allowed. If so, one needs to terminate the algorithm and notify the user about it.  Note that the false-position and bisection algorithms are quite similar. The only difference is the formula used to calculate the new estimate of the root as shown in steps #2 and #4! 13

14 IterationxLxL xUxU xrxr f(x m ) X X X10 -7 Table 1 Root of floating ball problem for false-position method 14

15  Find the root of using the initial guesses of x L =-2.5 and x U =-1.0 and a pre-specified tolerance of Table 2 Root of for false-position method IterationxLxL xUxU f(x L )f(x U )xrxr f(x r ) N/A

16 Read x l and x u Define the function f(x l )f(x u ) : 0 x u = x i x i = [x u f(x l )-x l f(x u ]/ [f(x l )-f(x u ) ] A Read E limit & max_iteration f(x l )f(x i ):0 E a = |(x i -x iold )*100/x i | x l = x i E a : E limit Iteration_count : max_iteration Iteration_count++ Print Count, x i & f(x i ) No convergence Start < > < > > < < > Stop 16 iteration_count=0 x iold = x l X iold= x i =

17 clear all; f (x) (x^2 - 5*x + 6); while 1 xl = input('Enter the value of xl: '); xu = input('Enter the value of xu: '); if (f(xl)*f(xu) < 0) disp('The xl and xu encompass atleast one root'); break; end disp ('The xl and xu do not encompass the root’); disp(‘Enter a new set'); end maxcount = input('Enter the value of maximum number of count: '); eps = input('Enter the value of epsilon (%): '); count = 0; xiold = xl; disp( ' Count Xi Error f(xi)'); while count < maxcount xi=(xu*f(xl) –xl*f(xu))/ (fxl)-f(xu)) ; if (f(xl)*f(xi) < 0) xu = xi; else xl = xi; end err=abs(xi - xiold)*100/xi); xmold = xi; Y(1) = count; Y(2) = xi; Y(3) = err; Y(4)=f(xi); disp (Y); if (abs(f(xl)*f(xi))<1.0e-10) break; end if err < eps break; end count = count+1; end if count == maxcount && err>eps disp ('no convergence'); end 17

18 18


Download ppt "Numerical Methods. Read xl and xu Define the function f(xl)f(xu) : 0 xu = xm Find xm=(xl+xu)/2 Read ε(limit) & max_iteration f(xl)f(xm):0 Ea = |(xm-xmold)*100/xm|"

Similar presentations


Ads by Google