• API
• FAQ
• Tools
• Archive
SHARE
TWEET Untitled a guest Sep 21st, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <iostream>
2. #include <vector>
3. #include <cmath>
4.
5. double eps, a, b, tab_step;
6.
7. std::vector<double> root_segs;
8.
9. double f(double x) {
10.   return sqrt(4.0*x+7.0) - 3.0 * cos(x);
11. }
12.
13. double fprime(double x) {
14.   return 3 * sin(x) + 2.0/sqrt(4*x+7);
15. }
16.
17. void in_par() {
18.   //std::cin >> tab_step >> eps;
19.   //std::cin >> a >> b;
20.   a = -1.5; b = 2;
21.   eps = 1e-8; tab_step = 0.01;
22. }
23.
24. void print_seg(int i) {
25.   std::cout << "[" << root_segs[i] << ", " << (root_segs[i]+tab_step) << "]";
26. }
27.
28. void sep() {
29.   for (double x = a; x <= b; x += tab_step)
30.     if (f(x) * f(x + tab_step) <= 0)
31.       root_segs.push_back(x);
32.   if (!root_segs.size()) {
33.     std::cout << "No roots present in requested segment, terminating\n";
34.     exit(0);
35.   }
36.   std::cout << "Root separation yielded following segments:" << std::endl;
37.   print_seg(0);
38.   for (int i = 1; i < root_segs.size(); i++)
39.     std::cout << ", ", print_seg(i);
40.   std::cout << std::endl;
41. }
42.
43. void bisect(double l, double r) {
44.   std::cout << "Bisection method; [" << l << ", " << r << "]: ";
45.   int step = 0;
46.   while (r - l > 2 * eps) {
47.     ++step;
48.     double mid = (l + r) / 2.0;
49.     if (f(mid) * f(r) < 0)
50.       l = mid;
51.     else
52.       r = mid;
53.   }
54.   double ans = (r+l)/2.0;
55.   std::cout
56.     << step << " steps, ans " << ans
57.     << ", |r| = " << fabs(f(ans))
58.     << ", fsl = " << (r-l)
59.     << std::endl;
60. }
61.
62. void newton(double xs) {
63.   std::cout << "Newton's method; x_0 = " << xs << "; ";
64.   int steps = 0;
65.   double nx = xs, px = xs;
66.   do {
67.     ++steps;
68.     px = nx;
69.     nx = nx - f(nx) / fprime(nx);
70.   } while (nx - px > eps);
71.   std::cout
72.     << steps << " steps, ans " << nx
73.     << ", |r| = " << fabs(f(nx))
74.     << std::endl;
75. }
76.
77. void mod_newton(double xs) {
78.   std::cout << "Modified Newton's method; x_0 = " << xs << "; ";
79.   int steps = 0;
80.   double nx = xs, px = xs;
81.   double fpr0 = fprime(xs);
82.   do {
83.     ++steps;
84.     px = nx;
85.     nx = nx - f(nx) / fpr0;
86.   } while (nx - px > eps);
87.   std::cout
88.     << steps << " steps, ans " << nx
89.     << ", |r| = " << fabs(f(nx))
90.     << std::endl;
91. }
92.
93. void secant(double k, double l) {
94.   std::cout << "Secant method; x_0 = " << k << ", x_1 = " << l;
95.   int steps = 0;
96.   while (fabs(k - l) > eps) {
97.     ++steps;
98.     double fs = f(l);
99.     double n = l - fs * ((l - k) / (fs - f(k)));
100.     k = l; l = n;
101.   }
102.   std::cout
103.     << ", " << steps
104.     << " steps, ans " << l
105.     << ", |r| = " << fabs(f(l))
106.     << std::endl;
107. }
108.
109. int main(int argc, char** argv) {
110.   std::cout << "LW 1: numerical methods of solving nonlinear equations\n";
111.   std::cout << "Variant 4, f(x) := sqrt(4*x + 7) - 3*cos(x)\n";
112.
113.   std::cout.precision(10);
114.   in_par();
115.
116.   std::cout
117.     << "A = " << a << "; B = " << b
118.     << "; h = " << tab_step
119.     << "; eps = " << eps << std::endl;
120.
121.   std::cout << std::endl;
122.   sep();
123.   std::cout << std::endl;
124.   for (int i = 0; i < root_segs.size(); i++)
125.     bisect(root_segs[i], root_segs[i] + tab_step);
126.   for (int i = 0; i < root_segs.size(); i++)
127.     newton(root_segs[i]);
128.   for (int i = 0; i < root_segs.size(); i++)
129.     mod_newton(root_segs[i]);
130.   for (int i = 0; i < root_segs.size(); i++)
131.     secant(root_segs[i], root_segs[i] + tab_step);
132.
133.   return 0;
134. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top