# method_Bisec,Hord,Newton(lab_4, BM)

Feb 27th, 2021
1,068
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <iostream>
2. #include <cmath>
3. using namespace std;
4.
5. float f(float X);
6. float df(float X);
7. float bissec(float X_L, float X_R, float E);
8. float Newton(float X_L, float X_R, float E);
9. float hord(float X_L, float X_R, float E);
10.
11.
12. int main()
13. {
14.     float x_l, x_r, e, x = 0;
15.     int c;
16.     cout << "Enter the segment:\nX_left = "; cin >> x_l;
17.     cout << "X_right = "; cin >> x_r;
18.     cout << "Enter the E:\tE = "; cin >> e;
19.     cout << "Select a method: Bisection(0), Newton(1), Hord(2)\n->";
20.     cin >> c;
21.     switch (c)
22.     {
23.     case 0:
24.         x = bissec(x_l, x_r, e);
25.         break;
26.     case 1:
27.         x = Newton(x_l, x_r, e);
28.         break;
29.     case 2:
30.         x = hord(x_l, x_r, e);
31.         break;
32.     }
33.     cout << "\n\nAnswer: X = " << round(x * 1000) / 1000;
34.
35.     return 0;
36. }
37.
38. float f(float X)
39. {
40.     return pow(X, 2) - cos(X) + X;
41. }
42.
43. float df(float X)
44. {
45.     return 2 * X + sin(X) + 1;
46. }
47.
48. float bissec(float X_L, float X_R, float E)
49. {
50.     float X = 0;
51.     int c;
52.     if (f(X_L) * f(X_R) < 0)
53.     {
54.         cout << "The convergence condition is met!\n";
55.         c = 0;
56.         while (1)
57.         {
58.             X = (X_L + X_R) / 2;
59.             c++;
60.             if (fabs(f(X)) < E) break;
61.             if (f(X_L) * f(X) < 0) X_R = X;
62.             else X_L = X;
63.             cout << "\nIteration " << c << ":\tx_left = " << round(X_L * 1000) / 1000 << "\tx_right = " << round(X_R * 1000) / 1000;
64.         }
65.     }
66.     else cout << "The convergence condition is not met!\n";
67.
68.     return X;
69. }
70.
71. float Newton(float X_L, float X_R, float E)
72. {
73.     float X = 0;
74.     int c = 0;
75.     if (f(X_L) * f(X_R) < 0)
76.     {
77.         cout << "The convergence condition is met for X = X_left!\n";
78.         X = X_L;
79.     }
80.     else
81.     {
82.         if (f(X_L) * f(X_R) > 0)
83.         {
84.             cout << "The convergence condition is met for X = X_right!\n";
85.             X = X_R;
86.         }
87.         else
88.         {
89.             cout << "The convergence condition is not met!\n";
90.             return 0;
91.         }
92.     }
93.     while (1)
94.     {
95.         X = X - f(X) / df(X);
96.         c++;
97.         if (fabs(f(X)) < E) break;
98.         cout << "\nIteration " << c << ":\tX = " << round(X * 1000) / 1000;
99.     }
100.
101.     return X;
102. }
103.
104. float hord(float X_L, float X_R, float E)
105. {
106.     int c = 0;
107.     while (fabs(f(X_R)) > E)
108.     {
109.         c++;
110.         X_L = X_R - ((X_R - X_L) * f(X_R)) / (f(X_R) - f(X_L));
111.         X_R = X_L - ((X_L - X_R) * f(X_L)) / (f(X_L) - f(X_R));
112.         cout << "\nIteration " << c << ":\tx_left = " << round(X_L * 1000) / 1000 << "\tx_right = " << round(X_R * 1000) / 1000;
113.     }
114.
115.     return X_R;
116. }
117.
RAW Paste Data