142{
143
144 std::vector<std::pair<long double, long double>> intervals = {
145 {-3.0L, -2.0L},
146 {0.0L, 1.0L},
147 {1.0L, 3.0L}
148 };
149
150
151 long double tol = 1e-14L;
152 int max_iter = 1000;
153
154
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
157 {
158 return bisection(a, b, tol, max_iter, iterations, decimal_places);
159 }},
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
161 {
162
163 long double initial_guess = (a + b) / 2.0L;
164 return newton_raphson(initial_guess, tol, max_iter, iterations, decimal_places);
165 }},
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
167 {
168 return hybrid_method(a, b, tol, max_iter, iterations, decimal_places);
169 }},
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
171 {
172 return brent_method(a, b, tol, max_iter, iterations, decimal_places);
173 }},
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
175 {
176 return ridder_method(a, b, tol, max_iter, iterations, decimal_places);
177 }}};
178
179
180 std::map<std::string, std::vector<RootInfo>> comparison_results;
181
182
183 for (const auto &method : methods)
184 {
185 for (const auto &interval : intervals)
186 {
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);
191 }
192 }
193
194
195 std::cout << "\n--- Comparison of All Methods (Precision: 1e-14) ---\n\n";
196
197
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";
205
206
207 std::cout << std::string(130, '-') << "\n";
208
209
210 for (const auto &method : methods)
211 {
212 std::cout << std::left << std::setw(25) << method.first;
213 for (size_t i = 0; i < intervals.size(); ++i)
214 {
215 if (comparison_results[method.first][i].root != comparison_results[method.first][i].root)
216 {
217
218 std::cout << std::left << std::setw(30) << "N/A"
219 << std::left << std::setw(15) << "N/A";
220 }
221 else
222 {
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;
225 }
226 }
227 std::cout << "\n";
228 }
229
230 std::cout << "\nNote: Precision is set to 1e-14, output displays 15 decimal places.\n\n";
231}
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)