Slavik9510

Untitled

Dec 6th, 2022
864
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.72 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Lagrange_Interpolation
  4. {
  5.  
  6.     class Program
  7.     {
  8.         class Data
  9.         {
  10.             private static int length = 0;
  11.             public double x, y;
  12.             public Data(double x, double y)
  13.             {
  14.                 this.x = x;
  15.                 this.y = y;
  16.                 length++;
  17.             }
  18.             ~Data()
  19.             {
  20.                 length--;
  21.             }
  22.             public static int GetLength()
  23.             {
  24.                 return length;
  25.             }
  26.         };
  27.         static double dividedDiff(Data[] f, int n0, int n)
  28.         {
  29.             if (n - n0 < 1)
  30.             {
  31.                 Console.WriteLine("Wrong data");
  32.                 Environment.Exit(-1);
  33.                 return -1;
  34.             }
  35.             else if (n - n0 == 1)
  36.             {
  37.                 return (f[n].y - f[n0].y) / (f[n].x - f[n0].x);
  38.             }
  39.             else
  40.             {
  41.                 return (dividedDiff(f, n0 + 1, n) - dividedDiff(f, n0, n - 1)) / (f[n].x - f[n0].x);
  42.             }
  43.         }
  44.         static double interpolate(Data[] f, double x0)
  45.         {
  46.             Console.WriteLine("**********************");
  47.             Console.WriteLine("Newton's Interpolation");
  48.             Console.WriteLine("**********************");
  49.  
  50.             Console.WriteLine($"X0 = {x0}\n");
  51.  
  52.             double result = 0;
  53.             int n = Data.GetLength();
  54.  
  55.             double Li = f[0].y;
  56.             for (int i = 0; i < n; i++)
  57.             {
  58.                 if (i > 0)
  59.                 {
  60.                     Li = dividedDiff(f, 0, i);
  61.                 }
  62.                 for (int j = 0; j < i; j++)
  63.                 {
  64.                     Li *= x0 - f[j].x;
  65.                 }
  66.                 Console.WriteLine($"Li[{i}] = {Li:F8}");
  67.                 result += Li;
  68.             }
  69.             Console.WriteLine();
  70.             return result;
  71.         }
  72.         static void Main(string[] args)
  73.         {
  74.        
  75.             Console.Write("К-сть точок: ");
  76.             int n = int.Parse(Console.ReadLine());
  77.             if (n < 2)
  78.             {
  79.                 Console.WriteLine("Error");
  80.                 Environment.Exit(1);
  81.             }
  82.             Data[] f = new Data[n];
  83.             for (int i = 0; i < n; i++)
  84.             {
  85.  
  86.                 Console.WriteLine($"Введiть {i + 1} точку: ");
  87.                 string[] c = Console.ReadLine().Split(' ');
  88.                 f[i] = new Data(double.Parse(c[0]), double.Parse(c[1]));
  89.             }
  90.             Console.Write("Введiть x0: ");
  91.             double x0 = double.Parse(Console.ReadLine());
  92.             Console.WriteLine($"Value of f({x0}) = {interpolate(f, x0)}");
  93.         }
  94.     }
  95. }
  96.  
Advertisement
Add Comment
Please, Sign In to add comment