Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Lagrange_Interpolation
- {
- class Program
- {
- class Data
- {
- private static int length = 0;
- public double x, y;
- public Data(double x, double y)
- {
- this.x = x;
- this.y = y;
- length++;
- }
- ~Data()
- {
- length--;
- }
- public static int GetLength()
- {
- return length;
- }
- };
- static double interpolate(Data[] f, double x0)
- {
- Console.WriteLine("************************");
- Console.WriteLine("Lagrange's Interpolation");
- Console.WriteLine("************************");
- Console.WriteLine($"X0 = {x0}\n");
- double result = 0;
- int n = Data.GetLength();
- for (int i = 0; i < n; i++)
- {
- double diagonal = f[i].y;
- for (int j = 0; j < n; j++)
- {
- if (j != i)
- diagonal *= (x0 - f[j].x) / (f[i].x - f[j].x);
- }
- Console.WriteLine($"L[{i}] = {diagonal:F8}");
- result += diagonal;
- }
- Console.WriteLine();
- return result;
- }
- static void Main(string[] args)
- {
- Console.Write("К-сть точок: ");
- int n = int.Parse(Console.ReadLine());
- if (n < 2)
- {
- Console.WriteLine("Error");
- Environment.Exit(1);
- }
- Data[] f = new Data[n];
- for (int i = 0; i < n; i++)
- {
- //Точки ввдоити через пробіл(розділовий знак кома)
- Console.WriteLine($"Введiть {i + 1} точку: ");
- string[] c = Console.ReadLine().Split(' ');
- f[i] = new Data(double.Parse(c[0]), double.Parse(c[1]));
- }
- Console.Write("Введiть x0: ");
- double x0 = double.Parse(Console.ReadLine());
- Console.WriteLine($"Value of f({x0}) = {interpolate(f, x0)}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement