Advertisement
HabKaffee

Untitled

Apr 30th, 2020
150
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. //x-y<=-2, x+y >= 2
  9.  
  10. const int SIZE = 100;
  11.  
  12.  
  13. double func(double a, double b, double x, double y, double gamma) {
  14. return a * pow(x, 2) + b * pow(y, 2)+gamma*(pow(fmax(0.0, 2 + x - y),2)+pow(fmax(0, 2 - x - y),2));
  15. }
  16.  
  17. double randOnRange(double min, double max) {
  18. double f = (double)rand() / RAND_MAX;
  19. return min + f * (max - min);
  20. };
  21.  
  22.  
  23. double gradientX(double a, double b, double gamma, double x, double y) {
  24. if ((x - y <= -2) && (x + y >= 2)) {
  25. return 2 * a * x;
  26. }
  27. else if ((x - y <= -2) && (x + y < 2)) {
  28. return 2 * a * x;
  29. }
  30. else if ((x - y > -2) && (x + y >= 2)) {
  31. return 2 * a * x - 2 * gamma * (2 + x - y);
  32. }
  33. else {
  34. return 2 * a * x - 2 * gamma * (2 + x - y);
  35. }
  36. }
  37.  
  38. double gradientY(double a, double b, double gamma, double x, double y) {
  39. if ((x - y <= -2) && (x + y >= 2)) {
  40. return 2 * b * y;
  41. }
  42. else if ((x - y <= -2) && (x + y < 2)) {
  43. return 2 * b * y - 2 * gamma * (2 - x - y);
  44. }
  45. else if ((x - y > -2) && (x + y >= 2)) {
  46. return 2 * b * y;
  47. }
  48. else {
  49. return 2 * b * y - 2 * gamma * (2 - x - y);
  50. }
  51. }
  52.  
  53. double maxL(double a, double b, double gamma) {
  54. if (2 * (a+gamma) > 2 * (b+gamma)) {
  55. return 2 * (a + gamma);
  56. }
  57. return 2 * (b + gamma);
  58. }
  59.  
  60.  
  61. double distGradient(const double &gradientX, const double &gradientY) {
  62. return sqrt(pow(gradientX, 2) + pow(gradientY, 2));
  63. }
  64.  
  65. void task() {
  66. puts("task:");
  67. double a = 16;
  68. double b = 3;
  69. double c = 13;
  70. double accuracy = 0.01;
  71. double gamma = 1;
  72. int count = 0;
  73. double x = 0, y = 0,xn=0,yn=0, L=0;
  74.  
  75. x = randOnRange(-1, 1);
  76. y = randOnRange(-1, 1);
  77.  
  78. printf("%lf,%lf\n", x, y);
  79.  
  80. for (int i = 0; i <= 9; i++) {
  81. double L = maxL(a, b, gamma);
  82. xn = x;
  83. yn = y;
  84. count = 0;
  85. for ( ; distGradient(gradientX(a,b,gamma,xn,yn), gradientY(a, b, gamma, xn, yn)) >= accuracy; ) {
  86. xn = xn - gradientX(a, b, gamma, xn, yn) / L;
  87. yn = yn - gradientY(a, b, gamma, xn, yn) / L;
  88. //printf("%lf\t%lf\t", xn, yn);
  89. count++;
  90. printf("%d xx\n", count);
  91. }
  92. printf("Count = %d, gamma = %5.1lf, x = %.7lf, y = %.7lf, func = %.7lf\n", count, gamma, xn,yn, func(a,b,xn,yn,gamma));
  93. gamma *= 2;
  94. }
  95. }
  96.  
  97. int main() {
  98. srand(time(0));
  99. task();
  100. return 0;
  101. }
Advertisement
RAW Paste Data Copied
Advertisement