This paper briefly introduces the Newton-raphson method and its R language implementation and gives several exercises for reference. Download PDF document (academia.edu)
- Newton-raphson Method
Let $f (x) $ is a differentiable function and let $a _0$ is a guess for a solution to the equation $ $f (x) =0$$ We can product A sequence of points $x =a_0, a_1, a_2, \dots $ via the recursive formula $ $a _{n+1}=a_n-\frac{f (a_n)}{f ' (A_n)}$$ that is s uccessively better approximation of a solution to the equation $f (x) =0$.
- R Codes There is 4 parameters in this function:
- f is the function of you input.
- Tol is the tolerance (default $1e-7$).
- x0 is the initial guess.
- N is the default number of iterations.
The process would be the end up until either the absolute difference between the adjacent approximations are less than tol c6>, or the number of iterations reaches N.
- Examples
Generally speaking, the "guess" is important. More precisely, according to intermediate value theorem we can find both values of which function Value is larger and less than 0, respectively. Then choosing the one, which first derivative are larger than another, as the initial guess value in the iterative formula. This process would guarantee the convergence of roots. Let's see some examples.
- example 1
Approximate the fifth root of 7.
solution:
denote $f (x) =x^5-7$. It is easily to know that $f (1) =-6 < 0$ and $f (2) =25 > 0$. Additionally, $f ' (1) =5 < F ' (2) =80$, so we set the initial guess value $x _0=2$. By Newton-raphson method We get the result is 1.47577316159. and $ $f (1.47577316159) \approx 1.7763568394e-15$$ which is very close to 0. R codes is below: # Example 1f = function (x) {x^5-7}h = 1E-7DF.DX = function (x) {(f (x + H)-f (x))/H}DF.DX (1); DF. DX (2) # [1] 5.0000009999# [1] 80.0000078272app = Newton (f, x0 = 2) app# [1] 1.68750003057 1.52264459615 1.47857137506 1.4757 8373325 1.47577316175# [6] 1.47577316159f (App[length (APP)) # [1] 1.7763568394e-15
- example 2
The function $f (x) =x^5-5x^4+5x^2-6$ has a root Between 1 and 5. Approximate it by Newton-raphson method.
solution:
We try to calculate some values first. $f (1) =-5, F (2) =-34, F (3) =-123, F (4) =-182, F (5) =119$, so there should is a root between 4 and 5. Since $f ' (4) =40 < F ' (5) =675$, hence $x _0=5$ is a proper initial guess value. By Newton-raphson method We get the result was 4.79378454069 and $ $f (4.79378454069) \approx-2.84217094304e-14$$ which is a Desired approximation. R codes is below: # Example 2f = function (x) {x^5-5 * x^4 + 5 * x^2-6}x = C (1:5) F (x) # [1] -5-34-123-182 11 9h = 1E-7DF.DX = function (x) {(f (x + H)-f (x))/H}DF.DX (4); DF.DX (5) # [1] 40.0000163836# [1] 675.000053008app = Newton (f, x0 = 5) app# [1] 4.82370371755 4.79453028339 4.79378501861 4. 79378454069 4.79378454069f (App[length (APP)) # [1] -2.84217094304e-14
- Example 3
A rectangular piece of cardboard of dimensions $8\times 17$ is used-make an open-top box by cutting out a small square of side $x $ from all corner and bending up the sides. Find a value of $x $ for which the box has volume 100.
Solution:
Firstly, building the model. $V (x) =x (8-2x) (17-2x) =100$, that's, we want to find the root of Equation $ $f (x) =x (8-2x) (17-2x) -100=0\leftrightarrow f (x) =4 x^3-50x^2+136x-100=0$$ We know, $ < x < 4$ and hence try to calculate some non-negative integers: $ $f (0) =-100, F (1) =-10, F (2) =4, F (3) =-34, F (4) =-100$$ Note that there is the intervals may has roots: $ (1, 2) \cup (2,3) $. Since $ $f ' (1) =48 > F ' (2) =-16 > F ' (3) =-56$$ so we set the initial guess values $x _0=1$ and $x ' _0=2$ (i.e. there is t Wo separate iteration procedures). By using Newton-raphson method we obtain the result is 11.26063715644 and 2.19191572127 respectively. Both of them is quite accurate. R codes is below:# Example 3f = function (x) {4 * x^3-50 * x^2 + 136 * x-100}x = C (0:4) f (x) # [1] -100 -10 4 -34-100h = 1e- 7DF.DX = function (x) {(f (x + H)-f (x))/H}DF.DX (1); DF.DX (2); DF.DX (3) # [1] 47.9999962977# [1] -16.0000024607# [1] -56.0000012229app1 = Newton (f, x0 = 1) app2 = Newton (f, x0 = 2) App1; app2# [1] 1.20833334940 1.25768359879 1.26062673622 1.26063715631 1.26063715644# [1] 2.24999996155 2.19469026652 2.19192282154 2.19191572132 2.19191572127f (App1[length (APP1)]); F (App2[length (APP2)]) # [1] 2.84217094304e-14# [1] -2.84217094304e-14
Introduction of Newton-raphson algorithm and its R implementation