28 std::function<
long double(
long double,
long double,
long double,
int, std::vector<std::string> &,
int)> method_func,
29 long double a,
long double b,
long double tol,
int max_iter,
32 std::vector<std::string> iterations;
33 long double root = method_func(a, b, tol, max_iter, iterations, decimal_places);
35 RootInfo info{root,
static_cast<int>(iterations.size()), decimal_places};
36 summary[method_name].emplace_back(info);
39 std::cout <<
"\nMethod: " << method_name <<
"\n";
40 if (method_name ==
"Newton-Raphson Method")
42 std::cout <<
"Initial guess: x0 = " << std::fixed << std::setprecision(decimal_places) << a <<
"\n";
46 std::cout <<
"Interval: [" << std::fixed << std::setprecision(2) << a <<
", " << b <<
"]\n";
48 std::cout <<
"Root: " << std::fixed << std::setprecision(decimal_places) << root <<
"\n";
49 std::cout <<
"Iterations:\n";
50 for (
const auto &iter : iterations)
52 std::cout << iter <<
"\n";
54 std::cout <<
"Iterations Count: " << iterations.size() <<
"\n";
61 std::vector<std::pair<long double, long double>> intervals = {
68 std::vector<long double> found_roots;
71 long double tol_bisection = 1e-4L;
72 long double tol_newton = 1e-14L;
73 long double tol_hybrid = 1e-14L;
76 std::cout <<
"\n--- Problem Steps Execution ---\n";
79 std::cout <<
"\nPart (i): Bisection Method to find roots to 4 decimal places\n";
80 for (
const auto &interval : intervals)
82 std::vector<std::string> iterations;
83 long double root =
bisection(interval.first, interval.second, tol_bisection, max_iter, iterations, 4);
84 RootInfo info{root,
static_cast<int>(iterations.size()), 4};
85 summary[
"Bisection Method"].emplace_back(info);
88 found_roots.emplace_back(root);
90 std::cout <<
"Root in [" << std::fixed << std::setprecision(2) << interval.first <<
", " << interval.second <<
"]: "
91 << std::fixed << std::setprecision(4) << root <<
"\n";
92 std::cout <<
"Iterations: " << iterations.size() <<
"\n";
96 std::cout <<
"\nPart (ii): Newton-Raphson Method to refine roots to 14 decimal places\n";
97 for (
auto &x0 : found_roots)
99 std::vector<std::string> iterations;
100 long double root =
newton_raphson(x0, tol_newton, max_iter, iterations, 14);
101 RootInfo info{root,
static_cast<int>(iterations.size()), 14};
102 summary[
"Newton-Raphson Method"].emplace_back(info);
104 std::cout <<
"Refined root starting from " << std::fixed << std::setprecision(4) << x0 <<
": "
105 << std::fixed << std::setprecision(14) << root <<
"\n";
106 std::cout <<
"Iterations: " << iterations.size() <<
"\n";
110 std::cout <<
"\nPart (iii): Hybrid Method to find roots to 14 decimal places\n";
111 for (
const auto &interval : intervals)
113 std::vector<std::string> iterations;
114 long double root =
hybrid_method(interval.first, interval.second, tol_hybrid, max_iter, iterations, 14);
115 RootInfo info{root,
static_cast<int>(iterations.size()), 14};
116 summary[
"Hybrid Method"].emplace_back(info);
118 std::cout <<
"Root in [" << std::fixed << std::setprecision(2) << interval.first <<
", " << interval.second <<
"] (Hybrid): "
119 << std::fixed << std::setprecision(14) << root <<
"\n";
120 std::cout <<
"Iterations: " << iterations.size() <<
"\n";
124 std::cout <<
"\n--- Summary of Problem Steps Results ---\n";
125 for (
const auto &method :
summary)
127 std::cout <<
"\nMethod: " << method.first <<
"\n";
129 for (
const auto &info : method.second)
131 std::cout <<
" Root " << idx++ <<
": " << std::fixed << std::setprecision(info.decimal_places) << info.root
132 <<
" | Iterations: " << info.iterations <<
"\n";
144 std::vector<std::pair<long double, long double>> intervals = {
151 long double tol = 1e-14L;
155 std::vector<std::pair<std::string, std::function<
long double(
long double,
long double,
long double,
int, std::vector<std::string> &,
int)>>> methods = {
156 {
"Bisection Method", [](
long double a,
long double b,
long double tol,
int max_iter, std::vector<std::string> &iterations,
int decimal_places) ->
long double
158 return bisection(a, b, tol, max_iter, iterations, decimal_places);
160 {
"Newton-Raphson Method", [](
long double a,
long double b,
long double tol,
int max_iter, std::vector<std::string> &iterations,
int decimal_places) ->
long double
163 long double initial_guess = (a + b) / 2.0L;
164 return newton_raphson(initial_guess, tol, max_iter, iterations, decimal_places);
166 {
"Hybrid Method", [](
long double a,
long double b,
long double tol,
int max_iter, std::vector<std::string> &iterations,
int decimal_places) ->
long double
168 return hybrid_method(a, b, tol, max_iter, iterations, decimal_places);
170 {
"Brent's Method", [](
long double a,
long double b,
long double tol,
int max_iter, std::vector<std::string> &iterations,
int decimal_places) ->
long double
172 return brent_method(a, b, tol, max_iter, iterations, decimal_places);
174 {
"Ridder's Method", [](
long double a,
long double b,
long double tol,
int max_iter, std::vector<std::string> &iterations,
int decimal_places) ->
long double
176 return ridder_method(a, b, tol, max_iter, iterations, decimal_places);
180 std::map<std::string, std::vector<RootInfo>> comparison_results;
183 for (
const auto &method : methods)
185 for (
const auto &interval : intervals)
187 std::vector<std::string> iterations;
188 long double root = method.second(interval.first, interval.second, tol, max_iter, iterations, 15);
189 RootInfo info{root,
static_cast<int>(iterations.size()), 15};
190 comparison_results[method.first].emplace_back(info);
195 std::cout <<
"\n--- Comparison of All Methods (Precision: 1e-14) ---\n\n";
198 std::cout << std::left << std::setw(25) <<
"Method"
199 << std::setw(30) <<
"Root 1 (-3,-2)"
200 << std::setw(15) <<
"Iterations"
201 << std::setw(30) <<
"Root 2 (0,1)"
202 << std::setw(15) <<
"Iterations"
203 << std::setw(30) <<
"Root 3 (1,3)"
204 << std::setw(15) <<
"Iterations" <<
"\n";
207 std::cout << std::string(130,
'-') <<
"\n";
210 for (
const auto &method : methods)
212 std::cout << std::left << std::setw(25) << method.first;
213 for (
size_t i = 0; i < intervals.size(); ++i)
215 if (comparison_results[method.first][i].root != comparison_results[method.first][i].root)
218 std::cout << std::left << std::setw(30) <<
"N/A"
219 << std::left << std::setw(15) <<
"N/A";
223 std::cout << std::left << std::setw(30) << std::fixed << std::setprecision(15) << comparison_results[method.first][i].root
224 << std::left << std::setw(15) << comparison_results[method.first][i].iterations;
230 std::cout <<
"\nNote: Precision is set to 1e-14, output displays 15 decimal places.\n\n";
234void get_user_input(
long double &a,
long double &b,
long double &x0, std::string &method_name,
long double &tol)
237 std::vector<std::string> available_methods = {
"Bisection Method",
"Hybrid Method",
"Brent Method",
"Ridder Method",
"Newton-Raphson Method",
"Problem Steps Mode",
"Compare All Methods"};
240 std::cout <<
"\nAvailable methods:\n";
241 for (
size_t i = 0; i < available_methods.size(); ++i)
243 std::cout << i + 1 <<
". " << available_methods[i] <<
"\n";
248 std::cout <<
"Select a method (1-" << available_methods.size() <<
"): ";
249 std::cin >> method_choice;
250 while (method_choice < 1 || method_choice >
static_cast<int>(available_methods.size()))
252 std::cout <<
"Invalid choice. Please select a method (1-" << available_methods.size() <<
"): ";
253 std::cin >> method_choice;
255 method_name = available_methods[method_choice - 1];
257 if (method_name ==
"Newton-Raphson Method")
260 std::cout <<
"Enter initial guess x0: ";
263 else if (method_name !=
"Problem Steps Mode" && method_name !=
"Compare All Methods")
266 std::cout <<
"Enter interval [a, b]:\n";
273 std::cout <<
"Invalid interval. 'a' should be less than 'b'. Please re-enter:\n";
281 if (method_name !=
"Problem Steps Mode" && method_name !=
"Compare All Methods")
284 std::cout <<
"Enter desired precision (e.g., 1e-14, up to 1e-16): ";
286 const long double min_tol = 1e-16L;
287 const long double max_tol = 1e-4L;
288 while (tol < min_tol || tol > max_tol)
290 std::cout <<
"Precision out of bounds (" << min_tol <<
" to " << max_tol <<
"). Please re-enter: ";
void run_method_user_selection(const std::string &method_name, std::function< long double(long double, long double, long double, int, std::vector< std::string > &, int)> method_func, long double a, long double b, long double tol, int max_iter)
void run_method(const std::string &method_name, std::function< long double(long double, long double, long double, int, std::vector< std::string > &, int)> method_func, long double a, long double b, long double tol, int max_iter, int decimal_places)