Code:
/* Program finds the roots of a function using the bisection method */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* Function prototypes */
double func_1(double x);
double func_2(double x);
double func_3(double x);
double bisect(double func(double), double a, double b, double epsilon);
/** Main function */
int main(void) {
int f_choice, // Choice for user to make on deciding which function to use
i_choice; // Choice for user to make on deciding which interval to use
double epsilon, // the error tolerance
a, // left endpoint
b, // right endpoint
root, // the root of the fuction
v_root; // value of the function with specified root
printf("Which function you want to choose (1, 2, 3): ");
scanf("%d", & f_choice);
/*Nested switch statment to figure which function and then which interval to use. For each
* outter-case there are two inner-cases for the interval. After the error tolerance has been
* recieved from the user the bisection method is invoked then the method to find the value of
* of the function at the computed root *************************************************** */
switch (f_choice) {
case (1):
printf("Which interval you want to choose (1, 2): ");
scanf("%d", & i_choice);
switch (i_choice) {
case (1):
printf("What error is accepted (epsilon): ");
scanf("%lf", & epsilon);
a = -1.0;
b = 0.0;
root = bisect(func_1, a, b, epsilon);
printf("The root of the function 1 in the interval 1 is %g\n", root);
v_root = func_1(root);
printf("The function value at the root is %g\n", v_root);
break;
case (2):
printf("What error is accepted (epsilon): ");
scanf("%lf", & epsilon);
a = 0.0;
b = 0.5;
root = bisect(func_1, a, b, epsilon);
printf("The root of the function 1 in the interval 2 is %g\n", root);
v_root = func_1(root);
printf("The function value at the root is %g\n", v_root);
break;
default:
;
break;
}
break;
case (2):
printf("Which interval you want to choose (1, 2): ");
scanf("%d", & i_choice);
switch (i_choice) {
case (1):
printf("What error is accepted (epsilon): ");
scanf("%lf", & epsilon);
a = -4.0;
b = -2.0;
root = bisect(func_2, a, b, epsilon);
printf("The root of the function 2 in the interval 1 is %g\n", root);
v_root = func_2(root);
printf("The function value at the root is %g\n", v_root);
break;
case (2):
printf("What error is accepted (epsilon): ");
scanf("%lf", & epsilon);
a = 2.0;
b = 4.0;
root = bisect(func_2, a, b, epsilon);
printf("The root of the function 2 in the interval 2 is %g\n", root);
v_root = func_2(root);
printf("The function value at the root is %g\n", v_root);
break;
default:
;
break;
}
break;
case (3):
printf("Which interval you want to choose (1, 2): ");
scanf("%d", & i_choice);
switch (i_choice) {
case (1):
printf("What error is accepted (epsilon): ");
scanf("%lf", & epsilon);
a = 0.0;
b = 1.0;
root = bisect(func_3, a, b, epsilon);
printf("The root of the function 3 in the interval 1 is %g\n", root);
v_root = func_3(root);
printf("The function value at the root is %g\n", v_root);
break;
case (2):
printf("What error is accepted (epsilon): ");
scanf("%lf", & epsilon);
a = 1.0;
b = 2.0;
root = bisect(func_3, a, b, epsilon);
printf("The root of the function 3 in the interval 2 is %g\n", root);
v_root = func_3(root);
printf("The function value at the root is %g\n", v_root);
break;
default:
;
break;
}
default:
;
break;
}
return 0;
}
/** Returns the polynomial 3x^3 - 3x^2 + 2 */
double func_1(double x) {
return (3.0 * pow(x, 3.0) - 3.0 * pow(x, 2.0) + 0.2);
}
/** Returns the polynomial x^4 - 3x^2 - 8 */
double func_2(double x) {
return (pow(x, 4.0) - 3.0 * pow(x, 2.0) - 8.0);
}
/** Returns the trig function 2sin(2x)-3cos(3x) */
double func_3(double x) {
return (2.0 * sin(2.0 * x) - 3.0 * cos(3.0 * x));
}
/** Figures out the root */
double bisect(double func(double), double a, double b, double epsilon) {
double f_a = func(a);
double f_b = func(b);
if (f_a * f_b > 0) {
return 0;
}
else if (f_a == 0) {
return a;
}
else if (f_b == 0) {
return b;
}
/* f_a * f_b < 0 */
double m;
while ( fabs(b - a) > epsilon) {
m = (a + b) / 2.0;
double f_mid = func(m);
/* Replace a or b? */
if (f_mid == 0.0) {
break; /* exit while loop */
}
else if (f_mid * f_a < 0.0) {
b = m;
}
else
/* f_mid * f_a < 0.0 */
{
a = m;
}
}
return m; // return the value of the new m which is now the root
}
I am sorry if it was unneccessary to include the entire code just did not want to leave anything out.