Given a function f(x) on floating number x and an initial guess for root, find root of function in interval. Here f(x) represents algebraic or transcendental equation.

For simplicity, we have assumed that derivative of function is also provided as input.

Example:

Input: A function of x (for example x3 – x2 + 2),
derivative function of x (3×2 – 3x for above example)
and an initial guess x0 = -20
Output: The value of root is : -1.00
OR any other value close to root.
We have discussed below methods to find root in set 1 and set 2
Set 1: The Bisection Method
Set 2: The Method Of False Position

Comparison with above two methods:

In previous methods, we were given an interval. Here we are required an initial guess value of root.
The previous two methods are guaranteed to converge, Newton Rahhson may not converge in some cases.
Newton Raphson method requires derivative. Some functions may be difficult to
impossible to differentiate.
For many problems, Newton Raphson method converges faster than the above two methods.
Also, it can identify repeated roots, since it does not look for changes in the sign of f(x) explicitly
The formula:
Starting from initial guess x1, the Newton Raphson method uses below formula to find next value of x, i.e., xn+1 from previous value xn.

nrm

Algorithm:
Input: initial x, func(x), derivFunc(x)
Output: Root of Func()

  1. Compute values of func(x) and derivFunc(x) for given initial x
  2. Compute h: h = func(x) / derivFunc(x)
  3. While h is greater than allowed error ε
    1. h = func(x) / derivFunc(x)
    2. x = x – h
[ad type=”banner”]

Below is C++ implementation of above algorithm.

[pastacode lang=”cpp” manual=”%2F%2F%20C%2B%2B%20program%20for%20implementation%20of%20Newton%20Raphson%20Method%20for%0A%2F%2F%20solving%20equations%0A%23include%3Cbits%2Fstdc%2B%2B.h%3E%0A%23define%20EPSILON%200.001%0Ausing%20namespace%20std%3B%0A%20%0A%2F%2F%20An%20example%20function%20whose%20solution%20is%20determined%20using%0A%2F%2F%20Bisection%20Method.%20The%20function%20is%20x%5E3%20-%20x%5E2%20%20%2B%202%0Adouble%20func(double%20x)%0A%7B%0A%20%20%20%20return%20x*x*x%20-%20x*x%20%2B%202%3B%0A%7D%0A%20%0A%2F%2F%20Derivative%20of%20the%20above%20function%20which%20is%203*x%5Ex%20-%202*x%0Adouble%20derivFunc(double%20x)%0A%7B%0A%20%20%20%20return%203*x*x%20-%202*x%3B%0A%7D%0A%20%0A%2F%2F%20Function%20to%20find%20the%20root%0Avoid%20newtonRaphson(double%20x)%0A%7B%0A%20%20%20%20double%20h%20%3D%20func(x)%20%2F%20derivFunc(x)%3B%0A%20%20%20%20while%20(abs(h)%20%3E%3D%20EPSILON)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20h%20%3D%20func(x)%2FderivFunc(x)%3B%0A%20%20%0A%20%20%20%20%20%20%20%20%2F%2F%20x(i%2B1)%20%3D%20x(i)%20-%20f(x)%20%2F%20f'(x)%20%20%0A%20%20%20%20%20%20%20%20x%20%3D%20x%20-%20h%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20cout%20%3C%3C%20%22The%20value%20of%20the%20root%20is%20%3A%20%22%20%3C%3C%20x%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%0Aint%20main()%0A%7B%0A%20%20%20%20double%20×0%20%3D%20-20%3B%20%2F%2F%20Initial%20values%20assumed%0A%20%20%20%20newtonRaphson(x0)%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”C++ Program” highlight=”” provider=”manual”/]

Output:

The value of root is : -1.00
[ad type=”banner”]

Tagged in:

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,