write a program in c++ for the newton raphson method to find a real root of equation f(x) =0 , 3x= cosx+1, correct to four decimal places
Answers
Explanation:
Program for Newton Raphson Method
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 (3x2 – 2x 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 Raphson 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) explicitlyThe 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.
newtonraphsonformula
Advantages of Newton Raphson Method:
It is best method to solve the non-linear equations.
It can also be used to solve the system of non-linear equations, non-linear differential and non-linear integral equations.
The order of convergence is quadric i.e. of second order which makes this method fast as compared to other methods.
It is very easy to implement on computer.
Disadvantages of Newton Raphson Method:
This method becomes complicated if the derivative of the function f(x) is not simple.
This method requires a great and sensitive attention regarding the choice of its approximation.
In each iteration, we have to evaluate two quantities f(x) and f'(x) for some x.
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 the implementation of above algorithm.
// C++ program for implementation of Newton Raphson Method for
// solving equations
#include<bits/stdc++.h>
#define EPSILON 0.001
using namespace std;
// An example function whose solution is determined using
// Bisection Method. The function is x^3 - x^2 + 2
double func(double x)
{
return x*x*x - x*x + 2;
}
// Derivative of the above function which is 3*x^x - 2*x
double derivFunc(double x)
{
return 3*x*x - 2*x;
}
// Function to find the root
void newtonRaphson(double x)
{
double h = func(x) / derivFunc(x);
while (abs(h) >= EPSILON)
{
h = func(x)/derivFunc(x);
// x(i+1) = x(i) - f(x) / f'(x)
x = x - h;
}
cout << "The value of the root is : " << x;
}
// Driver program to test above
int main()
{
double x0 = -20; // Initial values assumed
Output:
The value of root is : -1.00
How does this work?
The idea is to draw a line tangent to f(x) at point x1. The point where the tangent line crosses the x axis should be a better estimate of the root than x1. Call this point x2. Calculate f(x2), and draw a line tangent at x2.