Advertisement
vindos

Untitled

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