Advertisement
myname0

Newton

Nov 1st, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.67 KB | None | 0 0
  1.   static List<List<double>> CountSepaetedDifference(double[] fn)
  2.         {
  3.             List<List<double>> separatedDifference = new List<List<double>>();
  4.  
  5.             for (int i = 0; i < fn.Length; i++)
  6.             {
  7.                 List <double> tmp = new List<double>();
  8.                 tmp.Add(fn[i]);
  9.                 separatedDifference.Add(tmp);
  10.             }
  11.  
  12.             int n = fn.Length - 1;
  13.            
  14.             for (int i = 0; i < fn.Length - 1; i++)
  15.             {
  16.                 for (int j = 0; j < n; j++)
  17.                 {
  18.                     separatedDifference[j].Add((separatedDifference[j+1][i] - separatedDifference[j][i]) / (i + 1) );
  19.                 }
  20.                 n--;
  21.             }
  22.             return separatedDifference;
  23.         }
  24.         static void Newton()
  25.         {
  26.             double[] xn = { 1, 2, 3, 4, 5 };
  27.             double[] fn = { 1, 16, 81, 256, 625 };
  28.             double x = 0;
  29.             List<List<double>> separetedDifference = CountSepaetedDifference(fn);
  30.             double Nn = 0;
  31.             double multiplier = 1;
  32.  
  33.             for (int i = 0; i < xn.Length - 1; i++)
  34.             {
  35.                 x = (xn[i] + xn[i + 1]) / 2;
  36.                 Nn = separetedDifference[0][0];
  37.                 multiplier = 1;
  38.  
  39.                 for (int j = 0; j < xn.Length - 1; j++)
  40.                 {
  41.                     multiplier *= (x - xn[j]);
  42.                     Nn += separetedDifference[0][j + 1] * multiplier;
  43.                 }
  44.  
  45.                 Console.WriteLine("{0}  -  {1}  -  {2}", x, Nn, x * x * x * x);
  46.             }
  47.         }
  48.  
  49.         static void Main(string[] args)
  50.         {
  51.             Newton();
  52.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement