Está en la página 1de 20

Root Finding Convergence Analysis

Justin Ross & Matthew Kwitowski November 5, 2012

There are many dierent ways to calculate the root of a function. Some methods are direct and can be done by simply solving for x, while other methods require multiple steps and the results contain some error. In this paper, we will focus on four methods that require multiple steps and we will compare how eective each method is.

Contents
1 Introduction 2 Root Finding Methods 2.1 The Bisection Method 2.2 Fixed-Point Iteration . 2.3 Newtons Method . . . 2.4 Halleys Method . . . 3 Results 4 Conclusion 5 Bibliography 6 Appendix 6.1 Bisection Method Matlab Code . . 6.2 Fixed Point Iteration Matlab Code 6.3 Newtons Method Matlab Code . . 6.4 Halleys Method Matlab Code . . . 6.5 Maple Code . . . . . . . . . . . . . 3 3 3 4 5 6 7 10 11 12 12 14 16 18 20

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

Introduction

Our goal is to examine the convergence of various root-nding methods. Specically, we will examine how x + ln(x) = 0, x > 0 converges using the Bisection Method, Fixed Point Iteration, Newtons Method, and Halleys Method. We need to ensure eight digits of accuracy so that: |xc r| < .5 108 where r is the real root and xc is the estimated root. We will compare the rate of convergence for each method as well as compare the time and number of steps required to compute the approximate root. We will also discuss when it is appropriate to use each of the four methods. In order to know if we have reached our desired accuracy, we must rst gure out the real root of the function. Using Maple 11s built in function fsolve, we nd the approximate root of x +ln(x) = 0 to be 0.5671432904. We will assume this to be the most accurate root and compare our ndings to it.

2
2.1

Root Finding Methods


The Bisection Method

The Bisection Method is a slow, but certain, way to nd a root of a function if the root exists. We can nd the general area of the root by bracketing it on the interval [a, b] R so that there exists a pair f (a), f (b) where f (a)f (b) < 0. In Sauers Numerical Analysis, Theorem 1.2 states that if f is a continuous function on [a, b], satisfying f (a)f (b) < 0 then f has a root between a and b. This means there exists a number r R such that a < r < b and f (r) = 0. After each step in the bisection method, we can rene our guess to be the midpoint between the interval [a, b] around the root. We can check the sign of the function evaluated at this midpoint and then rene our bracket around the root further. This gives us a new interval that contains r. We continue this process until we are satised with our accuracy or we have reached a maximum number of iterations. Sauer also states that after n steps of the Bisection Method the error term is given by |xc r| < ba . 2n+1 (1)

We can use equation (1) to determine the number of iterations required for a given accuracy and initial guesses a, b. Letting |xc r|, we see that ab 2n+1

< ab 2 log( ab ) 1 log(2) 3

2n > n >

(2)

From equation (2), we nd that the number of iterations, n, depends on both our initial interval length and desired accuracy.

2.2

Fixed-Point Iteration

The xed-point method is a linearly convergent method. We dene a xed point of a function g if g (r) = r. Using the properties of exponents and logarithms we can rewrite the given problem as x + ln(x) = 0 ln(x) = x x = ex and dene our xed-point equation as g (x) = The Fixed Point iterative method is given by 1 . ex (3)

x0 = initial guess xi+1 = g (xi ) for i = 0, 1, 2....

If g is a continuous function and the {xi } converge to a number r then r is a xed point. So if g is continuous and the {xi } converge then g (r) = g ( lim xi ) = lim g (xi ) = lim xi+1 = r.
i+ i+ i+

Sauer also states in theorem 1.6 that if S = |g (r)| < 1 then the xed-point iterations converge linearly with rate S to the xed point r for initial guesses suciently close to r. 1 1 | = | 0.56714329 | 0.5671 (4) r e e We will be able to test this against our resulting error ratio eie+1 where ei = |r xi | in our i convergence analysis. S = |g (r)| = | The xed point iterative method forms a cobweb diagram geometrically. Figure 1 shows the cobweb diagram for g (x) = e1 x and the rst few steps with an initial guess x0 = 0.4 With this initial guess, we nd that we get closer to the xed point after each step and spiral inward closer to the root.

Figure 1: The xed point is the intersection of g (x) and the diagonal line.

2.3

Newtons Method

Newtons Method requires just one initial guess. It is also a specic form of the xed point method. The general method is x0 = initial guess f (xi ) xi+1 = xi for i = 0, 1, 2.... f (xi )

It is important to note that the rst derivative is required to compute the root using Newtons Method. This can slow the overall computation time if derivative is dicult to compute. The time required to calculate the root will be reduced if the derivative is known. Using Newtons Method we expect quadratic convergence when the initial guess is suciently close to the real root. At the very least, we expect faster convergence than the linearly convergent x methods. Since f (x) = 1+ x , the formula for Newtons Method is given by xi+1 = xi xi + ln(xi ) . xi + x2 i

2.4

Halleys Method

Halleys Method is a root nding method that converges cubically. In order to use Halleys method, the function must have a continuous second derivative. The general method is

x0 = initial guess xi+1 = xi 2f (xi )f (xi ) , for i = 0, 1, 2.... 2[f (xi )]2 f (xi )f (2) (xi )

Similarly to Newtons Method and Fixed Point Iteration, Halleys Method involves starting with an initial guess close to the root and then substituting it into the equation to get the next guess, and then repeating until you are satised with the degree of accuracy. For the function f (x) = x +ln(x), the formula for Halleys method is xi+1 = xi + ln(xi ) 2(xi + ln(xi ))(1 + 2(1 +
1 2 xi ) 1 xi )
i

1 + (xi + ln(xi ))( x 2)

Results

From the Bisection method and from equation (2) we expect the number of steps, n, required to approximate the root to be 1 log( .510 8 ) n 1 26.57. log(2) We chose to bracket the root around [0.1, 1.1] as our initial guess. Using Matlab, we performed a convergence analysis and found that 27 steps were required to converge to the root, which was expected. The midpoint is the current steps best approximation to the root. Notice the initial approximation to the root (the rst midpoint between [a, b] is already accurate to one decimal place.
Step 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 Left Guess 0.100000000 0.100000000 0.350000000 0.475000000 0.537500000 0.537500000 0.553125000 0.560937500 0.564843750 0.566796875 0.566796875 0.566796875 0.567041016 0.567041016 0.567102051 0.567132568 0.567132568 0.567140198 0.567140198 0.567142105 0.567143059 0.567143059 0.567143059 0.567143178 0.567143238 0.567143267 0.567143282 Midpoint 0.600000000 0.350000000 0.475000000 0.537500000 0.568750000 0.553125000 0.560937500 0.564843750 0.566796875 0.567773438 0.567285156 0.567041016 0.567163086 0.567102051 0.567132568 0.567147827 0.567140198 0.567144012 0.567142105 0.567143059 0.567143536 0.567143297 0.567143178 0.567143238 0.567143267 0.567143282 0.567143290 Right Guess 1.100000000 0.600000000 0.600000000 0.600000000 0.600000000 0.568750000 0.568750000 0.568750000 0.568750000 0.568750000 0.567773438 0.567285156 0.567285156 0.567163086 0.567163086 0.567163086 0.567147827 0.567147827 0.567144012 0.567144012 0.567144012 0.567143536 0.567143297 0.567143297 0.567143297 0.567143297 0.567143297 Error 0.032856710 0.217143290 0.092143290 0.029643290 0.001606710 0.014018290 0.006205790 0.002299540 0.000346415 0.000630147 0.000141866 0.000102275 0.000019796 0.000041240 0.000010722 0.000004537 0.000003093 0.000000722 0.000001185 0.000000232 0.000000245 0.000000007 0.000000112 0.000000053 0.000000023 0.000000008 0.000000001

Figure 2: Convergence table for the Bisection Method

Using the xed-point equation g (x) = ex and the initial guess of 0.4, we performed a convergence analysis for our function f (x) = x + ln(x). We see that this takes 31 steps to converge at the rate S = 0.56714. This conrms what we found in equation (3). This implies a linear rate of convergence. We also note that each xi root approximation is a point given by the cobweb diagram in Figure (1) as it spirals closer to the root.
i 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 xi 0.40000000 0.67032005 0.51154483 0.59956863 0.54904843 0.57749908 0.56130038 0.57046676 0.56526154 0.56821152 0.56653778 0.56748681 0.56694850 0.56725378 0.56708063 0.56717883 0.56712314 0.56715472 0.56713681 0.56714697 0.56714121 0.56714447 0.56714262 0.56714367 0.56714307 0.56714341 0.56714322 0.56714333 0.56714327 0.56714330 0.56714328 0.56714329 g(xi) 0.67032005 0.51154483 0.59956863 0.54904843 0.57749908 0.56130038 0.57046676 0.56526154 0.56821152 0.56653778 0.56748681 0.56694850 0.56725378 0.56708063 0.56717883 0.56712314 0.56715472 0.56713681 0.56714697 0.56714121 0.56714447 0.56714262 0.56714367 0.56714307 0.56714341 0.56714322 0.56714333 0.56714327 0.56714330 0.56714328 0.56714329 0.56714329 ei=|xi-r| 0.16714329 0.10317676 0.05559846 0.03242534 0.01809486 0.01035579 0.00584291 0.00332347 0.00188175 0.00106823 0.00060552 0.00034352 0.00019479 0.00011048 0.00006266 0.00003554 0.00002015 0.00001143 0.00000648 0.00000368 0.00000209 0.00000118 0.00000067 0.00000038 0.00000022 0.00000012 0.00000007 0.00000004 0.00000002 0.00000001 0.00000001 0.00000000 ei/ei-1 0.61729523 0.53886611 0.58320573 0.55804697 0.57230557 0.56421679 0.56880341 0.56620189 0.56767724 0.56684048 0.56731503 0.56704589 0.56719853 0.56711196 0.56716106 0.56713321 0.56714901 0.56714005 0.56714513 0.56714225 0.56714388 0.56714296 0.56714348 0.56714318 0.56714335 0.56714326 0.56714331 0.56714328 0.56714329 0.56714329 0.56714328

Figure 3: Convergence table for the Fixed-Point Method with xed point g (x) = ex

log(x) Another xed point iteration is given as g2 (x) = x . We found that using an initial guess x+1 of 0.4 g2 (x) converged in 64 steps as opposed to the 31 steps from g (x). The convergence analysis for g2 (x) showed an error ratio S = 0.7632. This shows that g2 (x) converges slower than g (x) which is also conrmed by the number of steps taken to converge. Other derivations of g (x) from f (x) = x + ln(x) may exist that give faster or slower convergence. In fact, Newtons Method is a specic form of xed point iteration.

Newtons Method converges with much fewer steps than the Bisection Method and the Fixed Point Method. We found that it converged to our root quadratically in just 4 steps as seen in Figure 4.

Figure 4: Convergence table for Newtons Method

While Newtons Method takes considerably fewer steps than the Fixed Point method and the Bisection Method, we found that, on average, our xed point method for g (x) took 0.02 seconds to converge. However, our Newtons Method took 0.03 seconds. We believe this is because the method takes time to calculate derivative f (x). So while Newtons Method converges in fewer steps, more time may be taken to determine the derivative of the function. When the derivative is known, Newtons Method converged in just 0.001 seconds where the bisection method converged in 0.007 seconds. Halleys Method converges cubically, requiring only three steps as seen in Figure 5. We nd that since both the rst and second derivative of f (x) is required that, while this method converges in very few steps, it takes, on average, 0.055 seconds. This is longer than each of the other methods. However, when the derivatives are known, Halleys Method takes 0.002 seconds which is slightly longer than when the derivatives were known for Newtons Method. This is most likely due to the added complexity of the iterative method with Halleys Method. Each step of Halleys Method requires more addition and multiplication computations to calculate the next xi+1 .

Figure 5: Convergence table for Halleys Method

Conclusion

As we have seen, each method has its advantages and disadvantages. The Bisection Method requires previous knowledge of where the root is to bracket the root around an interval [a, b]. While, it takes fewer steps compute the root than the Fixed Point Iteration, our initial approximate root was also closer to the real root. With the Bisection Method we are also guaranteed convergence as long as the root exists within our interval and the function is continuous on the interval.

Figure 6: Summary comparison of the number of steps and time required to compute the root for f (x) = x + ln(x) for each method.

The Fixed Point method can be dicult to compute if the function g (x) is dicult to nd. We saw that dierent functions g (x) of our given problem f (x) = x + ln(x) also converged at dierent rates. With the Fixed Point Iteration, not only is convergence not guaranteed, but it might also not be very fast. Newtons method was shown to be the best method for nding the root of f (x) = x + ln(x). It took more time to converge than the Bisection Method and Fixed Point Method when the derivative needed to be calculated, but when derivative is known it converged to the root in less time than each other method and with only one more step than the cubically converging Halleys Method. While Halleys method converged at the fastest rate with the least amount of steps, we found that the complexity behind the iterative method added more time to calculate the root. Even when the rst and second derivative was known, it took more time to compute than Newtons Method as we see in gure 6.

10

Bibliography

References
[1] T. Sauer, Numerical Analysis: Second Edition, Pearson Education, 2012, pp. 2455. [2] P. Acklam, A small paper on Halleys method, 23 December 2002, http://home.online.no/ pjacklam/notes/halley/halley.pdf, 26 October 2012

11

6
6.1
1 2 3 4 5 6

Appendix
Bisection Method Matlab Code

%B i s e c t i o n Method function [ Z ] = b i s e c t i o n ( f , a , b , t o l , maxIter )

%INPUTS % Inline 7 % a will 8 % b will 9 % tol is


10 11 12 13 14 15 17

function f g i v e us a n e g a t i v e r e s u l t when i n f g i v e us a p o s i t i v e r e s u l t when i n f t h e e r r o r we can be w i t h i n ( 0 . 0 0 0 0 0 0 0 0 5 f o r 8 d i g i t a c c u r a c y )

% OUTPUTS % Z i s a m a t r i c c o n t a i n n g each g u e s s and e r r o r

%Real r o o t o f t h e s u p p l i e d f u n c t i o n %f = x + l n ( x ) = 0 16 r e a l R o o t = 0 . 5 6 7 1 4 3 2 9 0 4 0 9 7 8 4 ; % Z contains the x a x i s values to check fo r roots % l e n g t h i s max number o f i t e r a t i o n s 20 Z = zeros ( 1 , 4 ) ;


18 19 21

%I n i t i a l l e f t g u e s s s t a r t i n g p o i n t 23 Z ( 1 , 1 ) = a ;
22 24

%F i r s t Midpoint v a l u e t o c h e c k 26 Z ( 1 , 2 ) = ( a+b ) / 2 ;
25 27 28 29 30 31 32 33 34 35

%F i r s t r i g h t g u e s s s t a r t i n g p o i n t Z(1 ,3) = b ; %I n i t i a l e r r o r Z ( 1 , 4 ) = abs ( Z ( 1 , 2 ) r e a l R o o t ) ;

%I f s t a r t i n g p o i n t a r e t h e same s i g n t h e n b r e a k program i f sign ( f ( Z ( 1 , 1 ) ) ) sign ( f ( Z ( 1 , 3 ) ) ) >= 0 36 error ( f ( a ) and f ( b ) a r e both t h e same s i g n ) 37 end
38 39 40 41 42

%k i s t h e c o u n t e r k = 1;

%End l o o p i f m i d p o i n t g u e s s i s w i t h i n t h e t o l e r a n c e or we have r e a c h e d the 43 %maximum number o f i t e r a t i o n s 12

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

while (Z( k , 4 ) > t o l && k < maxIter ) %I f t h e m i d p o i n t has t h e same s i g n as t h e l e f t p o i n t t h e n t h a t %m i d p o i n t becomes t h e new l e f t point i f sign ( f ( Z ( k , 2 ) ) ) == sign ( f ( Z ( k , 1 ) ) ) Z ( k +1 ,1) = Z ( k , 2 ) ; %S e t s l e f t p o i n t t o t h e m i d p o i n t Z ( k +1 ,3) = Z ( k , 3 ) ; %S e t s t h e r i g h t p o i n t t o t h e o l d r i g h t p o i n t else Z ( k +1 ,3) = Z ( k , 2 ) ; %S e t s t h e r i g h t p o i n t t o t h e m i d p o i n t Z ( k +1 ,1) = Z ( k , 1 ) ; %S e t s t h e l e f t p o i n t t o t h e o l d l e f t p o i n t end %New Midpoint t o c h e c k on x a x i s Z( k +1 ,2) = ( Z ( k +1 ,1) + Z( k +1 ,3) ) / 2 ; %Error i s t h e d i s t a n c e from t h e c u r r e n t m i d p o i n t g u e s s and t h e r e a l root Z( k +1 ,4) = abs ( r e a l R o o t Z ( k +1 ,2) ) ; %Error r a t i o %Z( k +1 ,5) = Z( k +1 ,4) /Z( k , 4 ) ; k = k+1; %I n c r e a s e c o u n t e r by 1 end %F i n a l Midpoint g u e s s xc = Z( k , 2 ) ;

13

6.2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 32 33

Fixed Point Iteration Matlab Code

%F i x e d P o i n t I t e r a t i o n S o l v e r function Z = f i x e d P o i n t ( g , a , t o l , maxIter ) %INPUTS % Inline function g % a i s the i n i t i a l guess % t o l i s t h e t o l e r a n c e we can be w i t h i n ( 0 . 0 0 0 0 0 0 0 0 5 f o r 8 d i g a c c u r a c y % m a x I t e r i s t h e maximum numbre o f i t e r a t i o n s a l l o w e d % OUTPUTS %Z i s a m a t r i x %Z( x , 1 ) i s t h e %Z( x , 2 ) i s t h e %Z( x , 3 ) i s t h e %Z( x , 4 ) i s t h e

where : x axis guess resulting function value e r r o r d i s t a n c e b e t w e e n t h e g u e s s and t h e r e a l r o o t error ratio

%Real r o o t o f t h e s u p p l i e d f u n c t i o n %f = x + l n ( x ) = 0 %g = ( x x l o g ( x ) ) / ( x+1) %h = 1/ e x realRoot = 0.567143290409784; %TEST ROOT FOR x + c o s ( x ) s i n ( x ) %r e a l R o o t = 0 . 7 8 5 3 9 8 2 ; %I n i t i a l i z e Z Z = zeros ( 1 , 4 ) ;

%S t a r t t h e Counter 31 k = 1 ; %The i n i t i a l g u e s s 34 Z ( 1 , 1 ) = a ;
35

%The i n i t i a l e v a l u a t i o n a t our g u e s s 37 Z ( 1 , 2 ) = g ( Z ( 1 , 1 ) ) ;
36 38 39 40 41 42 43 44 45 46

%I n i t i a l e r r o r Z ( 1 , 3 ) = abs ( Z ( 1 , 1 ) r e a l R o o t ) ; %There i s no i n i t i a l e r r o r r a t i o while (Z( k , 3 ) > t o l && Z ( k , 3 ) = 0 ) %Needs work when e r r o r = 0 i f isnan ( Z ( k , 1 ) ) | | isnan ( Z ( k , 2 ) ) | | isnan ( Z ( k , 3 ) ) 14

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

error ( Fixed Po i nt d o e s not c o n v e r g e ) end %Break i f max number o f i t e r a t i o n s r e a c h e d i f k > maxIter break end k = k + 1 ; %Increment t h e co u nt %Z( k , 1 ) = 1 ; %Update t h e co un t p l a c e h o l d e r Z( k , 1 ) = g ( Z ( k 1 ,1) ) ; %C r e a t e n e x t g u e s s Z( k , 2 ) = g ( Z ( k , 1 ) ) ; %E v a l u a t e t h e n e w e s t g u e s s Z( k , 3 ) = abs ( Z ( k , 1 ) r e a l R o o t ) ; %Determine t h e e r r o r Z( k , 4 ) = Z ( k , 3 ) /Z ( k 1 ,3) ; %Determine t h e e r r o r r a t i o %I f Error r a t i o i s g r e a t e r than 1 i t w i l l not c o n v e r g e i f Z( k , 3 ) > Z ( k 1 ,3) 2 && k > 2 error ( Fixed Po i nt d o e s not c o n v e r g e ) %ends program end end

15

6.3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

Newtons Method Matlab Code

%Newtons Method function Z = newtons ( f , a , t o l , maxIter , var ) %INPUTS % NOT INLINE f u n c t i o n f % a i s the i n i t i a l guess % t o l i s t h e t o l e r a n c e we can be w i t h i n ( 0 . 0 0 0 0 0 0 0 0 5 f o r 8 d i g a c c u r a c y % m a x I t e r i s t h e maximum number o f i t e r a t i o n s a l l o w e d % var i s the v a r i a b l e s u p p l i e d in the i n l i n e f u n c t i o n %OUPUTS % Z i s a matrix c o n s i s t i n g of : %Z( x , 1 ) i s t h e x a x i s g u e s s %Z( x , 2 ) i s t h e r e s u l t i n g f u n c t i o n v a l u e %Z( x , 3 ) i s t h e e r r o r r a t i o %Make t h e v a r i a b l e used a symbol syms ( var , r e a l ) ; tic f D i f f = d i f f ( f ) ; %Take f i r s t d e r i v a t i v e o f f f D i f f = i n l i n e ( f D i f f ) ; %Make f i r s t d e r i v a t i v e an i n l i n e f u n c t i o n f I n l i n e = i n l i n e ( f ) ; %Make s u p p l i e d f u n c t i o n i n l i n e

%Real r o o t o f t h e s u p p l i e d f u n c t i o n 26 %f = x + l n ( x ) = 0 27 %D e r i v a t i v e o f f = 1 + 1/ x 28 r e a l R o o t = 0 . 5 6 7 1 4 3 2 9 0 4 0 9 7 8 4 ;
29

%TEST ROOT FOR x + c o s ( x ) s i n ( x ) 31 %r e a l R o o t = 0 . 7 8 5 3 9 8 2 ;


30 32

%I n i t i a l i z e Z 34 Z = zeros ( 1 , 3 ) ;
33 35

%S t a r t t h e Counter 37 k = 1 ;
36 38 39 40 41 42 43 44 45 46

%The i n i t i a l g u e s s Z(1 ,1) = a ; %I n i t i a l e r r o r Z ( 1 , 2 ) = abs ( Z ( 1 , 1 ) r e a l R o o t ) ; %No I n i t i t a l e r r o r r a t i o

16

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

while (Z( k , 2 ) > t o l && k < maxIter && Z ( k , 2 ) = 0 ) %Crea t e t h e n e x t g u e s s Z( k +1 ,1) = Z ( k , 1 ) f I n l i n e ( Z ( k , 1 ) ) / f D i f f ( Z ( k , 1 ) ) ; %Deterimne e r r o r o f t h e newly c r e a t e d g u e s s Z( k +1 ,2) = abs ( Z ( k +1 ,1) r e a l R o o t ) ; %Determine t h e e r r o r r a t i o Z( k +1 ,3) = Z ( k +1 ,2) /Z ( k , 2 ) 2 ; %Determine t h e e r r o r r a t i o %I n c r e a s e t h e c o u n t e r k = k+1; end

17

6.4
1 2 3 4 5 6 7 8 9 10 11 12 14 15

Halleys Method Matlab Code

%H a l l e y s Method function Z = h a l l e y ( f , a , t o l , maxIter , var ) %INPUTS % NOT INLINE f u n c t i o n f % a i s the i n i t i a l guess % t o l i s t h e t o l e r a n c e we can be w i t h i n ( 0 . 0 0 0 0 0 0 0 0 5 f o r 8 d i g a c c u r a c y % m a x I t e r i s t h e maximum number o f i t e r a t i o n s a l l o w e d % var i s the v a r i a b l e s u p p l i e d in the i n l i n e f u n c t i o n

%Make t h e v a r i a b l e used a symbol 13 syms ( var , r e a l ) ; f i r s t D = d i f f ( f ) ; %F i r s t d e r i v a t i v e o f f 16 secondD = d i f f ( f i r s t D ) ; %Second d e r i v a t i v e o f f


17 18

f i r s t D = i n l i n e ( f i r s t D ) ; %F i r s t D e r i v a t i v e o f s u p p l i e d f u n c t i o n , i n l i n e 19 secondD = i n l i n e ( secondD ) ; %Second D e r i v a t i v e o f s u p p l i e d f u n c t i o n , inline 20 f = i n l i n e ( f ) ; %The s u p p l i e d f u n c t i o n c o v n e r t e d t o i n l i n e


21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 39 40 41 42 43 44

%Real r o o t o f t h e s u p p l i e d f u n c t i o n %f = x + l n ( x ) = 0 %D e r i v a t i v e o f f = 1 + 1/ x %Second D e r i v a t i v e o f f = 1/x 2 realRoot = 0.567143290409784; %I n i t i a l i z e Z Z = zeros ( 1 , 3 ) ; %S t a r t t h e Counter k = 1; %The i n i t i a l g u e s s Z(1 ,1) = a ;

%I n i t i a l e r r o r 38 Z ( 1 , 2 ) = abs ( Z ( 1 , 1 ) r e a l R o o t ) ; %No I n i t i t a l e r r o r r a t i o while (Z( k , 2 ) > t o l && k < maxIter && Z ( k , 2 ) = 0 ) %Crea t e t h e n e x t g u e s s

18

45 46 47 48 49 50 51 52 53 54 55 56

Z( k +1 ,1) = Z ( k , 1 ) [ 2 f ( Z ( k , 1 ) ) f i r s t D ( Z ( k , 1 ) ) ] / [ ( 2 ( f i r s t D ( Z ( k , 1 ) ) ) 2 ) f ( Z ( k , 1 ) ) secondD ( Z ( k , 1 ) ) ] ; %Deterimne e r r o r o f t h e newly c r e a t e d g u e s s Z( k +1 ,2) = abs ( Z ( k +1 ,1) r e a l R o o t ) ; %Determine t h e e r r o r r a t i o Z( k +1 ,3) = Z ( k +1 ,2) /Z ( k , 2 ) 3 ; %Determine t h e e r r o r r a t i o %I n c r e a s e t h e c o u n t e r k = k+1; end

19

6.5

Maple Code

func := x + ln(x) fsolve(func) = 0.5671432904

20

También podría gustarte