-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconsole_app.cpp
More file actions
955 lines (801 loc) · 27.4 KB
/
console_app.cpp
File metadata and controls
955 lines (801 loc) · 27.4 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
#include <bits/stdc++.h>
#define EPSILON 0.000001
using namespace std;
void jacobi_iteration(const vector<vector<double>> &a, const vector<double> &b, vector<double> &x_init, int max_iterations, double tolerance)
{
int n = b.size();
vector<double> x = x_init;
vector<double> x_new(n);
vector<double> mae_list;
cout << setw(10) << "Iteration" << setw(10) << "xi" << setw(10) << "yi" << setw(10) << "zi"
<< setw(10) << "x(i+1)" << setw(10) << "y(i+1)" << setw(10) << "z(i+1)"
<< setw(10) << "ex" << setw(10) << "ey" << setw(10) << "ez"
<< setw(10) << "mae" << setw(10) << "mse" << setw(10) << "rmse" << endl;
for (int iteration = 0; iteration < max_iterations; iteration++)
{
for (int i = 0; i < n; i++)
{
double sum_ax = 0;
for (int j = 0; j < n; j++)
{
if (i != j)
{
sum_ax += a[i][j] * x[j];
}
}
x_new[i] = (b[i] - sum_ax) / a[i][i];
}
double ex = (n > 0) ? abs(x_new[0] - x[0]) : 0;
double ey = (n > 1) ? abs(x_new[1] - x[1]) : 0;
double ez = (n > 2) ? abs(x_new[2] - x[2]) : 0;
double mae = (ex + ey + ez) / n;
double mse = (ex * ex + ey * ey + ez * ez) / n;
double rmse = sqrt(mse);
cout << setw(10) << iteration + 1 << setw(10) << fixed << setprecision(4) << x[0]
<< setw(10) << x[1] << setw(10) << x[2]
<< setw(10) << x_new[0] << setw(10) << x_new[1] << setw(10) << x_new[2]
<< setw(10) << ex << setw(10) << ey << setw(10) << ez
<< setw(10) << mae << setw(10) << mse << setw(10) << rmse << endl;
if (sqrt(pow(x_new[0] - x[0], 2) + pow(x_new[1] - x[1], 2) + pow(x_new[2] - x[2], 2)) < tolerance)
{
cout << "Converged after " << iteration + 1 << " iterations." << endl;
break;
}
x = x_new;
mae_list.push_back(mae);
}
}
void gauss_seidel(const vector<vector<double>> &a, const vector<double> &b, int max_iterations, double tolerance)
{
int n = b.size();
vector<double> x(n, 0);
vector<double> mae_list;
cout << setw(10) << "Iteration" << setw(10) << "xi" << setw(10) << "yi" << setw(10) << "zi"
<< setw(10) << "x(i+1)" << setw(10) << "y(i+1)" << setw(10) << "z(i+1)"
<< setw(10) << "ex" << setw(10) << "ey" << setw(10) << "ez"
<< setw(10) << "mae" << setw(10) << "mse" << setw(10) << "rmse" << endl;
for (int iteration = 0; iteration < max_iterations; iteration++)
{
vector<double> x_new = x;
for (int i = 0; i < n; i++)
{
double sum_ax = 0;
for (int j = 0; j < n; j++)
{
if (i != j)
{
sum_ax += a[i][j] * x_new[j];
}
}
x_new[i] = (b[i] - sum_ax) / a[i][i];
}
double ex = (n > 0) ? abs(x_new[0] - x[0]) : 0;
double ey = (n > 1) ? abs(x_new[1] - x[1]) : 0;
double ez = (n > 2) ? abs(x_new[2] - x[2]) : 0;
double mae = (ex + ey + ez) / n;
double mse = (ex * ex + ey * ey + ez * ez) / n;
double rmse = sqrt(mse);
cout << setw(10) << iteration + 1 << setw(10) << fixed << setprecision(4) << x[0]
<< setw(10) << x[1] << setw(10) << x[2]
<< setw(10) << x_new[0] << setw(10) << x_new[1] << setw(10) << x_new[2]
<< setw(10) << ex << setw(10) << ey << setw(10) << ez
<< setw(10) << mae << setw(10) << mse << setw(10) << rmse << endl;
if (sqrt(pow(x_new[0] - x[0], 2) + pow(x_new[1] - x[1], 2) + pow(x_new[2] - x[2], 2)) < tolerance)
{
cout << "Converged after " << iteration + 1 << " iterations." << endl;
break;
}
x = x_new;
mae_list.push_back(mae);
}
}
void calculate_errors(const vector<double> &true_values, const vector<double> &estimated_values, double &mse, double &mae, double &rmse)
{
double sum_squared_error = 0;
double sum_absolute_error = 0;
int n = true_values.size();
for (int i = 0; i < n; ++i)
{
double error = true_values[i] - estimated_values[i];
sum_squared_error += error * error;
sum_absolute_error += abs(error);
}
mse = sum_squared_error / n;
mae = sum_absolute_error / n;
rmse = sqrt(mse);
}
void printMatrix(const vector<vector<double>> &matrix)
{
for (const auto &row : matrix)
{
for (double val : row)
{
cout << setw(10) << setprecision(4) << val << " ";
}
cout << endl;
}
cout << endl;
}
void rearrangeRows(vector<vector<double>> &matrix, int col)
{
int n = matrix.size();
int flag = 0;
for (int row = 0; row < n; ++row)
{
if (matrix[row][col] == 0)
{
for (int l = 0; l < col; l++)
{
if (matrix[row][l] < 0 || matrix[row][l] > 0)
{
flag = 1;
break;
}
}
if (flag == 0)
{
for (int k = row + 1; k < n; ++k)
{
if (matrix[k][col] != 0)
{
swap(matrix[row], matrix[k]);
break;
}
}
}
}
}
}
vector<vector<double>> gaussElimination(vector<vector<double>> &matrix)
{
int n = matrix.size();
int m = matrix[0].size();
for (int col = 0; col < n - 1; ++col)
{
rearrangeRows(matrix, col);
for (int row = col + 1; row < n; ++row)
{
if (matrix[row][col] != 0)
{
double multiplier = matrix[row][col] / matrix[col][col];
for (int j = col; j < m; ++j)
{
matrix[row][j] -= multiplier * matrix[col][j];
}
}
}
}
return matrix;
}
vector<vector<double>> gaussJordan(vector<vector<double>> &matrix)
{
int n = matrix.size();
int m = matrix[0].size();
for (int col = 0; col < n; ++col)
{
rearrangeRows(matrix, col);
for (int row = 0; row < n; ++row)
{
if (row != col)
{
double multiplier = matrix[row][col] / matrix[col][col];
for (int j = 0; j < m; ++j)
{
matrix[row][j] -= multiplier * matrix[col][j];
}
}
}
}
return matrix;
}
struct ab_pair
{
float a;
float b;
};
vector<float> taking_input_co_efficient_arr(int power_of_equation)
{
vector<float> co_efficient_arr(power_of_equation + 1, 0);
cout<<"Enter coefficients of equation : ";
for (int i = 0; i < power_of_equation + 1; i++)
{
cin >> co_efficient_arr[i];
}
return co_efficient_arr;
}
float equation_value(float x, int power_of_equation, vector<float> co_efficient_arr)
{
float value = 0;
float power = power_of_equation;
// cout<<"check1:"<<co_efficient_arr.size()<<endl;
for (int i = 0; i < co_efficient_arr.size(); i++)
{
value = value + pow(x, power) * co_efficient_arr[i];
// cout << "check2:" << value << endl;
power--;
}
return value;
}
float derivative_equation_value(float x, int power_of_equation, vector<float> co_efficient_arr)
{
int co_power = power_of_equation;
float value = 0;
int power = power_of_equation - 1;
for (int i = 0; i < co_efficient_arr.size() - 1; i++)
{
value += pow(x, power) * co_efficient_arr[i] * co_power;
power--;
co_power--;
}
return value;
}
vector<ab_pair> make_pairab(int power_of_equation, vector<float> co_efficient_arr)
{
float range_a = 0;
float range_b = 0;
float c = sqrt((pow((co_efficient_arr[1] / co_efficient_arr[0]), 2) - 2 * (co_efficient_arr[2] / co_efficient_arr[0])));
range_a = floor(-1 * c);
range_b = ceil(1 * c);
vector<ab_pair> pairab;
pairab.reserve(power_of_equation);
float temp_a = range_a;
float temp_b = range_a + 1;
while (temp_b < range_b + 1)
{
if (equation_value(temp_a, power_of_equation, co_efficient_arr) * equation_value(temp_b, power_of_equation, co_efficient_arr) < 0)
{
pairab.push_back({temp_a, temp_b});
}
temp_a = temp_b;
temp_b = temp_b + 1;
}
return pairab;
}
float bisection_method(float temp_a, float temp_b, int power_of_equation, vector<float> co_efficient_arr)
{
// int temp_no_of_iteration = 1;
float mid = temp_a + (temp_b - temp_a) / 2;
float prev_mid = mid;
while (fabs(equation_value(mid, power_of_equation, co_efficient_arr)) > 0.000001) // To check floating point tolerance fabs function is necessary
{
// temp_no_of_iteration++;
if (equation_value(temp_a, power_of_equation, co_efficient_arr) * equation_value(mid, power_of_equation, co_efficient_arr) < 0)
{
temp_b = mid;
}
else
{
temp_a = mid;
}
mid = temp_a + (temp_b - temp_a) / 2;
cout << fabs((mid - prev_mid)) << endl;
prev_mid = mid;
}
// no_of_iteration += temp_no_of_iteration;
return mid;
}
float false_position_method(float temp_a, float temp_b, float power_of_equation, vector<float> co_efficient_arr)
{
// int temp_no_of_iteration = 1;
float ftemp_b = equation_value(temp_b, power_of_equation, co_efficient_arr);
float ftemp_a = equation_value(temp_a, power_of_equation, co_efficient_arr);
float mid = (temp_a * ftemp_b - temp_b * ftemp_a) / (ftemp_b - ftemp_a);
float prev_mid = mid;
while (fabs(equation_value(mid, power_of_equation, co_efficient_arr)) > 0.000001) // flaoting point tolarance check e fabs deya lagbe
{
// temp_no_of_iteration++;
if (equation_value(temp_a, power_of_equation, co_efficient_arr) * equation_value(mid, power_of_equation, co_efficient_arr) < 0)
{
temp_b = mid;
}
else
{
temp_a = mid;
}
ftemp_b = equation_value(temp_b, power_of_equation, co_efficient_arr);
ftemp_a = equation_value(temp_a, power_of_equation, co_efficient_arr);
mid = (temp_a * ftemp_b - temp_b * ftemp_a) / (ftemp_b - ftemp_a);
// cout<<fabs((mid-prev_mid))<<endl;
prev_mid = mid;
}
// no_of_iteration += temp_no_of_iteration;
return mid;
}
float scant_method(float x, float y, int power_of_equation, vector<float> co_efficient_arr)
{
float f_x = equation_value(x, power_of_equation, co_efficient_arr);
float f_y = equation_value(y, power_of_equation, co_efficient_arr);
float z;
// Ensuring that f(x) and f(y) are not equal initially
if (fabs(f_x - f_y) < EPSILON)
{
//cout << "Warning: Initial points too close or identical function values." << endl;
return x;
}
while (fabs(y - x) > EPSILON)
{
z = y - f_y * (y - x) / (f_y - f_x);
x = y;
f_x = f_y;
y = z;
f_y = equation_value(z, power_of_equation, co_efficient_arr);
}
return z;
}
float newton_raphson_method(float x, int power_of_equation, vector<float> co_efficient_arr)
{
float h = equation_value(x, power_of_equation, co_efficient_arr) / derivative_equation_value(x, power_of_equation, co_efficient_arr);
int iteration = 0;
float y = 0;
while (fabs(h) > EPSILON)
{
y = x;
x = y - h;
h = equation_value(x, power_of_equation, co_efficient_arr) / derivative_equation_value(x, power_of_equation, co_efficient_arr);
// iteration++;
cout << fabs(x - y) << endl;
}
// cout << "No of iteration :" << iteration << endl;
return x;
}
void printing_roots(int power_of_equation, vector<float> result)
{
for (int i = 0; i < power_of_equation; i++)
{
cout << "Root" << i + 1 << " : " << result[i] << endl;
}
}
vector<float> function_call(int power_of_equation, vector<ab_pair> pairab, vector<float> co_efficient_arr, int method_no)
{
vector<float> result;
result.resize(power_of_equation);
if (method_no == 1)
{
for (int i = 0; i < power_of_equation; i++)
{
result[i] = (bisection_method(pairab[i].a, pairab[i].b, power_of_equation, co_efficient_arr));
}
cout << "Solved by Bisection method" << endl;
}
else if (method_no == 2)
{
for (int i = 0; i < power_of_equation; i++)
{
result[i] = (false_position_method(pairab[i].a, pairab[i].b, power_of_equation, co_efficient_arr));
}
cout << "Solved by False Position method" << endl;
}
else if (method_no == 3)
{
for (int i = 0; i < power_of_equation; i++)
{
result[i] = newton_raphson_method(((pairab[i].a + pairab[i].b) / 2.0), power_of_equation, co_efficient_arr);
}
cout << "Solved by Newton-Raphson method" << endl;
}
else if (method_no == 4)
{
for (int i = 0; i < power_of_equation; i++)
{
result[i] = (scant_method(pairab[i].a + 0.5, pairab[i].b + 0.5, power_of_equation, co_efficient_arr));
// 0.5 is added for random initial value
}
cout << "Solved by Scant method" << endl;
}
else
{
cout << "Select a valid method" << endl;
}
return result;
}
// Function to perform LU Decomposition
void luDecomposition(const vector<vector<double>> &mat, int n, vector<vector<double>> &lower, vector<vector<double>> &upper)
{
for (int i = 0; i < n; i++)
{
for (int k = i; k < n; k++)
{
double sum = 0;
for (int j = 0; j < i; j++)
{
sum += lower[i][j] * upper[j][k];
}
upper[i][k] = mat[i][k] - sum;
}
for (int k = i; k < n; k++)
{
if (i == k)
{
lower[i][i] = 1;
}
else
{
double sum = 0;
for (int j = 0; j < i; j++)
{
sum += lower[k][j] * upper[j][i];
}
lower[k][i] = (mat[k][i] - sum) / upper[i][i];
}
}
}
}
// Forward substitution to solve L*y = b
void forwardSubstitution(const vector<vector<double>> &L, vector<double> &y, const vector<double> &b, int n)
{
for (int i = 0; i < n; i++)
{
double sum = 0;
for (int j = 0; j < i; j++)
{
sum += L[i][j] * y[j];
}
y[i] = (b[i] - sum);
}
}
// Backward substitution to solve U*x = y
void backwardSubstitution(const vector<vector<double>> &U, vector<double> &x, const vector<double> &y, int n)
{
for (int i = n - 1; i >= 0; i--)
{
double sum = 0;
for (int j = i + 1; j < n; j++)
{
sum += U[i][j] * x[j];
}
x[i] = (y[i] - sum) / U[i][i];
}
}
// Function to display a vector
void displayVector(const vector<double> &vec)
{
for (double val : vec)
{
cout << setw(10) << setprecision(5) << val << " ";
}
cout << endl;
}
// Function to display a matrix
void displayMatrix(const vector<vector<double>> &matrix, int n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout << setw(10) << setprecision(5) << matrix[i][j] << " ";
}
cout << endl;
}
}
// Function to calculate the inverse of a matrix using LU decomposition
vector<vector<double>> matrixInverse(const vector<vector<double>> &lower, const vector<vector<double>> &upper, int n)
{
vector<vector<double>> inverse(n, vector<double>(n));
for (int i = 0; i < n; i++)
{
vector<double> b(n, 0);
b[i] = 1; // Setting the i-th element to 1 for the identity matrix
vector<double> y(n, 0);
vector<double> x(n, 0);
// Solving for each column of the inverse
forwardSubstitution(lower, y, b, n);
backwardSubstitution(upper, x, y, n);
// Assign the solution to the corresponding column of the inverse matrix
for (int j = 0; j < n; j++)
{
inverse[j][i] = x[j];
}
}
return inverse;
}
// Runge-Kutta 4th order method for a linear ODE
void rungeKuttaLinear(double y0, double t0, double t_end, double h)
{
double t = t0;
double y = y0;
cout << "t\t y (Linear ODE)" << endl;
// Linear differential equation: dy/dt = ay + b
double a = 1.0; // Coefficient
double b = 0.0; // Constant term
while (t <= t_end)
{
cout << t << "\t " << y << endl;
double k1 = h * (a * y + b);
double k2 = h * (a * (y + k1 / 2) + b);
double k3 = h * (a * (y + k2 / 2) + b);
double k4 = h * (a * (y + k3) + b);
y += (k1 + 2 * k2 + 2 * k3 + k4) / 6; // Update y
t += h; // Increment time
}
}
// Runge-Kutta 4th order method for a trigonometric ODE
void rungeKuttaTrigonometric(double y0, double t0, double t_end, double h)
{
double t = t0;
double y = y0;
cout << "\nSolving Trigonometric ODE:" << endl;
cout << "t\t y (Trigonometric ODE)" << endl;
// Trigonometric differential equation: dy/dt = sin(t)
while (t <= t_end)
{
cout << t << "\t " << y << endl;
double k1 = h * sin(t);
double k2 = h * sin(t + h / 2);
double k3 = h * sin(t + h / 2);
double k4 = h * sin(t + h);
y += (k1 + 2 * k2 + 2 * k3 + k4) / 6; // Update y
t += h; // Increment time
}
}
void slnoflinear()
{
cout << "1. Jacobi iterative method" << endl
<< "2. Gauss-Seidel iterative method" << endl
<< "3. Gauss elimination" << endl
<< "4. Gausss-Jordan elimination" << endl
<< "5. LU factorization" << endl;
int select;
cout << "Select what type of equation you want to solve:";
cin >> select;
if (select == 1)
{
int n;
cout << "Enter the number of equations (and variables): ";
cin >> n;
vector<vector<double>> a(n, vector<double>(n));
vector<double> b(n);
cout << "Enter the coefficients of the equations (" << n << "x" << n + 1 << " matrix):" << endl;
for (int i = 0; i < n; i++)
{
cout << "Row " << i + 1 << " (coefficients followed by constant): ";
for (int j = 0; j < n; j++)
{
cin >> a[i][j];
}
cin >> b[i];
}
int max_iterations;
double tolerance;
cout << "Enter the maximum number of iterations: ";
cin >> max_iterations;
cout << "Enter the tolerance for convergence: ";
cin >> tolerance;
vector<double> x_init(n, 0.0);
jacobi_iteration(a, b, x_init, max_iterations, tolerance);
vector<double> true_values(n, 1.0);
vector<double> final_solution_jacobi(n, 0.0);
double mse_jacobi, mae_jacobi, rmse_jacobi;
calculate_errors(true_values, final_solution_jacobi, mse_jacobi, mae_jacobi, rmse_jacobi);
cout << "Jacobi Method: MSE: " << mse_jacobi << ", MAE: " << mae_jacobi << ", RMSE: " << rmse_jacobi << endl;
}
else if (select == 2)
{
int n;
cout << "Enter the number of equations (and variables): ";
cin >> n;
vector<vector<double>> a(n, vector<double>(n));
vector<double> b(n);
cout << "Enter the coefficients of the equations (" << n << "x" << n + 1 << " matrix):" << endl;
for (int i = 0; i < n; i++)
{
cout << "Row " << i + 1 << " (coefficients followed by constant): ";
for (int j = 0; j < n; j++)
{
cin >> a[i][j];
}
cin >> b[i];
}
int max_iterations;
double tolerance;
cout << "Enter the maximum number of iterations: ";
cin >> max_iterations;
cout << "Enter the tolerance for convergence: ";
cin >> tolerance;
vector<double> x_init(n, 0.0);
gauss_seidel(a, b, max_iterations, tolerance);
vector<double> true_values(n, 1.0);
vector<double> final_solution_jacobi(n, 0.0);
vector<double> final_solution_gs(n, 0.0);
double mse_gs, mae_gs, rmse_gs;
calculate_errors(true_values, final_solution_gs, mse_gs, mae_gs, rmse_gs);
cout << "Gauss-Seidel Method: MSE: " << mse_gs << ", MAE: " << mae_gs << ", RMSE: " << rmse_gs << endl;
}
else if (select == 3)
{
int n;
cout << "Enter the number of equations (and variables): ";
cin >> n;
vector<vector<double>> matrix(n, vector<double>(n + 1));
for (int i = 0; i < n; i++)
{
for (int j = 0; j <= n; j++)
{
cin >> matrix[i][j];
}
}
cout << "Original Matrix:\n";
printMatrix(matrix);
// Perform Gauss Elimination
matrix = gaussElimination(matrix);
cout << "\nGauss Elimination Matrix\n";
printMatrix(matrix);
}
else if (select == 4)
{
int n;
cout << "Enter the number of equations (and variables): ";
cin >> n;
vector<vector<double>> matrix(n, vector<double>(n + 1));
for (int i = 0; i < n; i++)
{
for (int j = 0; j <= n; j++)
{
cin >> matrix[i][j];
}
}
cout << "Original Matrix:\n";
printMatrix(matrix);
// Perform Gauss Elimination
matrix = gaussElimination(matrix);
matrix = gaussJordan(matrix);
cout << "\nGauss Jordan Elimination matrix\n";
printMatrix(matrix);
cout << endl;
}
else if (select == 5)
{
int n;
cout << "Enter the number of variables: ";
cin >> n;
vector<vector<double>> mat(n, vector<double>(n));
// Taking user input for the matrix A
cout << "Enter the elements of the matrix A:" << endl;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout << "Element [" << i << "][" << j << "]: ";
cin >> mat[i][j];
}
}
// Declaring lower and upper matrices
vector<vector<double>> lower(n, vector<double>(n, 0));
vector<vector<double>> upper(n, vector<double>(n, 0));
// Performing LU Decomposition
luDecomposition(mat, n, lower, upper);
vector<double> b(n);
cout << "Enter the elements of the vector b:" << endl;
for (int i = 0; i < n; i++)
{
cout << "Element [" << i << "]: ";
cin >> b[i];
}
vector<double> y(n, 0); // Solution to L*y = b
vector<double> x(n, 0); // Solution to U*x = y
forwardSubstitution(lower, y, b, n);
backwardSubstitution(upper, x, y, n);
cout << "\nSolution vector x:" << endl;
displayVector(x);
}
else
cout << "NO method selected";
}
void slnofnonlinear()
{
cout << "1.Bisection Method" << endl
<< "2.False Position Method" << endl
<< "3.Newton-Raphson Method" << endl
<< "4.Secant Method" << endl;
int select;
cout<<"Select a method : ";
cin>>select;
cout << "Enter no of degree for equation : ";
int power_of_equation = 0;
cin >> power_of_equation;
// declaring co efficient vector and taking input from function
vector<float> co_efficient_arr = taking_input_co_efficient_arr(power_of_equation);
// declaring pair/range(a,b) vector and initializing it by function
vector<ab_pair> pairab = make_pairab(power_of_equation, co_efficient_arr);
// declaring result vector and initializing it with calling specific function
vector<float> result = function_call(power_of_equation, pairab, co_efficient_arr, select);
// printing result vector by function
printing_roots(power_of_equation, result);
}
void slnofdiff()
{
double y0;
double t0;
double t_end;
double h;
cout << "Enter the value of Initial Condition, Initial time, End time, step size" << endl;
cin >> y0 >> t0 >> t_end >> h;
int choice;
cout << "1. Solve Linear equation:" << endl
<< "2. Trigonometric (sine) equation" << endl;
cout << "Enter your choice: ";
cin >> choice;
if (choice == 1)
{
rungeKuttaLinear(y0, t0, t_end, h);
}
else if (choice == 2)
{
rungeKuttaTrigonometric(y0, t0, t_end, h);
}
else
{
cout << "No solution choose" << endl;
}
}
void inversematrix()
{
int n;
cout << "Enter the number of variables: ";
cin >> n;
vector<vector<double>> mat(n, vector<double>(n));
// Taking user input for the matrix A
cout << "Enter the elements of the matrix A:" << endl;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout << "Element [" << i << "][" << j << "]: ";
cin >> mat[i][j];
}
}
// Declaring lower and upper matrices
vector<vector<double>> lower(n, vector<double>(n, 0));
vector<vector<double>> upper(n, vector<double>(n, 0));
// Performing LU Decomposition
luDecomposition(mat, n, lower, upper);
vector<vector<double>> inverse = matrixInverse(lower, upper, n);
cout << "\nInverse of Matrix A:" << endl;
displayMatrix(inverse, n);
}
int main()
{
char c;
while (1)
{
cout << "want to solve a equation(y/n):" << endl;
cout << "Enter your choice :";
cin >> c;
if (c == 'y')
{
cout << "1. Solution of Linear Equations" << endl
<< "2. Solution of Non-linear Equations" << endl
<< "3. Solution of Differential Equations" << endl
<< "4.Matrix inversion" << endl;
int select;
cout << "Select what type of equation you want to solve:";
cin >> select;
if (select == 1)
{
slnoflinear();
cout << endl;
}
else if (select == 2)
{
slnofnonlinear();
cout << endl;
}
else if (select == 3)
{
slnofdiff();
cout << endl;
}
else if (select == 4)
{
inversematrix();
}
else
cout << "Nothing is seleceted" << endl;
}
else
{
cout << "No equation want to solve" << endl;
break;
}
}
return 0;
}