freesky

task_sem3_2.1

Oct 10th, 2013
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.12 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include "optim.h"
  4.  
  5. #define LEFT 0.0
  6. #define RIGHT 5.0
  7. #define EVALUATE 1e-10
  8.  
  9. double f(double x) {
  10.     numcount1++;
  11.     return -(pow(sin(x), 6.0) * tan(1.0 - x) * exp(30.0 * x));
  12. }
  13.  
  14. double f1(double x, double *d) {
  15.     numcount1++;   
  16.     *d = exp(30.0 * x) * pow(sin(x), 5.0) * (sin(x) / pow(cos(1.0 - x), 2.0) - 6.0 * tan(1.0 - x) * (5.0 * sin(x) + cos(x)));
  17.     return -(pow(sin(x), 6.0) * tan(1.0 - x) * exp(30.0 * x));
  18. }
  19.  
  20. int main(int argc, char *argv[]) {
  21.     double ax, bx, cx;
  22.     double fa, fb, fc;
  23.     double x;
  24.    
  25.     ax = LEFT; bx = RIGHT;
  26.    
  27.     numcount1 = 0;
  28.     minbrack(f, &ax, &bx, &cx, &fa, &fb, &fc);
  29.     x = golden(f, ax, bx, cx, 1e-10);
  30.     printf("golden\tx = %e | max(f) = %e | funcs = %d | iters = %d\n", x, -f(x), numcount1, itercount1);
  31.    
  32.     numcount1 = 0;
  33.     x = fmin1(f, LEFT, RIGHT, 0.0);
  34.     printf("fmin\tx = %e | max(f) = %e | funcs = %d | iters = %d\n", x, -f(x), numcount1, itercount1);
  35.    
  36.     numcount1 = 0;
  37.     x = davidon(f1, LEFT, RIGHT, EVALUATE, 1e-10);
  38.     printf("davidon\tx = %e | max(f) = %e | funcs = %d | iters = %d\n", x, -f(x), numcount1, itercount1);
  39.    
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment