Advertisement
Guest User

LABA 2

a guest
Feb 17th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. //
  2. // main.cpp
  3. // lab2
  4. //
  5. // Created by kosha on 17/02/2020.
  6. // Copyright © 2020 kosha. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include "mlisp.h"
  11. double xmin= 0.;
  12. double xa, xb, ya, yb;
  13. double tolerance= 0.00001;
  14. double mphi = (3. - sqrt(5.)) /2.;
  15. double a = 3.;
  16. double b = 4.;
  17. double fun( double x ){
  18. x = x - 100./101.;
  19. return (x + sin(2*x - 0.25*pi) + log(x+1) - 0.5);
  20. }
  21. double close__enough_Q(double x,double y);
  22. double close__enough_Q(double x,double y){
  23. return ( abs(x - y) < tolerance);
  24. }
  25.  
  26. double _CNA_try(double a, double b, double xa, double ya, double xb, double yb);
  27. double _CNA_try(double a, double b, double xa, double ya, double xb, double yb){
  28. close__enough_Q(a,b) ? ((a+b) * 0.5) : (
  29. display("+"),
  30. (ya < yb) ? (b =xb, xb = xa, yb = ya, xa = a + (mphi * (b-a)), _CNA_try(a,b,xa,fun(xa),xb,yb)) : ( a= xa, xa =xb, yb = ya, xb = (b = mphi*(b-a)), _CNA_try(a,b, xa, ya, xb, fun(xb))
  31. ));
  32.  
  33. }
  34. double golden_start(double a, double b);
  35. double golden_start(double a, double b){
  36. xa = a + mphi * (b-a);
  37. xb = b - mphi * (b-a);
  38. return _CNA_try (a, b, xa, fun(xa), xb, fun(xb));
  39.  
  40. }
  41.  
  42. double golden_section_search(double a, double b);
  43.  
  44. double golden_section_search(double a, double b){
  45. {
  46. (a < b) ? xmin = golden_start(a,b) : xmin = golden_start (b,a);
  47. }
  48. newline();
  49. return xmin;
  50. }
  51.  
  52.  
  53.  
  54. int main(){
  55. xmin = golden_section_search(a,b);
  56. display("interval=\t");
  57. display(a);
  58. display(" , ");
  59. display(b);
  60. display("]\n");
  61. display("xmin=\t\t");
  62. display(xmin);
  63. display("f(xmin)=\t");
  64. fun(xmin);
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement