Bijection method
Bijection method
Code:
include <stdio.h>
#include <math.h>
// Function for which we want to find root
double f(double x) {
return x*x*x - 2*x - 5;
}
// Bisection method function
double bisection(double a, double b, double tol) {
double c;
while ((b - a) >= tol) { // Find midpoint
c = (a + b) / 2;
// Check if root is found or not
if (f(c) == 0.0)
return c;
// Decide the side to repeat the steps
else if (f(c)*f(a) < 0)
b = c;
else
a = c; }
return c;
}
int main() {
double a, b, tol, root;
// Input initial values of interval [a, b] and tolerance
printf("Enter initial interval [a, b]: ");
scanf("%lf %lf", &a, &b);
printf("Enter tolerance: ");
scanf("%lf", &tol);
// Apply bisection method
root = bisection(a, b, tol);
// Output the root
printf("Root is: %lf\n", root);
return 0;
}
Output:
Newton-Raphson method
Code:
#include <stdio.h>
#include <math.h>
// Function for which we want to find root
double f(double x) {
return x*x*x - 2*x - 5;
}
// Derivative of the function
double df(double x) {
return 3*x*x – 2 }
// Newton-Raphson method function
double newtonRaphson(double x0, double tol, int
max_iter) {
double x = x0;
int iter = 0;
while (iter < max_iter) {
double fx = f(x);
double dfx = df(x);
// Check if derivative is close to zero, indicating
convergence issues
if (fabs(dfx) < 1e-10) {
printf("Derivative is close to zero. Newton-Raphson
method may not converge.\n");
return NAN; // Not-a-Number
}
// Update x using Newton-Raphson formula
double x_new = x - fx / dfx;
// Check for convergence based on tolerance
if (fabs(x_new - x) < tol)
return x_new;
x = x_new;
iter++;
}
printf("Maximum iterations reached. Newton-Raphson
method did not converge.\n");
return NAN; // Not-a-Number
}
int main() {
double x0, tol, root;
int max_iter;
// Input initial guess, tolerance, and maximum iterations
printf("Enter initial guess: ");
scanf("%lf", &x0);
printf("Enter tolerance: ");
scanf("%lf", &tol);
printf("Enter maximum iterations: ");
scanf("%d", &max_iter);
// Apply Newton-Raphson method
root = newtonRaphson(x0, tol, max_iter);
// Output the root
if (!isnan(root))
printf("Root is: %lf\n", root);
return 0;
}
Output:
Regula Falsi (False Position) method
Code:
#include <stdio.h>
#include <math.h>
// Function for which we want to find root
double f(double x) {
return x*x*x - 2*x - 5;
}
// Regula Falsi method function
double regulaFalsi(double a, double b, double tol, int
max_iter) {
double c;
int iter = 0;
while (iter < max_iter) {
// Calculate c using Regula Falsi formula
c = (a*f(b) - b*f(a)) / (f(b) - f(a));
// Check if root is found or not
if (f(c) == 0.0 || fabs(f(c)) < tol)
return c;
// Decide the side to repeat the steps
if (f(c)*f(a) < 0)
b = c;
else
a = c;
iter++;
}
printf("Maximum iterations reached. Regula Falsi method
did not converge.\n");
return NAN; // Not-a-Number
}
int main() {
double a, b, tol, root;
int max_iter;
// Input initial interval [a, b], tolerance, and maximum
iterations
printf("Enter initial interval [a, b]: ");
scanf("%lf %lf", &a, &b);
printf("Enter tolerance: ");
scanf("%lf", &tol);
printf("Enter maximum iterations: ");
scanf("%d", &max_iter);
// Apply Regula Falsi method
root = regulaFalsi(a, b, tol, max_iter);
// Output the root
if (!isnan(root))
printf("Root is: %lf\n", root);
return 0;
}
Output: