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.
Algorithm:
Input: initial x, func(x), derivFunc(x)
Output: Root of Func()
- Compute values of func(x) and derivFunc(x) for given initial x
- Compute h: h = func(x) / derivFunc(x)
- While h is greater than allowed error ε
- h = func(x) / derivFunc(x)
- x = x – h
Below is C++ implementation of above algorithm.
Output:
The value of root is : -1.00[ad type=”banner”]