SHARE
TWEET

Untitled

a guest Feb 27th, 2020 143 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. FILE *logfile;
  5. FILE *logfile2;
  6. double func1(double x);
  7. double grid_search(double f(double), double start, double end, double tol);
  8. double dihotomie(double f(double), double start, double end, double tol);
  9. double raportAur(double f(double), double start, double end, double tol);
  10.  
  11. int main()
  12. {
  13.     logfile = fopen("log.csv","w");
  14.     logfile2 = fopen("log2.csv", "w");
  15.     if(logfile == NULL) return 1;
  16.     double op = grid_search( func1, 0, 2*acos(-1), 0.001);
  17.     double op2 = dihotomie( func1, 0, 2*acos(-1), 0.001);
  18.     double op3 = raportAur( func1, 0, 2*acos(-1), 0.001);
  19.     printf("Optimum: %f ,%f \n", op, func1(op));
  20.     printf("Optimum: %f ,%f \n", op2, func1(op2));
  21.     printf("Optimum: %f ,%f \n", op3, func1(op2));
  22.     return 0;
  23. }
  24.  
  25. double grid_search(double f(double), double start, double end, double tol)
  26. {
  27.  double min = 32000;
  28.  double i;
  29.  double poz;
  30.  for(i = start ; i < end ; i+=tol)
  31.  {
  32.     fprintf(logfile, "%f , %f , %f , %f  \n", i, f(i), poz ,min);
  33.     if(f(i) < min)
  34.     {
  35.         min = f(i);
  36.         poz = i;
  37.     }
  38.  }
  39.  return poz;
  40. }
  41.  
  42. double dihotomie(double f(double), double start, double end, double tol)
  43. {
  44.     //a = start , b = end
  45.     double eps = tol/3;
  46.  
  47.     while(end - start > tol)
  48.     {
  49.         double c = ((start + end)/2) - eps;
  50.         double d = ((start + end)/2) + eps;
  51.  
  52.         if(f(c) > f(d))
  53.             start = c;
  54.         else
  55.             end = d;
  56.  
  57.         fprintf(logfile2, "%f , %f , %f , %f  \n", start, f(start), end ,f(end));
  58.     }
  59.     return start;
  60. }
  61.  
  62. double raportAur(double f(double), double start, double end, double tol)
  63. {
  64.     double R = ((1+sqrt(5))/2);
  65.  
  66.     double a = start;
  67.     double b = end;
  68.  
  69.     double c = b - ((b-a)/R);
  70.     double d = a + ((b-a)/R);
  71.  
  72.     while(b-a > tol)
  73.     {
  74.     if(f(c) > f(d))
  75.     {
  76.         a = c;
  77.         c = d;
  78.         d = a + (b-a)/R;
  79.     }
  80.     else
  81.     {
  82.         b = d;
  83.         d = c;
  84.         c = b - (b-a)/R;
  85.     }
  86.     }
  87.     return b;
  88. }
  89.  
  90. double vertex(double f(double), double x1, double x2, double x3)
  91. {
  92.     double y1 = f(x1), y2 = f(x2), y3 = f(x3);
  93.     double denom = (x1-x2)*(x1-x3)*(x2-x3);
  94.     double A = (x3*(y2-y1)+x2*(y1-y3)+x1*(y3-y2))/denom;
  95.     double B = (x3*x3*(y1-y2)+x2*x2*(y1-y3)+x1*x1*(y2-y3))/denom;
  96.     return -B/(2*A);
  97. }
  98.  
  99. double interpolareParabolica(double f(double), double start, double end, double tol)
  100. {
  101.     double a = start;
  102.     double b = end;
  103.  
  104.     double c = (a+b)/2;
  105.  
  106.     while(b-a > tol)
  107.     {
  108.         double d = vertex(f, a , b, c);
  109.  
  110.         if(d < c)
  111.         {
  112.             double aux;
  113.             aux = c;
  114.             c = d;
  115.             d = aux;
  116.         }
  117.         if(f(c) > f(d))
  118.         {
  119.             a = c;
  120.             c = d;
  121.         }
  122.         else
  123.         {
  124.             b = d;
  125.         }
  126.     }
  127. }
  128.  
  129.  
  130. double func1(double x)
  131. {
  132.     return sin(x);
  133. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top