# Untitled

May 10th, 2020
133
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #define _CRT_SECURE_NO_WARNINGS
2. #include <stdio.h>
3. #include <stdlib.h>
4. #include <math.h>
5. #include <time.h>
6. #include <string.h>
7.
8. typedef struct point {
9. double x;
10. double y;
11. int flag;
12. }POINT;
13.
14. double func(double x) {
15. return pow((x - 2), 2);
16. }
17.
18. double calcDot(double (*func)(double), double L, double x1, double x2) {
19. return 1 / (2 * L) * (L * (x1 + x2) + func(x1) - func(x2));
20. }
21.
22. double lowGrade(double (*func)(double), double L, double x1, double x2) {
23. return (func(x1) + func(x2)) / 2 - L / 2 * (x2 - x1);
24. }
25.
26. double calcL(double a, double b) {
27. if ((2 * (a - 2)) > (2 * (b - 2))) {
28. return (2 * (a - 2));
29. }
30. else {
31. return (2 * (b - 2));
32. }
33. }
34.
35.
36.
37. int main() {
38. POINT arr[1000] = { 0 };
39.
40. double a = 0, b = 5, accuracy = 0.1, L = calcL(a, b);
41. double y = calcDot(&func, L, a, b);
42. arr[0] = { a, func(a), 0 };
43. arr[1] = { y,func(y),1 };
44. arr[2] = { b,func(b),0 };
45. int f = 2;
46. double upperLimit = fmin(arr[0].y, arr[2].y);
47. double lowerLimit = lowGrade(&func, L, a, b);
48.
49. int n = 1;
50.
51. while (upperLimit - lowerLimit > accuracy) {
52. double min = upperLimit;
53. int r = 0;
54. for (int i = 0; i < f + 1; i++) {
55. if ((arr[i].flag != 0) && (lowerLimit <= arr[i].y) && (arr[i].y < min)) {
56. min = arr[i].y;
57. r = i;
58. }
59. }
60. upperLimit = fmin(upperLimit, func(arr[r].x));
61. lowerLimit = min;
62.
63. arr[r].y = func(arr[r].x);
64. arr[r].flag = 0;
65. arr[f + 2] = { arr[f].x,arr[f].y,arr[f].flag };
66. arr[r + 1] = { arr[r].x,arr[r].y,arr[r].flag };
67. arr[r] = { calcDot(&func,L,arr[r-1].x,arr[r].x), lowGrade(&func, L, arr[r-1].x,arr[r].x), 1 };
68. arr[r + 2] = { calcDot(&func,L,arr[r].x,arr[r + 1].x), lowGrade(&func, L, arr[r].x,arr[r + 1].x), 1 };
69. n++;
70. }
71. printf("[%.4lf;%.4lf]\nIteretion:%d", lowerLimit, upperLimit, n);
72.
73. return 0;
74. }