-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
77 lines (66 loc) · 2.43 KB
/
Copy pathmain.cpp
File metadata and controls
77 lines (66 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
#include "linear_equation_solver.h"
#include "non_linear_equation_solver.h"
#include "utils.h"
#include "performance_analyzer.h"
using namespace std;
int main(){
int choice;
cout << "Select the type of equation:\n";
cout << "1. Non-Linear Equation\n";
cout << "2. Linear Equation\n";
cin >> choice;
if(choice == 1){
string equation;
cout << "Enter the polynomial equation (e.g., 2x^2 + 3x + 1): "<<endl;
cin.ignore();
getline(cin, equation);
try{
auto f=Utils::parseEquation(equation);
auto f_prime=Utils::parseDerivative(equation);
NonLinearEquationSolver solver(f, f_prime);
vector<string> Names={"Bisection Method", "Newton-Raphson Method", "Secant Method", "Fixed Point Iteration" };
for(int m_Choice = 1; m_Choice <= Names.size(); ++m_Choice){
double result;
double executionTime = measureExecutionTime([&]() {
result = solver.solve(m_Choice);
});
cout << fixed << setprecision(6);
cout << "Method: " << Names[m_Choice - 1] << "\n";
cout << "Root: " << result << "\n";
cout << "Time taken: " << executionTime << " ms\n\n";
}
}
catch(const exception& e){
cerr<< "An error occurred: "<<e.what() << endl;
}
}
else if(choice == 2){
int n;
cout<<"Enter the size of the matrix (n x n):\n";
cin >> n;
Matrix A = Utils::inputMatrix(n);
Vector b = Utils::inputVector(n);
LinearEquationSolver solver(A,b);
vector<string> Names={ "Gaussian Elimination", "LU Decomposition", "Jacobi Method", "Gauss-Seidel Method" };
for(int m_Choice = 1; m_Choice<=4; ++m_Choice){
Vector solution;
double executionTime = measureExecutionTime([&](){
solution = solver.solve(m_Choice);
});
cout<<"Method: "<<Names[m_Choice - 1] <<endl;
cout << "Solution:"<<endl;
for(size_t i = 0; i < solution.size(); ++i){
cout << "x[" << i << "] = " << fixed << setprecision(6) << solution[i] <<endl;
}
cout << "Time taken: " << executionTime << " ms"<<endl;
}
}
else{
cout<<"Invalid choice.\n";
}
return 0;
}