Root Finder  1.0
Using various numerical methods
Loading...
Searching...
No Matches
main.cpp File Reference

The main entry point for the Root-Finding Algorithms Solver project. More...

#include "functions.h"
#include "methods.h"
#include "plotting.h"
#include "utils.h"
#include <iostream>
#include <limits>
#include <map>
#include <vector>
#include <functional>
#include <string>
#include <iomanip>
+ Include dependency graph for main.cpp:

Go to the source code of this file.

Functions

int main ()
 

Detailed Description

The main entry point for the Root-Finding Algorithms Solver project.

Author
Gilbert Young
Date
2024/09/19

Definition in file main.cpp.

Function Documentation

◆ main()

int main ( )

Definition at line 54 of file main.cpp.

55{
56 // Plot the function once at the beginning with range [-3, 3]
57 plot_function(-3.0L, 3.0L, -10.0L, 10.0L);
58
59 char choice;
60 do
61 {
62 long double a = 0.0L, b = 0.0L, x0 = 0.0L, tol = 1e-14L;
63 std::string method_name;
64
65 // Get user input for method selection
66 get_user_input(a, b, x0, method_name, tol);
67
68 int max_iter = 1000;
69
70 // Map of methods excluding Newton-Raphson and Compare All Methods
71 std::map<std::string, std::function<long double(long double, long double, long double, int, std::vector<std::string> &, int)>> methods = {
72 {"Bisection Method", [](long double a, long double b, long double tol, int max_iter, std::vector<std::string> &iterations, int decimal_places) -> long double
73 {
74 return bisection(a, b, tol, max_iter, iterations, decimal_places);
75 }},
76 {"Hybrid Method", [](long double a, long double b, long double tol, int max_iter, std::vector<std::string> &iterations, int decimal_places) -> long double
77 {
78 return hybrid_method(a, b, tol, max_iter, iterations, decimal_places);
79 }},
80 {"Brent Method", [](long double a, long double b, long double tol, int max_iter, std::vector<std::string> &iterations, int decimal_places) -> long double
81 {
82 return brent_method(a, b, tol, max_iter, iterations, decimal_places);
83 }},
84 {"Ridder Method", [](long double a, long double b, long double tol, int max_iter, std::vector<std::string> &iterations, int decimal_places) -> long double
85 {
86 return ridder_method(a, b, tol, max_iter, iterations, decimal_places);
87 }}};
88
89 if (method_name == "Newton-Raphson Method")
90 {
91 std::vector<std::string> iterations;
92 long double root = newton_raphson(x0, tol, max_iter, iterations, calculate_decimal_places(tol));
93 RootInfo info{root, static_cast<int>(iterations.size()), calculate_decimal_places(tol)};
94 summary[method_name].emplace_back(info);
95
96 // Display results
97 std::cout << "\nMethod: " << method_name << "\n";
98 std::cout << "Initial guess: x0 = " << std::fixed << std::setprecision(info.decimal_places) << x0 << "\n";
99 std::cout << "Root: " << std::fixed << std::setprecision(info.decimal_places) << root << "\n";
100 std::cout << "Iterations:\n";
101 for (const auto &iter : iterations)
102 {
103 std::cout << iter << "\n";
104 }
105 std::cout << "Iterations Count: " << iterations.size() << "\n";
106 }
107 else if (method_name == "Problem Steps Mode")
108 {
109 // Run the problem steps
111 }
112 else if (method_name == "Compare All Methods")
113 {
114 // Run the comparison
116 }
117 else
118 {
119 // Get the method function
120 auto it = methods.find(method_name);
121 if (it != methods.end() && it->second != nullptr)
122 {
123 run_method_user_selection(method_name, it->second, a, b, tol, max_iter);
124 }
125 else
126 {
127 std::cerr << "Method not found or not implemented.\n";
128 }
129 }
130
131 // Output summary of all results
132 if (method_name != "Problem Steps Mode" && method_name != "Compare All Methods")
133 {
134 std::cout << "\n--- Summary of All Results ---\n";
135 for (const auto &method : summary)
136 {
137 std::cout << "\nMethod: " << method.first << "\n";
138 int idx = 1;
139 for (const auto &info : method.second)
140 {
141 std::cout << " Root " << idx++ << ": " << std::fixed << std::setprecision(info.decimal_places) << info.root
142 << " | Iterations: " << info.iterations << "\n";
143 }
144 }
145 // Clear summary for next run
146 summary.clear();
147 }
148
149 // Ask user if they want to run again
150 std::cout << "\nDo you want to run the program again? (y/n): ";
151 std::cin >> choice;
152
153 } while (choice == 'y' || choice == 'Y');
154
155 // Pause and wait for user input before exiting
156 std::cout << "\nPress Enter to exit...";
157 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
158 std::cin.get();
159
160 return 0;
161}
long double bisection(long double a, long double b, long double tol, int max_iter, std::vector< std::string > &iterations, int decimal_places)
Definition methods.cpp:17
long double hybrid_method(long double a, long double b, long double tol, int max_iter, std::vector< std::string > &iterations, int decimal_places)
Definition methods.cpp:76
long double ridder_method(long double a, long double b, long double tol, int max_iter, std::vector< std::string > &iterations, int decimal_places)
Definition methods.cpp:226
long double newton_raphson(long double x0, long double tol, int max_iter, std::vector< std::string > &iterations, int decimal_places)
Definition methods.cpp:51
long double brent_method(long double a, long double b, long double tol, int max_iter, std::vector< std::string > &iterations, int decimal_places)
Definition methods.cpp:139
void plot_function(long double x_min, long double x_max, long double y_min, long double y_max, int width, int height, long double label_interval)
Definition plotting.cpp:20
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)
Definition utils.cpp:305
std::map< std::string, std::vector< RootInfo > > summary
Definition utils.cpp:24
void compare_all_methods()
Definition utils.cpp:141
void run_problem_steps()
Definition utils.cpp:58
void get_user_input(long double &a, long double &b, long double &x0, std::string &method_name, long double &tol)
Definition utils.cpp:234
int calculate_decimal_places(long double tol)
Definition utils.cpp:297