Advertisement
Guest User

ello

a guest
Jan 16th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <stdio.h>
  4. using namespace std;
  5. double bisection(double,double,double);
  6. int sign(double);
  7. double fun(double);
  8. double derivative(double);
  9. double newton_raphson(double,double,int);
  10. int setMax_it();
  11. double setEps();
  12. double setStart();
  13. double setLeft();
  14. double setRight();
  15.  
  16.  
  17. int main()
  18. {
  19.     cout<<"Metoda Bisekcji:"<<endl<<endl;
  20.     double left=setLeft();
  21.     double right=setRight();
  22.     double eps=setEps();
  23.  
  24.     cout<<"Miejsce zerowe funkcji x^3+x-1 to: "<<bisection(left,right,eps)<<endl;
  25.     cout<<"Metoda Newtona-Raphsona:"<<endl<<endl;
  26.     int max_it=setMax_it();
  27.     double start=setStart();
  28.     eps=setEps();
  29.     cout<<endl;
  30.     cout<<"Miejsce zerowe funkcji x^3+x-1 to: "<<newton_raphson(start,eps,max_it)<<endl;
  31.  
  32.     return 0;
  33. }
  34. double setLeft()
  35. {
  36.     double x;
  37.     cout<<"Podaj lewa granice przedzialu: ";
  38.     cin>>x;
  39.     return x;
  40. }
  41. double setRight()
  42. {
  43.     double x;
  44.     cout<<"Podaj prawa granice przedzialu: ";
  45.     cin>>x;
  46.     return x;
  47. }
  48. double setStart()
  49. {
  50.     double x;
  51.     cout<<"Podaj argument startowy: ";
  52.     cin>>x;
  53.     return x;
  54. }
  55. double setEps()
  56. {
  57.     double x;
  58.     cout<<"Podaj epsilon: ";
  59.     cin>>x;
  60.     return x;
  61. }
  62. int setMax_it()
  63. {
  64.     int max_it;
  65.     cout<<"Podaj maksymalna liczbe iteracji: ";
  66.     cin>>max_it;
  67.     return max_it;
  68.  
  69. }
  70. double derivative(double x)
  71. {
  72.     return 3*x*x+1;
  73. }
  74. double fun(double x)
  75. {
  76.     return (pow(x,3)+x-1);
  77. }
  78. int sign(double x)
  79. {
  80.     if (x > 0)
  81.         return 1;
  82.     if (x < 0)
  83.         return -1;
  84.     return 0;
  85. }
  86. double bisection(double a,double b,double eps)
  87. {
  88.     if (sign(fun(a))==sign(fun(b)))
  89.     {
  90.         cout << "Wartosci funkcji w punktach a i b maja taki sam znak"<<endl;
  91.         return NAN;
  92.     }
  93.     for(;;)
  94.     {
  95.         double x=(a+b)/2;
  96.         if(abs(fun(x))<=eps)
  97.             return x;
  98.         if(b-a<=eps)
  99.             return x;
  100.         if(sign(fun(x))!=sign(fun(a)))
  101.             b=x;
  102.         else
  103.             a=x;
  104.     }
  105.  
  106. }
  107. double newton_raphson(double start,double eps,int max_it)
  108. {
  109.     for(int i=0; i<=max_it; i++)
  110.     {
  111.         if(abs(fun(start))<=eps)
  112.             return start;
  113.         double xn=start-(fun(start)/derivative(start));
  114.         if(abs(xn-start)<=eps)
  115.             return xn;
  116.         start=xn;
  117.     }
  118.     cout<<"Podano niepoprawny punkt startowy"<<endl;
  119.     return NAN;
  120.  
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement