/*
*
BISECTION ALGORITHM 2.1
*
*
To find a solution to f(x) = 0 given the continuous function
*
f on the interval [a,b], where f(a) and f(b) have
*
opposite signs:
*
*
INPUT:
endpoints a,b; tolerance TOL;
*
maximum number of iterations N0.
*
*
OUTPUT:
approximate solution p or
*
a message that the algorithm fails.
*/
#include<stdio.h>
#include<math.h>
#define ZERO 1.0E-20
#define true 1
#define false 0
main()
{
double A,FA,B,FB,C,P,FP,TOL,C1,C2;
int I,NO,OK,FLAG;
FILE *OUP[1];
double absval(double);
double F(double);
void INPUT(int *, double *,
double *, double *, double *, double *, int *);
void OUTPUT(FILE **, int *);
INPUT(&OK, &A, &B, &FA, &FB, &TOL, &NO);
if (OK) {
OUTPUT(OUP, &FLAG);
/* STEP 1 */
I = 1;
/* STEP 2 */
OK = true;
while ((I<=NO) && OK) {
/* STEP 3 */
/* compute P(I) */
C = (B - A) / 2;
C1 = (B - A) / 3.0;
C2 = C1 + (B - A) / 3.0;
printf("C1 = %8.6f C2 = %8.6f \n", C1, C2);
P = A + C;
/* STEP 4 */
FP = F(P);
if (FLAG == 2) fprintf(*OUP,"%3d
%15.8e
%15.7e \n",I,P,FP);
if ((absval(FP)<ZERO) || (C<TOL)) {
/* procedure completed successfully */
fprintf(*OUP,"\nApproximate solution P = %11.8f \n",P);
fprintf(*OUP,"with F(P) = %12.8f\n",FP);
fprintf(*OUP,"Number of iterations = %3d",I);
fprintf(*OUP,"
Tolerance = %15.8e\n",TOL);
OK = false;
}
else {
/* STEP 5 */
I++;
/* STEP 6 */
/* compute A(I) and B(I) */