13constexpr double EPS = 1e-6;
15double f(
const double x)
17 return sin(
x) - 0.5 *
x ;
20double g(
const double x)
22 return -(
x - 1.234567) * (
x + 0.987654) ;
25double h(
const double x)
30double t(
const double x)
37double Bisect(
const double a,
const double b,
const double eps);
39double Bisect(
const double a,
const double b);
41double 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 : ";
93 cout << endl <<
" point of zero = " << x0 << endl;
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;
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 ) {
169double 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)
clf define function syms x f
guarantee entry into the loop while abs(fc) >
Call the bisection method to find the root of the function eps
end switch method_loc case midpoint c