13 constexpr
double EPS = 1e-6;
15 double f(
const double x)
17 return sin(x) - 0.5 * x ;
20 double g(
const double x)
22 return -(x - 1.234567) * (x + 0.987654) ;
25 double h(
const double x)
30 double t(
const double x)
37 double Bisect(
const double a,
const double b,
const double eps);
39 double Bisect(
const double a,
const double b);
41 double Bisect(
const std::function<
double(
double)>& func,
42 const double a,
const double b,
const double eps);
47 cout <<
" Determine point of zero in [a,b] by bisection " << endl;
49 cout <<
" f(x) := sin(x) - x/2" << endl;
50 cout <<
" g(x) := (1.234567-x)*(x+0.987654)" << endl;
51 cout <<
" h(x) := 3.0 - exp(x)" << endl;
52 cout <<
" t(x) := 1-x*x" << endl;
54 std::function<double(
double)> ff;
56 cout << endl <<
"Which function do you prefer ? ";
71 cout <<
" no correct choice. h(x) is used." << endl;
79 cout <<
" " << choice <<
"(a) > 0, a : ";
81 cout <<
" " << choice <<
"(b) < 0, b : ";
90 if ( abs(fa) <
EPS || abs(fb) <
EPS) {
91 if ( abs(fa) <
EPS ) {
93 cout << endl <<
" point of zero = " << x0 << endl;
95 if ( abs(fb) <
EPS ) {
97 cout << endl <<
" point of zero = " << x0 << endl;
102 cout <<
"I have to swap a and b" << endl;
108 cout << endl <<
" point of zero = " << x0 << endl;
112 cout << endl <<
"There is potentially no solution in [" <<
a <<
"," <<
b <<
"]" << endl;
124 double Bisect(
const double a,
const double b,
const double eps)
126 double x0, fc, c = (
a +
b) / 2;
128 fc = sin(c) - 0.5 * c;
129 if ( std::abs(fc) < eps ) {
132 else if ( fc > 0.0 ) {
148 double x0, fc, c = (
a +
b) / 2;
151 if ( std::abs(fc) <
EPS ) {
154 else if ( fc > 0.0 ) {
169 double Bisect(
const std::function<
double(
double)>& func,
170 const double a,
const double b,
const double eps)
172 double c = (
a +
b) / 2, fc = func(c), x0;
174 if ( std::abs(fc) < eps ) {
177 else if ( fc > 0.0 ) {
double Bisect(const double a, const double b, const double eps)