Advertisement
Guest User

method 1 v2.0

a guest
Jun 17th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <math.h>
  3. #include <stdlib.h>
  4. #include <conio.h>
  5. #include <stdio.h>
  6.  
  7. using namespace std;
  8. double e=0.01;
  9. double a = 0;
  10. double b = 2;
  11.  
  12. double f(double x)
  13. {
  14. return (sqrt(pow(x,2)+3)/(x+5));
  15. }
  16.  
  17. double razrad()
  18. {
  19. double d,x,y;
  20. d=(b-a)/4;
  21. double x0=a;
  22. double f0=f(x0);
  23. label:
  24. double x1=x0+d;
  25. double f1=f(x1);
  26. if (f0>f1)
  27. {
  28. x0=x1;
  29. f0=f1;
  30. if(a<=x0 && x0<=b)
  31. goto label;
  32. }
  33. if (fabs(d)<=e)
  34. {
  35. x=x0;
  36. y=f(x0);
  37. }
  38. else
  39. {
  40. x0=x1;
  41. f0=f1;
  42. d=-d/4;
  43. goto label;
  44. }
  45. cout <<"rmin=" << x << endl;
  46. cout <<"rfmin=" << y << endl;
  47. return 0;
  48. }
  49.  
  50. double dihot()
  51. {
  52. double s=0.001;
  53. double x1,x2,en,x,y;
  54. do
  55. {
  56. x1=(b+a-s)/2;
  57. x2=(b+a+s)/2;
  58. if(f(x1)<=f(x2)) b=x2;
  59. else a=x1;
  60. en=(b-a)/2;
  61. }
  62. while(en>e);
  63. x=(a+b)/2;
  64. y=f(x);
  65. cout << endl;
  66. cout << "dmin = " << x << endl;
  67. cout << "dfmin = " << y << endl;
  68. return 0;
  69. }
  70.  
  71. double gold()
  72. {
  73. double x1,x2,f1,f2,t,l,en,x,y;
  74. t=(sqrt(5)-1)/2;
  75. l=b-a;
  76. x1 = b - t*l;
  77. x2 = a + t*l;
  78. f1 = f(x1);
  79. f2 = f(x2);
  80. while(l>2*e)
  81. {
  82. if(f1<=f2)
  83. {
  84. b = x2;
  85. l=b-a;
  86. x2=x1;
  87. f2=f1;
  88. x1=b-t*l;
  89. f1=f(x1);
  90. }
  91. else
  92. {
  93. a=x1;
  94. l=b-a;
  95. x1=x2;
  96. f1=f2;
  97. x2=a+t*l;
  98. f2=f(x2);
  99. }
  100. }
  101. x=(a+b)/2;
  102. y=f(x);
  103. cout << endl;
  104. cout << "gmin = " << x << endl;
  105. cout << "gfmin = " << y << endl;
  106. return 0;
  107. }
  108.  
  109. double parabol()
  110. {
  111. double x1, x2, x3, f1, f2, f3, a1, a2, x, fx, xp;
  112. x1=(3-sqrt(5))*(b-a)/2;
  113. x3=(sqrt(5)-1)*(b-a)/2;
  114. x2=(x1+x3)/2;
  115. x=0;
  116. f1=f(x1);
  117. f2=f(x2);
  118. f3=f(x3);
  119. do
  120. {
  121. xp=x;
  122. a1=(f2-f1)/(x2-x1);
  123. a2=(1/(x3-x2))*( (f3-f1)/(x3-x1) - (f2-f1)/(x2-x1) );
  124. x=0.5*(x1+x2-(a1/a2));
  125. fx=f(x);
  126. if(x1<x<x2<x3)
  127. if(fx>=f2)
  128. {
  129. x1=x;
  130. f1=f(x1);
  131. }
  132. else
  133. {
  134. x3=x2;
  135. x2=x;
  136. f2=f(x2);
  137. f3=f(x3);
  138. }
  139. else if(x1<x2<x<x3)
  140. if(fx>=f2)
  141. {
  142. x3=x;
  143. f3=f(x3);
  144. }
  145. else
  146. {
  147. x1=x2;
  148. x2=x;
  149. f1=f(x1);
  150. f2=f(x2);
  151. }
  152. }
  153. while(abs(xp-x)>e);
  154. cout << endl;
  155. cout << "pmin = " << x << endl;
  156. cout << "pfmin = " << fx << endl;
  157. return 0;
  158. }
  159.  
  160.  
  161. int main()
  162. {
  163. razrad();
  164. dihot();
  165. gold();
  166. parabol();
  167. system ("pause");
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement