# Untitled

Apr 19th, 2020
137
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. // f(x, y) = 16x ^ 2 + 3y ^ 2, c = 13
8.
9. const int SIZE = 100;
10.
11.
12. double func(double a, double b, double x, double y) {
13.     return a * pow(x, 2) + b * pow(y, 2);
14. }
15.
16. double randOnRange(double min, double max) {
17.     double f = (double)rand() / RAND_MAX;
18.     return min + f * (max - min);
19. };
20.
21. double calculateX(double x, double L, const double& gradientX) {
23. };
24.
25. double calculateY(double y, double L, const double& gradientY) {
27. };
28.
29. double gradientX(double a, double x) {
30.     return 2 * a * x;
31. }
32. double gradientY(double b, double y) {
33.     return 2 * b * y;
34. }
35.
36. double max(double a, double b) {
37.     if (2 * a > 2 * b) {
38.         return 2 * a;
39.     }
40.     else return 2 * b;
41. }
42.
43. int maxN(const double& func, double accuracy, double L) {
44.     double N = ((2 * L) / (pow(accuracy, 2))) * func;
45.     floor(N);
46.     return N;
47. }
48.
51. }
52.
53. void firstTest()  {
54.     puts("test 1:");
55.     double a = 16;
56.     double b = 3;
57.     double c = 13;
58.     double accuracy = 0.01;
59.     double arrCoords[SIZE][2] = { 0 };
60.     long double count = 0;
61.     double L = max(a, b);
62.     int maxIter = 0;
63.     for (int i = 0; i < SIZE; i++) {
64.         arrCoords[i][0] = randOnRange(-sqrt(c / a), sqrt(c / a));
65.         arrCoords[i][1] = sqrt((c - a * pow(arrCoords[i][0], 2)) / b);
66.         //printf("[%.3lf;%.3lf]\t", arrCoords[i][0], arrCoords[i][1]);
67.     }
68.     for (int i = 0; i<SIZE;i++){
69.         maxIter += maxN(func(a, b, arrCoords[i][0], arrCoords[i][1]), accuracy, L);
70.         int k = 0;
72.             double x = arrCoords[i][0];
73.             double y = arrCoords[i][1];
74.             arrCoords[i][0] = calculateX(x, L, gradientX(a, x));
75.             arrCoords[i][1] = calculateY(y, L, gradientY(b, y));
76.
77.         }
78.         count += k;
79.         //printf("%d ", k);
80.         //printf("%d", maxIter);
81.         //printf("\n");
82.     }
83.
84.     printf("average max iteration  = %d\naverage real iteration = %.2lf\n", maxIter / SIZE, ceil(count / SIZE));
85. }
86.
87. void secondTest() {
88.     puts("test 2:");
89.     double a = 16;
90.     double b = 3;
91.     double c = 13;
92.     double accuracy = 0.01;
93.     double arrCoords[SIZE][2] = { 0 };
94.     long double count = 0;
95.     double L = max(a, b);
96.     int maxIter = 0;
97.     for (int i = 0; i < SIZE; i++) {
98.         arrCoords[i][0] = randOnRange(-0.5, 0.5);
99.         arrCoords[i][1] = sqrt((c - a * pow(arrCoords[i][0], 2)) / b);
100.         //printf("[%.3lf;%.3lf]\t", arrCoords[i][0], arrCoords[i][1]);
101.     }
102.     for (int i = 0; i < SIZE; i++) {
103.         maxIter += maxN(func(a, b, arrCoords[i][0], arrCoords[i][1]), accuracy, L);
104.         int k = 0;
106.             double x = arrCoords[i][0];
107.             double y = arrCoords[i][1];
108.             arrCoords[i][0] = calculateX(x, L, gradientX(a, x));
109.             arrCoords[i][1] = calculateY(y, L, gradientY(b, y));
110.
111.         }
112.         count += k;
113.         /*printf("%d ", k);
114.         printf("%d", maxIter);
115.         printf("\n");*/
116.     }
117.
118.     printf("average max iteration  = %d\naverage real iteration = %.2lf\n", maxIter / SIZE, ceil(count / SIZE));
119. }
120.
121. void thirdTest() {
122.     puts("test 3:");
123.     double a = 16;
124.     double b = 3;
125.     double c = 13;
126.     double accuracy = 0.01;
127.     double arrCoords[SIZE][2] = { 0 };
128.     double count = 0;
129.     double L = max(a, b);
130.     int maxIter = 0;
131.     for (int i = 0; i < SIZE; i++) {
132.         arrCoords[i][0] = randOnRange(-sqrt(c / a), -sqrt(c / a) + 0.1);
133.         arrCoords[i][1] = sqrt((c - a * pow(arrCoords[i][0], 2)) / b);
134.         //printf("[%.3lf;%.3lf]\t", arrCoords[i][0], arrCoords[i][1]);
135.     }
136.     for (int i = 0; i < SIZE; i++) {
137.         maxIter += maxN(func(a, b, arrCoords[i][0], arrCoords[i][1]), accuracy, L);
138.         int k = 0;
140.             double x = arrCoords[i][0];
141.             double y = arrCoords[i][1];
142.             arrCoords[i][0] = calculateX(x, L, gradientX(a, x));
143.             arrCoords[i][1] = calculateY(y, L, gradientY(b, y));
144.
145.         }
146.         count += k;
147.         /*printf("%d ", k);
148.         printf("%d", maxIter);
149.         printf("\n");*/
150.     }
151.     printf("average max iteration  = %d\naverage real iteration = %.2lf\n", maxIter / SIZE, ceil(count / SIZE));
152. }
153.
154. int main() {
155.     srand(time(0));
156.     firstTest();
157.     secondTest();
158.     thirdTest();
159.     return 0;
160. }
RAW Paste Data Copied