Advertisement
noor017

Aitkens Process

Nov 5th, 2015
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. ///***************Solve a fixed quadratic equation using Aitken’s Delta-Squared Process***************///
  2. ///** a = 4; b = 6; c = -1 **///
  3. ///** Equation: 4x^2 + 6x - 1 = 0 **///
  4. ///** x1 = 0.151388 ; x2 = -1.65139 **///
  5.  
  6. #include<iostream>
  7. #include<cmath>
  8.  
  9. #define ESP 0.000001
  10.  
  11.  
  12. using namespace std;
  13.  
  14. int main()
  15. {
  16.     double x1, x2, x3, x4, y;
  17.  
  18.     //First Value
  19.  
  20.     cin >> x1 >> x2 >> x3;
  21.  
  22.     #define F(x) (x2-x1*x*x)/x3
  23.     //#define F(x) (1-4*x*x)/6
  24.  
  25.     x1 = 1;                 //Initial Value
  26.     for (int i=1; ; i=i+3)
  27.     {
  28.         x2 = F(x1);
  29.         x3 = F(x2);
  30.  
  31.         if(fabs(x3-x2) < ESP)
  32.         {
  33.             cout << "First Value: " << x3 << endl;
  34.             break;
  35.         }
  36.  
  37.         else
  38.         {
  39.             y = x3-2*x2+x1;
  40.             x1 = x3-(((x3-x2)*(x3-x2))/y);
  41.         }
  42.     }
  43.  
  44.  
  45.     //Second Value
  46.  
  47.     //cin >> x1;
  48.  
  49.     x1 = 2;                 //Initial Value
  50.     for (int i=1; ; i=i+3)
  51.     {
  52.         x2 = F(x1);
  53.         x3 = F(x2);
  54.  
  55.         if(fabs(x3-x2) < ESP)
  56.         {
  57.             cout << "Second Value: " << x3 << endl;
  58.             break;
  59.         }
  60.  
  61.         else
  62.         {
  63.             y = x3-2*x2+x1;
  64.             x1 = x3-(((x3-x2)*(x3-x2))/y);
  65.         }
  66.     }
  67.  
  68.     return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement