You are on page 1of 2

/* file name : bisect.

cpp

*/

/*------------ BISECTION METHOD TO FIND ROOT OF AN EQUATION ----------*/ /* THE EXPRESSION FOR AN EQUATION IS DEFINED IN function fx YOU CAN WRITE DIFFERENT EQUATION IN function fx. HERE, f(x) = x*x*x - 1.8*x*x - 10*x - 17 INPUTS : 1) Initial interval [a,b] in which root is to be found. 2) Permissible error in the root. OUTPUTS : 1) Number of iterations for given interval and permissible error. 3) Value of the root in given interval. /*-----------------------------#include<stdio.h> #include<math.h> #include<stdlib.h> void main() { double fx ( double x); double x,a,b,fa,fb,c,fc,err; int n,i; printf("\n printf("\n\n BISECTION METHOD TO FIND ROOT OF AN EQUATION"); f(x) = x*x*x-9*x+1");

*/

PROGRAM --------------------------*/

/* DECLARATION OF FUNCTION

*/

printf("\n\nEnter an interval [a,b] in " "which root is to be found"); printf("\na = "); scanf("%lf",&a); /* INTERVAL [a,b] IS TO BE ENTERED HERE */ printf("b = "); scanf("%lf",&b); printf("\nEnter the value of permissible error = "); scanf("%lf",&err); n = (log10(abs(b - a)) - log10( err ) )/log10(2); /* CALCULATION OF STEPS 'n' */ n = n + 1; /* n SHOULD NOT BE A FRACTIONAL NUMBER; ADD '1' */ i = 0; printf("\nNumber of iterations for this error bound are = %d",n); printf("\n\npress any key for step by step display of intervals"); printf("\n\n while(n-- > 0) { fa = fx(a); fb = fx(b); c = (a + b)/2; fc = fx(c); RESULTS OF BISECTION METHOD\n"); /* /* /* /* CALCULATE CALCULATE CENTER OF CALCULATE f(x) AT x = a f(x) AT x = b THE INTERVAL f(x) AT x = c */ */ */ */

i++; printf("\n\n%d a = %lf b = %lf printf("\n f(a) = %lf f(b) = %lf c = %lf",i,a,b,c); f(c) = %lf",fa,fb,fc);

if( (fc*fa) < 0) b = c; /* IF f(c)f(a) < 0, NEW INTERVAL IS [a,c] */ if( (fc*fb) < 0) a = c; /* IF f(c)f(b) < 0, NEW INTERVAL IS [b,c] */ printf("\n } x = a + (b - a)/2; interval : a = %lf b = %lf",a,b);

/* ROOT = a + half interval [a,b]

*/

printf("\nThe value of root is = %20.15lf",x); /* ROOT */ } /*---------- FUNCTION PROCEDURE TO CALCULATE VALUE OF EQUATION --------*/ double fx ( double x) { double f; f = (x*x*x-9*x+1); /* FUNCTION f(x) */ return(f); } /*------------------------ END OF PROGRAM -----------------------------*/

You might also like