Newton Raphson method in C
The Newton-Raphson method is a numerical technique used to find approximate solutions to equations that cannot be easily solved analytically. It is particularly useful for finding the roots of a real-valued function, which are the values of the variable that make the function equal to zero. The method starts with an initial guess for the root and then iteratively refines that guess to get closer to the actual root.
The basic idea behind the Newton-Raphson method is to use the tangent line at the current guess to estimate the next approximation of the root. The derivative of the function gives the slope of the tangent line, and the intersection of the tangent line with the x-axis gives the new approximation for the root. This process is repeated in subsequent iterations until the desired level of accuracy is achieved.
The Newton-Raphson method is known for its rapid convergence when the initial guess is reasonably close to the true root. However, it can be sensitive to the choice of the initial guess, and in some cases, it may fail to converge or converge to a different root. Therefore, careful selection of the initial guess and verification of convergence are essential when using this method. Despite its limitations, the Newton-Raphson method is widely used in various fields of science, engineering, and numerical analysis to find solutions to complex equations and optimize functions.
C program to illustrate the Newton-Raphson method:
#include <stdio.h>
#include <math.h>
// Function for the equation f(x) = x^4 - 8x^2 + 16
double Equation(double x) {
return pow(x, 4) - 8 * pow(x, 2) + 16;
}
// Derivative of the function f'(x) = 4x^3 - 16x
double Derivative(double x) {
return 4 * pow(x, 3) - 16 * x;
}
// Newton-Raphson method to find the root
double Newton_Raphson_Method(double x0, double tolerance, int max_iterations) {
int i;
double x = x0;
for (i = 0; i<max_iterations; i++) {
double fx = Equation(x);
double fpx = Derivative(x);
// Check for division by zero to avoid problems
if (fpx == 0) {
printf("Derivative is zero. The method won't converge for the initial guess: %lf\n", x);
return x;
}
double x_new = x - fx / fpx;
// Check if the new approximation is close enough
if (fabs(x_new - x) < tolerance) {
printf("Converged after %d iterations.\n", i + 1);
return x_new;
}
x = x_new;
}
printf("Method did not converge after %d iterations.\n", max_iterations);
return x;
}
int main() {
// Taking 4 initial guesses for converging better
double x0_1 = -2.0;
double x0_2 = -1.0;
double x0_3 = 1.0;
double x0_4 = 2.0;
double tolerance = 1e-6;
int max_iterations = 100;
printf("Root 1: %lf\n", Newton_Raphson_Method(x0_1, tolerance, max_iterations));
printf("Root 2: %lf\n", Newton_Raphson_Method(x0_2, tolerance, max_iterations));
printf("Root 3: %lf\n", Newton_Raphson_Method(x0_3, tolerance, max_iterations));
printf("Root 4: %lf\n", Newton_Raphson_Method(x0_4, tolerance, max_iterations));
return 0;
}
Output:
![Newton Raphson method in C](https://static.tutorialandexample.com/c/newton-raphson-method-in-c1.png)
Explanation:
Go through the explanation, which consists of an explanation of every function written in the program, to get a good understanding.
Equation function
The Equation function takes a value of x as input and returns the value of the equation x^4 - 8x^2 + 16. It uses the pow function from <math.h> to calculate x^4 and x^2.
Derivative function
The Derivative function represents the derivative of the function f(x), which is f'(x) = 4x^3 - 16x. It calculates the derivative of the equation using the same pow function as in the Equation function.
Newton_Raphson_Method function
The Newton_Raphson_Method function is the core of the Newton-Raphson method for finding the root of the equation. It takes three parameters: x0 as the initial guess, toleraince as the desired level of accuracy for the root approximation, and max_iterations to set a maximum number of iterations to avoid an infinite loop.
Inside this function, we use a loop to iterate through the Newton-Raphson method. In each iteration, we calculate the value of the function f(x) (using the Equation function) and its derivative f'(x) (using the Derivative function) at the current approximation x.
The method continues iterating until either the new approximation x_new is close enough to the current approximation x (within the specified tolerance) or the maximum number of iterations is reached.
If the derivative f'(x) becomes zero during the iteration (which may lead to division by zero), we print a warning message and return the current approximation x.
At the end of the loop, if the method has not yet converged after the maximum number of iterations, we print a message indicating that the method did not converge.
Main() function
The main() function calls the Newton_Raphson_Method function for four different initial guesses x0_1, x0_2, x0_3, and x0_4. It also sets the tolerance to 1e-6 (1.0 x 10^-6) and the maximum number of iterations to 100.
After that, the roots of equation x^4 - 8x^2 + 16 = 0 are calculated and printed to the console using printf. The function Newton_Raphson_Method is called for each initial guess to find the corresponding root.
How is the initial guess selected?
There are some methods that help to choose the initial guess. These functions are as follows:
Good Initial Approximations:
Try to choose initial guesses that are reasonably close to the actual roots of the equation. The closer the initial guess is to the real root, the faster the method is likely to converge.
Multiple Roots:
Be aware that some equations may have multiple roots or complex roots. You would also need to consider complex initial guesses for complex roots.
Avoiding Zero Derivatives:
The method will not converge if the derivative of the function becomes zero at the initial guess. In such cases, you may need to choose a different starting point.
Try Different Values:
If the method fails to converge or converges to a different root, try different initial guesses until you find suitable values that lead to the correct roots.