Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Collections.Generic;
- namespace ConsoleApplication1
- {
- class Program
- {
- static double MyFunc(double x)
- {
- return Math.Abs(x);
- }
- static void PiecewiseLinearInterpolation(List<double> MyList, Func<double, double> F)
- {
- Console.WriteLine("Кусочно-линейная интерполяция");
- for (int i = 0; i < MyList.Count - 1; i++)
- {
- double x1 = MyList[i];
- double x2 = MyList[i + 1];
- double y1 = F(x1);
- double y2 = F(x2);
- double a1 = (y2 - y1) / (x2 - x1);
- double a0 = y1 - a1 * x1;
- double x0 = (x2 + x1) / 2;
- double y0 = a1 * x0 + a0;
- Console.WriteLine("1: x = {0}, \ty = {1}", x1, y1);
- Console.WriteLine("2: x = {0}, \ty = {1}\n", x1, y1);
- }
- }
- static double InterpolationPolynomialLagrange(double h, double[] xValues, double[] yValues, int n)
- {
- Console.WriteLine("Интерполяционный многочлен в форме Лагранжа");
- double lagrangePol = 0;
- for (int i = 0; i < n; i++)
- {
- double basicsPol = 1;
- for (int j = 0; j < n; j++)
- {
- if (j != i)
- {
- basicsPol *= (h - xValues[j]) / (xValues[i] - xValues[j]);
- }
- }
- Console.WriteLine("x = {0}\t y = {1} ",xValues[i], yValues[i]);
- lagrangePol += basicsPol * yValues[i];
- }
- Console.WriteLine("Конечный результат, сделанный на всякий случай");
- return lagrangePol;
- }
- static void Main(string[] args)
- {
- Func<double, double> F = MyFunc;
- double a = (-1); //нижняя граница
- double b = 1; //верхняя граница
- double n = 10; //число шагов
- int size = 10;
- double h = (b - a) / n; //шаг
- List<double> MyList = new List<double>(); //узлы интерполяции
- double x = a;
- for (double k = 0; k <= 10; k++)
- {
- x = (a + b) / 2 + (b - a) / 2 * Math.Cos((2 * k + 1) * Math.PI / 2 / (n + 1));
- MyList.Add(x);
- }
- double[] xValues = new double[size];
- double[] yValues = new double[size];
- for (int i = 0; i < MyList.Count - 1; i++)
- {
- xValues[i] = MyList[i];
- yValues[i] = MyFunc(MyList[i]);
- }
- Console.WriteLine(InterpolationPolynomialLagrange(0.2, xValues, yValues, size));
- PiecewiseLinearInterpolation(MyList, MyFunc);
- Console.Read();
- }
- }
- }
- //double y0_0 = F(x0);
- //Console.WriteLine("3: x = {0}, \ty = {1}\n", Math.Round(x0,5), Math.Round(y0_0,5));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement