C Code
/*
---------------------------------------------------------------------------
Algo2-2.c C program for implementing Algorithm
2.2
Algorithm translated to C by: Dr. Norman
Fahrer
IBM and Macintosh verification by: Daniel Mathews
NUMERICAL METHODS: C Programs, (c) John H. Mathews 1995
To accompany the text:
NUMERICAL METHODS for Mathematics, Science and Engineering, 2nd Ed,
1992
Prentice Hall, Englewood Cliffs, New Jersey, 07632, U.S.A.
Prentice Hall, Inc.; USA, Canada, Mexico ISBN 0-13-624990-6
Prentice Hall, International Editions: ISBN
0-13-625047-5
This free software is compliments of the author.
E-mail
address: in%"mathews@fullerton.edu"
Algorithm 2.2 (Bisection Method).
Section 2.2, Bracketing Methods for Locating a Root,
Page 61
---------------------------------------------------------------------------
*/
/*
---------------------------------------------------------------------------
Algorithm 2.2 (Bisection Method). To find a root of the
equation f(x) = 0 in the interval
[a,b]. Proceed with the
method only if f(x) is continuous
and f(a) and f(b) have
opposite signs.
---------------------------------------------------------------------------
*/
/* User has to supply a function named : ffunction
An example is included in this program */
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
/* define prototype for USER-SUPPLIED function
f(x) */
double ffunction(double x);
/* EXAMPLE for "ffunction" */
double ffunction(double x)
{
return (x * sin(x) -
1);
}
/* -------------------------------------------------------- */
/* Main program for algorithm 2.2 */
void main()
{
double Delta =
1E-6; /*
Tolerance for width of interval */
int Satisfied =
0; /*
Condition for loop termination */
double A,
B; /*
Endpoints of the interval [A,B] */
double YA,
YB; /* Function values
at the interval-borders */
int
Max; /* Calculation of the maximum
number of iterations */
int
K; /*
Loop Counter */
double C, YC; /* Midpoint of
interval and function value there */
printf("-----------------------------------------------------\n");
printf("Please enter endpoints A and B of the
interval [A,B]\n");
printf("EXAMPLE : A = 0 and B = 2.
Type: 0 2 \n");
scanf("%lf %lf", &A, &B);
printf("The interval ranges from %lf to
%lf\n", A,B);
YA = ffunction(A); /*
compute function values */
YB = ffunction(B);
Max = (int) ( 1 + floor( ( log(B-A) -
log(Delta) ) / log(2) ) );
printf("Max = %d\n",Max);
/* Check to see if the bisection method
applies */
if( ( (YA >= 0) && (YB >=0) )
|| ( (YA < 0) && (YB < 0) ) ) {
printf("The values ffunction(A)
and ffunction(B)\n");
printf("do not differ in
sign.\n");
exit(0); /*
exit program */
}
for(K = 1; K <= Max ; K++) {
if(Satisfied ==
1) break;
C = (A + B) /
2; /* Midpoint of interval */
YC =
ffunction(C); /* Function value at midpoint */
if( YC == 0)
{ /* first
'if' */
A
= C; /* Exact root is found
*/
B
= C;
}
else if( ( (YB >= 0) && (YC >=0) ) || ( (YB
< 0) && (YC < 0) ) ) {
B =
C; /* Squeeze from the right
*/
YB = YC;
}
else {
A =
C; /* Squeeze from the left */
YA = YC;
}
if( (B-A) < Delta ) Satisfied = 1; /* check for early
convergence */
} /* end of 'for'-loop */
printf("----------------------------------------------\n");
printf("The maximum number of iterations is :
%d\n",Max);
printf("The number of performed iterations is
: %d\n",K - 1);
printf("----------------------------------------------\n");
printf("The computed root of f(x) = 0 is :
%lf \n",C);
printf("----------------------------------------------\n");
printf("The accuracy is +- %lf\n", B-A);
printf("----------------------------------------------\n");
printf("The value of the function f(C) is
%lf\n",YC);
} /* End of main program */
(c) John H. Mathews 2004