Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <time.h>
- #include <string.h>
- // f(x, y) = 16x ^ 2 + 3y ^ 2, c = 13
- const int SIZE = 100;
- double func(double a, double b, double x, double y) {
- return a * pow(x, 2) + b * pow(y, 2);
- }
- double randOnRange(double min, double max) {
- double f = (double)rand() / RAND_MAX;
- return min + f * (max - min);
- };
- double calculateX(double x, double L, const double& gradientX) {
- return x - gradientX/L;
- };
- double calculateY(double y, double L, const double& gradientY) {
- return y - gradientY/L;
- };
- double gradientX(double a, double x) {
- return 2 * a * x;
- }
- double gradientY(double b, double y) {
- return 2 * b * y;
- }
- double max(double a, double b) {
- if (2 * a > 2 * b) {
- return 2 * a;
- }
- else return 2 * b;
- }
- int maxN(const double& func, double accuracy, double L) {
- double N = ((2 * L) / (pow(accuracy, 2))) * func;
- floor(N);
- return N;
- }
- double distGradient(const double &gradientX, const double &gradientY) {
- return sqrt(pow(gradientX, 2) + pow(gradientY, 2));
- }
- void firstTest() {
- puts("test 1:");
- double a = 16;
- double b = 3;
- double c = 13;
- double accuracy = 0.01;
- double arrCoords[SIZE][2] = { 0 };
- long double count = 0;
- double L = max(a, b);
- int maxIter = 0;
- for (int i = 0; i < SIZE; i++) {
- arrCoords[i][0] = randOnRange(-sqrt(c / a), sqrt(c / a));
- arrCoords[i][1] = sqrt((c - a * pow(arrCoords[i][0], 2)) / b);
- //printf("[%.3lf;%.3lf]\t", arrCoords[i][0], arrCoords[i][1]);
- }
- for (int i = 0; i<SIZE;i++){
- maxIter += maxN(func(a, b, arrCoords[i][0], arrCoords[i][1]), accuracy, L);
- int k = 0;
- for (; distGradient(gradientX(a,arrCoords[i][0]),gradientY(b, arrCoords[i][1]))>=accuracy; k++) {
- double x = arrCoords[i][0];
- double y = arrCoords[i][1];
- arrCoords[i][0] = calculateX(x, L, gradientX(a, x));
- arrCoords[i][1] = calculateY(y, L, gradientY(b, y));
- }
- count += k;
- //printf("%d ", k);
- //printf("%d", maxIter);
- //printf("\n");
- }
- printf("average max iteration = %d\naverage real iteration = %.2lf\n", maxIter / SIZE, ceil(count / SIZE));
- }
- void secondTest() {
- puts("test 2:");
- double a = 16;
- double b = 3;
- double c = 13;
- double accuracy = 0.01;
- double arrCoords[SIZE][2] = { 0 };
- long double count = 0;
- double L = max(a, b);
- int maxIter = 0;
- for (int i = 0; i < SIZE; i++) {
- arrCoords[i][0] = randOnRange(-0.5, 0.5);
- arrCoords[i][1] = sqrt((c - a * pow(arrCoords[i][0], 2)) / b);
- //printf("[%.3lf;%.3lf]\t", arrCoords[i][0], arrCoords[i][1]);
- }
- for (int i = 0; i < SIZE; i++) {
- maxIter += maxN(func(a, b, arrCoords[i][0], arrCoords[i][1]), accuracy, L);
- int k = 0;
- for (; distGradient(gradientX(a, arrCoords[i][0]), gradientY(b, arrCoords[i][1])) >= accuracy; k++) {
- double x = arrCoords[i][0];
- double y = arrCoords[i][1];
- arrCoords[i][0] = calculateX(x, L, gradientX(a, x));
- arrCoords[i][1] = calculateY(y, L, gradientY(b, y));
- }
- count += k;
- /*printf("%d ", k);
- printf("%d", maxIter);
- printf("\n");*/
- }
- printf("average max iteration = %d\naverage real iteration = %.2lf\n", maxIter / SIZE, ceil(count / SIZE));
- }
- void thirdTest() {
- puts("test 3:");
- double a = 16;
- double b = 3;
- double c = 13;
- double accuracy = 0.01;
- double arrCoords[SIZE][2] = { 0 };
- double count = 0;
- double L = max(a, b);
- int maxIter = 0;
- for (int i = 0; i < SIZE; i++) {
- arrCoords[i][0] = randOnRange(-sqrt(c / a), -sqrt(c / a) + 0.1);
- arrCoords[i][1] = sqrt((c - a * pow(arrCoords[i][0], 2)) / b);
- //printf("[%.3lf;%.3lf]\t", arrCoords[i][0], arrCoords[i][1]);
- }
- for (int i = 0; i < SIZE; i++) {
- maxIter += maxN(func(a, b, arrCoords[i][0], arrCoords[i][1]), accuracy, L);
- int k = 0;
- for (; distGradient(gradientX(a, arrCoords[i][0]), gradientY(b, arrCoords[i][1])) >= accuracy; k++) {
- double x = arrCoords[i][0];
- double y = arrCoords[i][1];
- arrCoords[i][0] = calculateX(x, L, gradientX(a, x));
- arrCoords[i][1] = calculateY(y, L, gradientY(b, y));
- }
- count += k;
- /*printf("%d ", k);
- printf("%d", maxIter);
- printf("\n");*/
- }
- printf("average max iteration = %d\naverage real iteration = %.2lf\n", maxIter / SIZE, ceil(count / SIZE));
- }
- int main() {
- srand(time(0));
- firstTest();
- secondTest();
- thirdTest();
- return 0;
- }
Add Comment
Please, Sign In to add comment