Newton method is used for finding root of the function in given interval.
Algorithm:
IN: Function f, which is continous function and value: a - being intital guess - the closer to root, the better. Function fD, which is the derivative of function f. OUT: Root in given interval. 1. Make x = a and prevX = a. 2. Check if abs(f(x)) < precision. 3. If it is end algorithm and return x. 4. Make x = prevX - (f(prevX) / fD(prevX)). 5. Make prevX = x. 6. Go to step 2.
Sample Output:
f(x) = x * (x + 2) -1 -3 root: -2.414201183431953
Iteration Number | a | prevX | x | f(x) |
---|---|---|---|---|
1 | -3 | -3 | -3 | 2.0 |
2 | -3 | -3 | 2.5 | 0.25 |
3 | -3 | 2.5 | -2.416 | 0.0069 |
4 | -3 | -2.416 | -2.414 | 6.007E-6 |
Pros:
- easy to implement (without approximating the derivative of function!).
- very fast method (not counting some special functions).
- no need to find initial interval, simply choose a number and one of the roots should be found.
Cons:
- has trouble with some functions.
- If a stationary point of given function is encountered, there will be division by zero, because of the derivative being zero.
- need to calculate derivative of given function.
- may miss root.
Java Implementation Python Implementation C++ Implementation C# Implementation