Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <cstdio>
  4. #define M_PI 3.14159265358979323846
  5.  
  6. using namespace std;
  7.  
  8. double f(double x){
  9.     return x*x-4;
  10. }
  11.  
  12. double g(double x){
  13.     return sin(2*x);
  14. }
  15.  
  16. //double f(double x){
  17. //    return -x*x/50.0;
  18. //}
  19.  
  20. //double g(double x){
  21. //    return 1+x*x/100.0-x/200.0;
  22. //}
  23.  
  24. double pole(double a, double b, double h){
  25.     double S = 0;
  26.     double x0 = a;
  27.     double x1 = x0+h;
  28.  
  29.     while(x0<=b){
  30.         S+=(g(x0)-f(x0)+g(x1)-f(x1))*h/20.0;
  31.         x0 = x1;
  32.         x1+=h;
  33.     }
  34.     return S;
  35. }
  36.  
  37. int retC(int &a, int &b){
  38.     int i,j;
  39.     for(i=0; g(i)-f(i)<26; i++);
  40.     i+=100;
  41.  
  42.     for(j=0; g(j)-f(j)<100; j++);
  43.     j+=26;
  44.  
  45.     if(i<j){
  46.         a= 26;
  47.         b= 100;
  48.         return i;
  49.     }
  50.     else{
  51.         a=100;
  52.         b=26;
  53.         return j;
  54.     }
  55. }
  56.  
  57. void wypisz(){
  58.     int a,b;
  59.     int c = retC(a,b);
  60.  
  61.     cout << "A = (" << c << ", " << g(c-b) << ") "<<endl;
  62.     cout << "B = (" << c << ", " << g(c-b)-a << ") "<<endl;
  63.     cout << "C = (" << c-b << ", " << g(c-b)-a << ") "<<endl;
  64.     cout << "D = (" << c-b << ", " << g(c-b) << ") "<<endl;
  65.  
  66.     cout << "wartosci funkcji: " <<endl;
  67.     cout <<f(58) << " " <<g(58) <<endl;
  68.  
  69.  
  70. }
  71.  
  72.  
  73. double bisekcjaF(double a, double b, double d){
  74.     double sr;
  75.     while(fabs(a-b)>d){
  76.         sr= (a+b)/2;
  77.         if(f(sr)*f(b)>0){
  78.             b=sr;
  79.         }
  80.         else{
  81.             a=sr;
  82.         }
  83.     }
  84.     return sr;
  85. }
  86.  
  87.  
  88. double bisekcjaG(double a, double b, double d){
  89.     double sr;
  90.     while(fabs(a-b)>d){
  91.         sr= (a+b)/2;
  92.         if(g(sr)*g(b)>0){
  93.             b=sr;
  94.         }
  95.         else{
  96.             a=sr;
  97.         }
  98.     }
  99.     return sr;
  100. }
  101.  
  102.  
  103.  
  104.  
  105. int main()
  106. {
  107.  
  108.     //zad.1.
  109. //    cout << pole(0,10,0.01);
  110. //    wypisz();
  111.  
  112.     //zad.2. bisekcja
  113.    // cout << bisekcjaF(1,5,0.01);
  114.  
  115.     cout << bisekcjaG(5,10,0.0000001) <<endl;
  116.     cout << 2* M_PI <<endl;
  117.  
  118.     return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement