1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
/*
This program will find the roots of one of three different functions on one of
two intervals based on user input.
*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
double evaluate(double f(double), double a, double b, double error) {
double mid;
double f_mid;
double f_a = f(a);
double f_b = f(b);
while(fabs(a- b) > error) {
mid = (a+b)/2.0;
f_mid = f(mid);
if(f_mid == 0.0)
{
break;
}
else if((f_mid * f_a) < 0.0)
{
b = mid;
}
else
{
a = mid;
}
}
if (fabs(a-b) == error)
printf("No root found.");
return mid;
}
double func1(double x) {
return (3.0*x*x*x) - (3.0*x*x) + 0.2;
}
double func2(double x) {
return (x*x*x*x) - (3.0*x*x) - 8.0;
}
double func3(double x) {
return 2.0*sin(2.0*x) - 3.0*cos(3.0*x);
}
int main(void)
{
int o, p;
double a, b, error, root, root_value;
printf("Which function do you want to choose (1,2,3): ");
error1:
scanf("%d", &o);
switch ( o ) {
case 1:
printf("Which interval do you want to choose (1,2): ");
error2:
scanf("%d", &p);
printf("What error is accepted (epsilon): ");
scanf("%g", &error);
switch ( p ) {
case 1:
a = -1.0;
b = 0.0;
root = evaluate(func1, a, b, error);
root_value = func1(root);
printf("The root of the function 1 in the interval 1 is %g.\nThe function value at the root is %g\n", root, root_value);
break;
case 2:
a = 0.0;
b = 0.5;
root = evaluate(func1, a, b, error);
root_value = func1(root);
printf("The root of the function 1 in the interval 2 is %g.\nThe function value at the root is %g\n", root, root_value);
break;
default:
printf("ERROR: Enter 1 or 2 for your interval: ");
goto error2;
break;
}
break;
case 2:
printf("Which interval do you want to choose (1,2): ");
error3:
scanf("%d", &p);
printf("What error is accepted (epsilon): ");
scanf("%g", &error);
switch ( p ) {
case 1:
a = -4.0;
b = -2.0;
root = evaluate(func2, a, b, error);
root_value = func1(root);
printf("The root of the function 2 in the interval 1 is %g.\nThe function value at the root is %g\n", root, root_value);
break;
case 2:
a = 2.0;
b = 4.0;
root = evaluate(func2, a, b, error);
root_value = func2(root);
printf("The root of the function 2 in the interval 2 is %g.\nThe function value at the root is %g\n", root, root_value);
break;
default:
printf("ERROR: Enter 1 or 2 for your interval: ");
goto error3;
break;
}
break;
case 3:
printf("Which interval do you want to choose (1,2): ");
error4:
scanf("%d", &p);
printf("What error is accepted (epsilon): ");
scanf("%g", &error);
switch ( p ) {
case 1:
a = 0.0;
b = 1.0;
root = evaluate(func3, a, b, error);
root_value = func3(root);
printf("The root of the function 3 in the interval 1 is %g.\nThe function value at the root is %g\n", root, root_value);
break;
case 2:
a = 1.0;
b = 2.0;
root = evaluate(func3, a, b, error);
root_value = func3(root);
printf("The root of the function 3 in the interval 2 is %g.\nThe function value at the root is %f\n", root, root_value);
break;
default:
printf("ERROR: Enter 1 or 2 for your interval: ");
goto error4;
break;
}
break;
default:
printf("Enter a number between 1 and 3: ");
goto error1;
break;
}
return 0;
}
|