Advertisement
Guest User

Untitled

a guest
Jan 15th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath> //sinus
  3. using namespace std;
  4.  
  5. double f(double x) {
  6. return (x / (1.2 - sin(2 * x)));
  7. }
  8.  
  9. double concave(double x1, double a, double b) { //1.21
  10. return ((-2)*(f(a)*(b-x1)+f(x1)*(a-b)+f(b)*(x1-a))/((b-x1)*(a-b)*(x1-a)));
  11. }
  12.  
  13. double xTwo(double x1, double a, double b) { //1.22
  14. return ((f(a)*((b*b)-(x1*x1))
  15. +f(x1)*((a*a)-(b*b))
  16. +f(b)*((x1*x1)-(a*a)))/
  17. 2*(f(a)*(b-x1)
  18. +f(x1)*(a-b)
  19. +f(b)*(x1-a))
  20. );
  21. }
  22.  
  23.  
  24.  
  25. int main() {
  26. int expansion_factor = 4, i=1;
  27. double accuracy = 0.000001;
  28. double x1[100], x2[100], a[100], b[100], shift1, shift2;
  29. x1[0] = 0.22; //initial point > 0
  30. cout << f(0) << " < " << f(x1[0]) << "\n\n"; //so
  31. a[1] = (-1)*expansion_factor*x1[0]; x1[1] = 0; b[1]=x1[0];
  32. cout << "i\ta\tx1\tx2\tb\n";
  33.  
  34. do {
  35. while (concave(x1[i], a[i], b[i]) <= 0) { //1.21, 1.24
  36. cout << i << "\t" << a[i] << "\t" << x1[i] << "\t" << x2[i] << "\t" << b[i];
  37. cout << "\tconcave\n";
  38. a[i] = x1[i];
  39. x1[i] = b[i];
  40. b[i] = b[i] + expansion_factor*(b[i]-x1[i]);
  41. }
  42. x2[i] = xTwo(x1[i], a[i], b[i]);
  43. if (x2[i] < x1[i]) {
  44. cout << i << "\t" << a[i] << "\t" << x1[i] << "\t" << x2[i] << "\t" << b[i];
  45. cout << "\tx2<x1\n";
  46. shift1 = x2[i];
  47. x2[i] = x1[i];
  48. x1[i] = shift1;
  49. }
  50. if (x2[i] < a[i] || x2[i] > b[i]) { //1.25
  51. cout << i << "\t" << a[i] << "\t" << x1[i] << "\t" << x2[i] << "\t" << b[i];
  52. cout << "\tout of interval\n";
  53. shift1 = a[i];
  54. shift2 = x1[i];
  55. a[i] = x2[i];
  56. x1[i] = shift1;
  57. b[i] = shift2;
  58. };
  59. cout << i << "\t" << a[i] << "\t" << x1[i] << "\t" << x2[i] << "\t" << b[i];
  60. if (f(x1[i])>=f(x2[i])) { //1.11 - 1.13
  61. cout << "\ta deleted\n";
  62. a[i+1] = x1[i];
  63. b[i+1] = b[i];
  64. x1[i+1] = x2[i];
  65. }
  66. else { //1.15 - 1.18
  67. cout << "\tb deleted\n";
  68. a[i+1] = a[i];
  69. b[i+1] = x2[i];
  70. x1[i+1] = x1[i];
  71. }
  72.  
  73. i++;
  74. } while (i<90);//x2[i]-x1[i] > accuracy);
  75. return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement