Skip to content

Instantly share code, notes, and snippets.

@AnuragAnalog
Created July 9, 2021 04:05
Show Gist options
  • Save AnuragAnalog/9b38489a1ad27343abd20831f790b952 to your computer and use it in GitHub Desktop.
Save AnuragAnalog/9b38489a1ad27343abd20831f790b952 to your computer and use it in GitHub Desktop.
Bisection method Implementation in C
/********* Bisection method which is also known as bolzano method is based on the repeated application of intermediate value property.
Let the function f(x) be continous between a and b. For definiteness, let f(a) be (-)ve and f(b) be (+)ve. Then the first approximation to the root is x1 = (a+b)/2.
If f(x1)=0, then x1 is a root of f(x) = 0, otherwise, the root lies between a
and x1 or x1 and baccording to f(x1) is (+)ve or (-)ve. Then we bisect the inte
rval as before and continue the process until the root is found to the desird ac
curacy. *********/
/*************** PROGRAM STARTS HERE ***************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/********* FUNCTION DECLARATION *********/
void bisect(float a, float b);
float function(float val);
void check_bound(float a, float b);
/********** MAIN STARTS HERE *********/
int main(int argc, char **argv)
{
float a, b;
if (argc != 3) //Verification of arguments
{
fprintf(stderr, "Usage: %s <lower> <upper>\n", argv[0]);
exit(1);
}
a = atof(argv[1]);
b = atof(argv[2]);
check_bound(a, b);
printf("By using Bisection method: \n");
printf("The equation is: \n");
printf("\t");
printf("f(x) = x^4 - 26*x^2 + 49*x - 25\n");
printf("--------------------------------\n");
printf(" f(a) f(b) c \n");
printf("--------------------------------\n");
bisect(a, b); //Calling Function
exit(0);
}
void bisect(float a, float b)
{
float i, fa, fb, fm, mid; //Declaration of variables in float
while (1) //Producing an infinte loop
{
fa = function(a); //Calling Functions
fb = function(b); //Calling Functions
printf("%f %f ", fa, fb);
if ((fa * fb) > 0) //Check condition
{
printf("Root doesn't exist between %.1f and %.1f\n", a, b);
exit(0);
}
else
{
mid = (a + b) / 2; //Calculating the mid-point of the interval
fm = function(mid); //Calculating the function value at mid-point
printf(" %f\n", mid);
if (fm == 0.0000) //Check condition
{
printf("%f is the given root\n", mid);
exit(0);
}
else if ((fa * fm) < 0)
{
b = mid;
}
else if ((fm * fb) < 0)
{
a = mid;
}
if (floor(a*10000)/10000 == floor(b*10000)/10000)
{
printf("The given root is %f after %.1f iterations\n", b, i);
break; //Getting out of the loop
}
}
i++; //Incrementing of i
}
return ;
}
float function(float val)
{
float fx, x = val;
fx = (x * x * x * x) - (26 * x * x) + (49 * x) - 25;
return fx;
}
void check_bound(float a, float b)
{
float fa, fb;
fa = function(a);
fb = function(b);
if ((fa * fb) == 0)
{
printf("The root is one of the boundaries.\n");
exit(0);
}
return ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment