17long double bisection(
long double a,
long double b,
long double tol,
int max_iter, std::vector<std::string> &iterations,
int decimal_places)
19 long double fa =
f(a), fb =
f(b);
22 std::cerr <<
"Bisection method fails. f(a) and f(b) should have opposite signs.\n";
27 for (
int i = 0; i < max_iter; ++i)
30 long double fc =
f(c);
31 std::ostringstream oss;
32 oss <<
"Step " << i + 1 <<
": [" << std::fixed << std::setprecision(decimal_places) << a <<
", " << b <<
"]";
33 iterations.push_back(oss.str());
34 if ((b - a) / 2.0L < tol)
51long double newton_raphson(
long double x0,
long double tol,
int max_iter, std::vector<std::string> &iterations,
int decimal_places)
54 for (
int i = 0; i < max_iter; ++i)
56 long double fx0 =
f(x0);
60 std::cerr <<
"Newton-Raphson method fails. Derivative zero.\n";
64 std::ostringstream oss;
65 oss <<
"Step " << i + 1 <<
": x0 = " << std::fixed << std::setprecision(decimal_places) << x0
67 iterations.push_back(oss.str());
68 if (fabs(x1 - x0) < tol)
76long double hybrid_method(
long double a,
long double b,
long double tol,
int max_iter, std::vector<std::string> &iterations,
int decimal_places)
78 long double fa =
f(a), fb =
f(b);
81 std::cerr <<
"Hybrid method fails. f(a) and f(b) should have opposite signs.\n";
86 for (
int i = 0; i < max_iter; ++i)
89 long double fc =
f(c);
90 std::ostringstream oss;
91 oss <<
"Step " << i + 1 <<
": [" << std::fixed << std::setprecision(decimal_places) << a <<
", " << b <<
"]";
92 iterations.push_back(oss.str());
93 if ((b - a) / 2.0L < tol)
99 long double d = c - fc / fpc;
102 long double fd =
f(d);
103 std::ostringstream oss_newton;
104 oss_newton <<
"Newton Step: c = " << std::fixed << std::setprecision(decimal_places) << c
106 iterations.push_back(oss_newton.str());
107 if (fabs(d - c) < tol)
139long double brent_method(
long double a,
long double b,
long double tol,
int max_iter, std::vector<std::string> &iterations,
int decimal_places)
141 long double fa =
f(a), fb =
f(b);
144 std::cerr <<
"Brent's method fails. f(a) and f(b) should have opposite signs.\n";
148 if (fabs(fa) < fabs(fb))
154 long double c = a, fc = fa, s = b, fs = fb;
158 for (
int i = 0; i < max_iter; ++i)
160 if (fb != fc && fa != fc)
163 s = (a * fb * fc) / ((fa - fb) * (fa - fc)) +
164 (b * fa * fc) / ((fb - fa) * (fb - fc)) +
165 (c * fa * fb) / ((fc - fa) * (fc - fb));
170 s = b - fb * (b - a) / (fb - fa);
174 bool condition1 = (s < (3 * a + b) / 4.0L || s > b);
175 bool condition2 = (mflag && fabs(s - b) >= fabs(b - c) / 2.0L);
176 bool condition3 = (!mflag && fabs(s - b) >= fabs(c - d) / 2.0L);
177 bool condition4 = (mflag && fabs(b - c) < tol);
178 bool condition5 = (!mflag && fabs(c - d) < tol);
180 if (condition1 || condition2 || condition3 || condition4 || condition5)
191 long double fs_new =
f(s);
192 std::ostringstream oss;
193 oss <<
"Step " << i + 1 <<
": a = " << std::fixed << std::setprecision(decimal_places) << a
194 <<
", b = " << b <<
", s = " << s;
195 iterations.push_back(oss.str());
212 if (fabs(fa) < fabs(fb))
218 if (fabs(b - a) < tol)
226long double ridder_method(
long double a,
long double b,
long double tol,
int max_iter, std::vector<std::string> &iterations,
int decimal_places)
228 long double fa =
f(a), fb =
f(b);
231 std::cerr <<
"Ridder's method fails. f(a) and f(b) should have opposite signs.\n";
235 for (
int i = 0; i < max_iter; ++i)
237 long double c = 0.5L * (a + b);
238 long double fc =
f(c);
239 long double s_sq = fc * fc - fa * fb;
242 std::cerr <<
"Ridder's method fails. Square root of negative number.\n";
245 long double s = sqrt(s_sq);
249 long double sign = ((fa - fb) < 0) ? -1.0L : 1.0L;
250 long double x = c + (c - a) * fc / s * sign;
251 long double fx =
f(x);
252 std::ostringstream oss;
253 oss <<
"Step " << i + 1 <<
": [" << std::fixed << std::setprecision(decimal_places) << a <<
", " << b <<
"]";
254 iterations.push_back(oss.str());
266 else if (fa * fx < 0.0)
277 if (fabs(b - a) < tol)
281 return 0.5L * (a + b);
long double f(long double x)
long double f_prime(long double x)
long double bisection(long double a, long double b, long double tol, int max_iter, std::vector< std::string > &iterations, int decimal_places)
long double hybrid_method(long double a, long double b, long double tol, int max_iter, std::vector< std::string > &iterations, int decimal_places)
long double ridder_method(long double a, long double b, long double tol, int max_iter, std::vector< std::string > &iterations, int decimal_places)
long double newton_raphson(long double x0, long double tol, int max_iter, std::vector< std::string > &iterations, int decimal_places)
long double brent_method(long double a, long double b, long double tol, int max_iter, std::vector< std::string > &iterations, int decimal_places)