Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. double a,b,c,eps,c1, e, x, m, M,x1,y;
  7. int i,n,k;
  8. bool u;
  9. double f (double x) {
  10. return pow(x,4)-2*pow(x,2)-3;
  11. }
  12.  
  13. double f1(double x) {
  14. return 4*pow(x,3)-4*x;
  15. }
  16.  
  17. void bis_apriori(double a,double b){
  18. n=trunc(log((b-a)/eps)/log(2))+1; u=true;
  19. for(int i=1;i<=n;i++){
  20. c=(a+b)/2;
  21. if(f(c)==0) {
  22. cout << " c : " << fixed << setprecision(15) << c << " f(c) : " << f(c) << fixed << setprecision(15) << " n : " << n << endl;
  23. u=false; break;}
  24. else{ if(f(a)*f(c)>0){
  25. a=c;}
  26. else{
  27. b=c;
  28. }
  29. }
  30. }
  31. if(u) cout << " c -> " << fixed << setprecision(15) << c << " f(c) -> " << f(c) << fixed << setprecision(15) << " n -> " << n << endl;
  32.  
  33. }
  34. void bis_aposteriori(double a,double b){
  35. c=a; u=true;
  36. do{ c1=c; k++;
  37. c=(a+b)/2;
  38. if(f(c)==0) {
  39. cout << " c -> " << fixed << setprecision(15) << c << " f(c) -> " << f(c) << fixed << setprecision(15) << " n -> " << k << endl;
  40. u=false; break;}
  41. else{ if(f(a)*f(c)>0){
  42. a=c;}
  43. else{
  44. b=c;
  45. }
  46. }
  47.  
  48.  
  49. }while(2*abs(c-c1)>eps);
  50. if(u) cout << " c -> " << fixed << setprecision(15) << c << " f(c) -> " << f(c) << fixed << setprecision(15) << " n -> " << k << endl;
  51.  
  52. }
  53.  
  54.  
  55.  
  56. void secanta(double a, double b, double eps, double m1, double m2) {
  57.  
  58.  
  59. c=a-(f(a)/(f(b)-f(a)))*(b-a);
  60. if (f(c)*f(a)>0){ e=b;x=a;
  61. }
  62. else{
  63. e=a;x=b;
  64. }
  65.  
  66. int k2 = 0;
  67. do {
  68. x1=x;
  69. k2++;
  70. x = x1 - (f(x1) * (e - x1)) / (f(e) - f(x1));
  71. } while ((((m1-m2) * abs(x - x1)) / m2) > eps);
  72.  
  73. cout << " c -> " << fixed << setprecision(15) << x << " f(c) -> " << f(x) << fixed << setprecision(15) << " n -> " << k2 << endl;
  74.  
  75. }
  76.  
  77. void coardaapos(double a, double b, double eps, double m1, double m2) {
  78. c = a;
  79. u = true;
  80. int k4 = 0;
  81. do {
  82. c1 = c;
  83. k4++;
  84. c = (a * f(b) - b * f(a)) / (f(b) - f(a));
  85. if (f(c) == 0) {
  86. cout << " c : " << fixed << setprecision(15) << c << " f(c) : " << f(c) << fixed << setprecision(15) << " n : " << k4 << endl;
  87. u = false;
  88. break;
  89. } else {
  90. if (f(c)*f(a) > 0) {
  91. a = c;
  92. } else {
  93. b = c;
  94. }
  95. }
  96.  
  97. } while (((m1 * abs(c - c1)) / m2) > eps);
  98. if (u) {
  99. cout << " c : " << fixed << setprecision(15) << c << " f(c) : " << f(c) << fixed << setprecision(15) << " n : " << k4 << endl;
  100. }
  101. }
  102. void coardaapriori(double x1, double x2) {
  103.  
  104. int k1 = trunc(log(eps/0.5) / (log(f1(x2) - f1(x1)) - log(f1(x2)))) + 1;
  105. c = a;
  106. for (int i = 1; i <= k1; i++) {
  107.  
  108. c = c - (f(c) * (x2 - c)) / (f(x2) - f(c));
  109. }
  110. cout << " c : " << fixed << setprecision(15) << c << " f(c) : " << fixed << setprecision(15) << f(c) << " n : " << k1 << endl;
  111.  
  112. }
  113. void tang(double a, double b, double eps, double m1, double m2) {
  114. int k5=0;double x,x1,c=0;
  115. c = (a * f(b) - b * f(a))/(f(b)-f(a));
  116. if (f(a)*f(c)>0) { x = b;
  117. } else { x = a;
  118. }
  119. do {
  120. x1=x;
  121. x = x1 - f(x1)/f1(x1);
  122. k5++;
  123.  
  124.  
  125. }while(((m1*pow((x-x1),2))/(2*m2))>=eps);
  126. cout << " c -> " << fixed << setprecision(15) << x << " f(c) -> " << fixed << setprecision(15) << f(x) << " n -> " << k5 << endl;
  127.  
  128.  
  129. }
  130. void mixt(double a,double b,double eps){
  131.  
  132. int k6 = 0;double h=0; double x,y;
  133. h = (a * f(b) - b * f(a))/(f(b)-f(a));
  134. if (f(a)*f(h)>0) { x = b;
  135. y = a; } else { x = a;
  136. y = b; }
  137. do {
  138. y = y - (f(y) * (x-y))/(f(x)-f(y));
  139. x = x - f(x)/f1(x);
  140. k6++;
  141.  
  142.  
  143. }
  144. while (abs(x-y) >= eps);
  145. cout << " c -> " << fixed << setprecision(15) << x << " f(c) -> " << fixed << setprecision(15) << f(x) << " n -> " << k6 << endl;
  146.  
  147.  
  148. }
  149. int main(){
  150. a=1; b=2;
  151. m=abs(f1(a));
  152. M=abs(f1(b));
  153.  
  154. cout<<"a -> "<<a<<" "<<" b -> "<<b<<endl;
  155. cout<<"M -> "<<M<<" "<<" m -> "<<m<<endl;
  156. m=pow(10,-15);
  157. cout<<"bisectia apriori"<<endl;
  158. eps=pow(10,-15);
  159. bisapriori(a,b);
  160. cout<<"bisectia aposteriori"<<endl;
  161. bisaposteriori(a,b);
  162. cout<<"secanta aposteriori"<<endl;
  163. secanta(a,b,eps,M,m);
  164. cout<<"coarde aposteriori"<<endl;
  165. coardaapos(a,b,eps,M,m);
  166. cout<<"coarde apriori"<<endl;
  167. eps=pow(10,-15);
  168. cout<<"nu putem aplica metoda f'(a)=0,ceea ce nu satisface conditiile metodei"<<endl;
  169. cout<<"dar daca a=1.5,putem aplica metoda(f e convexa,f e strict crescatoare,f'(a)>0,f e derivabila)"<<endl;
  170.  
  171.  
  172. coardaapriori(1.5,b);
  173.  
  174. cout<<"tangenta aposteriori"<<endl;
  175. tang(1.5,b,eps,44,7.5);
  176. cout<<"mixta"<<endl;
  177. mixt(1.5,b,eps);
  178.  
  179. return 0;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement