Advertisement
dzieciol

mnn

Dec 9th, 2016
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. double f(double x)
  6. {
  7. double zwracana;
  8. zwracana = x*x*x - 2*x*x - 4*x - 7;
  9. return zwracana;
  10.  
  11. }
  12.  
  13. double fprim(double x)
  14. {
  15. double zwracana;
  16. zwracana = 3*x*x - 4*x - 4;
  17. return zwracana;
  18.  
  19. }
  20. double mod(double x)
  21. {
  22. if(x<=0)return -1*x;
  23. else return x;
  24.  
  25. }
  26.  
  27.  
  28. double Bisekcja(double a, double b, double eps)
  29. {
  30. double xi;
  31. if(mod(f(a))<=eps) return a;
  32.  
  33. if (mod(f(b))<=eps) return b;
  34. //cout<<f(a)<<endl;
  35. //cout<<f(b)<<endl;
  36.  
  37. if (f(a)*f(b)>0) cout<<"blad";
  38. else
  39. {
  40. while (1){
  41.  
  42. xi= (a+b)/2;
  43.  
  44.  
  45. if(mod(f(xi))<=eps)
  46. {
  47. return xi;
  48. }
  49. else
  50. {
  51. if(f(a)*f(xi)<=eps)
  52. {
  53. b=xi;
  54.  
  55. }
  56. else a=xi;
  57. if (b-a<=eps)return xi;
  58.  
  59. }
  60. }
  61. }
  62.  
  63. }
  64.  
  65.  
  66. double newton (double x, double eps)
  67. {
  68. //ograniczenie na liczbe iteracji;
  69.  
  70. for (int i=0 ; i<100 ; i++)
  71. {
  72. if(mod(f(x))<eps)
  73. {
  74. return x;
  75. }else
  76. x -= f(x)/fprim(x);
  77.  
  78. }
  79. cout<<"blad"<<endl;
  80.  
  81.  
  82.  
  83. }
  84. int main()
  85. {
  86. //cout << "Hello world!" << endl;
  87. cout<<Bisekcja(3,4,0.001)<<endl;
  88. cout<<newton(4,0.001);
  89.  
  90. return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement