Advertisement
vindos

Untitled

Feb 17th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.46 KB | None | 0 0
  1. #include "mlisp.h"
  2. #include <iostream>
  3. #include <cmath>
  4. double a=0.;
  5. double b=2.;
  6. double xmin=0.;
  7. double tolerance=0.00001;
  8. double mphi=(3.-sqrt(5))/2.;
  9. double xa;
  10. double xb;
  11. double ya;
  12. double yb;
  13.  
  14. double fun(double x){
  15.     x=x-105./106.;
  16.     return (x*atan(x)-1./3.);
  17. }
  18.  
  19. double golden__section__search(double a, double b);
  20. double golden__start(double a,double b);
  21. double _VVV_try(double a,double b,double xa,double ya,double xb,double yb);
  22. double close__enough_Q(double x,double y);
  23.  
  24.  
  25.  
  26. double golden__section__search(double a, double b){
  27.     {
  28.  
  29.         (a<b)? xmin=golden__start(a,b) : xmin=golden__start(b,a);
  30.     }
  31.     newline();
  32.     return(xmin);
  33. }
  34.  
  35.  
  36. double golden__start(double a,double b){
  37.     {
  38.         xa=a+(mphi*(b-a));
  39.         xb=b-(mphi*(b-a));
  40.     }
  41.     return _VVV_try(a,b,xa,fun(xa),xb,fun(xb));
  42. }
  43.  
  44.  
  45. double _VVV_try(double a,double b,double xa,double ya,double xb,double yb){
  46.     close__enough_Q(a,b) ? ((a+b) * 0.5) : (
  47.         display("+"),
  48.         (ya < yb) ? (b =xb, xb = xa, yb = ya, xa = a + (mphi * (b-a)), _VVV_try(a,b,xa,fun(xa),xb,yb)) : ( a= xa, xa =xb, ya = yb, xb = (b - mphi*(b-a)), _VVV_try(a,b, xa, ya, xb, fun(xb))
  49.     ));
  50. }
  51.  
  52.  
  53. double close__enough_Q(double x,double y){
  54.     return (abs(x-y)<tolerance);
  55. }
  56.  
  57. int main(){
  58.     xmin=golden__section__search(a,b);
  59.     display("interval=\t[");
  60.     display(a);
  61.     display(" , ");
  62.     display(b);
  63.     display("]\n");
  64.     display("xmin=\t\t");
  65.     display(xmin);
  66.     newline();
  67.     display("f(xmin)=\t");
  68.     display(fun(xmin));
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement