0% found this document useful (0 votes)
4 views

Bijection method

The document provides C code implementations for three numerical methods to find the roots of the function f(x) = x^3 - 2x - 5: the Bisection method, the Newton-Raphson method, and the Regula Falsi method. Each method includes a function to calculate the root, input prompts for initial values and tolerances, and outputs the found root or a message indicating convergence issues. The code snippets demonstrate the structure and logic necessary for implementing these root-finding algorithms.

Uploaded by

Anjali Negi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Bijection method

The document provides C code implementations for three numerical methods to find the roots of the function f(x) = x^3 - 2x - 5: the Bisection method, the Newton-Raphson method, and the Regula Falsi method. Each method includes a function to calculate the root, input prompts for initial values and tolerances, and outputs the found root or a message indicating convergence issues. The code snippets demonstrate the structure and logic necessary for implementing these root-finding algorithms.

Uploaded by

Anjali Negi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

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:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy