Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. #include <math.h>
  2. #include <iostream>
  3. #include <cstdlib>
  4.  
  5. #define F0 -0.56284
  6. #define EPSILON 1.0
  7. #define CYCLES 50
  8.  
  9. float leftBorder = -1;
  10. float rightBorder = 1;
  11.  
  12. /*
  13. float function(float x){
  14. float sin = sinf(2*x+1)+2;
  15. return 1 / (1 + expf(sin));
  16. }
  17. */
  18.  
  19.  
  20.  
  21. float function(float x){
  22. return powf(x,2);
  23. }
  24.  
  25.  
  26. float fib(int n){
  27. if (n == 0 || n == 1)
  28. return 1;
  29.  
  30. return fib(n - 1) + fib(n - 2);
  31. }
  32.  
  33. float calcD(int n){
  34. return (fib(n-1)/fib(n))*(2) + (powf(-1,n)/fib(n))*EPSILON;
  35. //return (fib(CYCLES - n) / fib(CYCLES)) * (rightBorder - leftBorder);
  36. }
  37.  
  38. float calculate(float a, float b) {
  39. float left, right;
  40.  
  41. for (int i = 1; i < CYCLES; i++) {
  42. left = fib(CYCLES - i - 1)/fib(CYCLES - i + 1)*(b - a) + a;
  43. right = fib(CYCLES - i)/fib(CYCLES - i + 1)*(b - a) + a;
  44.  
  45. if (function(right) > function(left)) {
  46. b = right;
  47. } else
  48. {
  49. a = left;
  50. }
  51.  
  52. return function((a+b)/2);
  53. }
  54. }
  55.  
  56.  
  57. int main(){
  58. /*
  59. float x1,x2, result, tempD;
  60.  
  61. for(int i = 1; i < CYCLES + 1; i++){
  62. //calculate x1 and x2
  63. tempD = calcD(i);
  64. //if(tempD <= EPSILON){printf("BREAK\n"); break;}
  65. x1 = leftBorder - tempD; x2 = rightBorder + tempD;
  66.  
  67. if(function(x1) >= function(x2)){
  68. leftBorder = x1;
  69. }else{
  70. rightBorder = x2;
  71. }
  72. }*/
  73.  
  74. float result = calculate(-1, 1);
  75. printf("x = %d\nMinimum = %lf\n", 12121212, result);
  76.  
  77. return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement