Advertisement
HabKaffee

Untitled

May 10th, 2020
186
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. int n = 0;
  15.  
  16. double func(double x) {
  17.     n++;
  18.     return pow((x - 2), 2);
  19.  
  20. }
  21.  
  22. double calcDot(double (*func)(double), double L, double x1, double x2) {
  23.     return 1 / (2 * L) * (L * (x1 + x2) + func(x1) - func(x2));
  24. }
  25.  
  26. double lowGrade(double (*func)(double), double L, double x1, double x2) {
  27.     return (func(x1) + func(x2)) / 2 - L / 2 * (x2 - x1);
  28. }
  29.  
  30. double calcL(double a, double b) {
  31.     if ((2 * (a - 2)) > (2 * (b - 2))) {
  32.         return (2 * (a - 2));
  33.     }
  34.     else {
  35.         return (2 * (b - 2));
  36.     }
  37. }
  38.  
  39.  
  40.  
  41. int main() {
  42.     POINT arr[1000] = { 0 };
  43.  
  44.     double a = 0, b = 5, accuracy = 0.1, L = calcL(a, b);
  45.     double y = calcDot(&func, L, a, b);
  46.     arr[0] = { a, func(a), 0 };
  47.     arr[1] = { y,func(y),1 };
  48.     arr[2] = { b,func(b),0 };
  49.     int f = 2;
  50.     double upperLimit = fmin(arr[0].y, arr[2].y);
  51.     double lowerLimit = lowGrade(&func, L, a, b);
  52.  
  53.     int t[5] = { 0 };
  54.  
  55.     while (upperLimit - lowerLimit > accuracy) {
  56.         double min = upperLimit;
  57.         int r = 0;
  58.         for (int i = 0; i < f + 1; i++) {
  59.             if ((arr[i].flag != 0) && (lowerLimit <= arr[i].y) && (arr[i].y < min)) {
  60.                 min = arr[i].y;
  61.                 r = i;
  62.             }
  63.         }
  64.         upperLimit = fmin(upperLimit, func(arr[r].x));
  65.         lowerLimit = min;
  66.  
  67.         arr[r].y = func(arr[r].x);
  68.         arr[r].flag = 0;
  69.         arr[f + 2] = { arr[f].x,arr[f].y,arr[f].flag };
  70.         arr[r + 1] = { arr[r].x,arr[r].y,arr[r].flag };
  71.         arr[r] = { calcDot(&func,L,arr[r-1].x,arr[r].x), lowGrade(&func, L, arr[r-1].x,arr[r].x), 1 };
  72.         arr[r + 2] = { calcDot(&func,L,arr[r].x,arr[r + 1].x), lowGrade(&func, L, arr[r].x,arr[r + 1].x), 1 };
  73.         f += 2;
  74.         for (int j = 0; j <= f; j++) {
  75.             printf("[x = %lf; y = %lf]\n", arr[j].x, arr[j].y);
  76.         }
  77.     }
  78.     printf("\n\n[%.4lf;%.4lf]\n", arr[3].x, arr[2].x, n);
  79.     printf("Ymin = [%.4lf;%.4lf]\nIteretion:%d", lowerLimit, upperLimit, n);
  80.     return 0;
  81. }
Advertisement
RAW Paste Data Copied
Advertisement