Advertisement
Pafnytiu

Методы вычислений - 3

Oct 31st, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.16 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4.  
  5. namespace ConsoleApplication1
  6. {
  7.     class Program
  8.     {
  9.         static double MyFunc(double x)
  10.         {
  11.             return Math.Abs(x);
  12.         }
  13.  
  14.         static void PiecewiseLinearInterpolation(List<double> MyList, Func<double, double> F)
  15.         {
  16.             Console.WriteLine("Кусочно-линейная интерполяция");
  17.             for (int i = 0; i < MyList.Count - 1; i++)
  18.             {
  19.                 double x1 = MyList[i];
  20.                 double x2 = MyList[i + 1];
  21.                 double y1 = F(x1);
  22.                 double y2 = F(x2);
  23.  
  24.                 double a1 = (y2 - y1) / (x2 - x1);
  25.                 double a0 = y1 - a1 * x1;
  26.  
  27.                 double x0 = (x2 + x1) / 2;
  28.                 double y0 = a1 * x0 + a0;
  29.  
  30.                 Console.WriteLine("1: x = {0}, \ty = {1}", x1, y1);
  31.                 Console.WriteLine("2: x = {0}, \ty = {1}\n", x1, y1);
  32.             }
  33.         }
  34.  
  35.         static double InterpolationPolynomialLagrange(double h, double[] xValues, double[] yValues, int n)
  36.         {
  37.             Console.WriteLine("Интерполяционный многочлен в форме Лагранжа");
  38.             double lagrangePol = 0;
  39.  
  40.             for (int i = 0; i < n; i++)
  41.             {
  42.                 double basicsPol = 1;
  43.                 for (int j = 0; j < n; j++)
  44.                 {
  45.                     if (j != i)
  46.                     {
  47.                         basicsPol *= (h - xValues[j]) / (xValues[i] - xValues[j]);
  48.                     }
  49.                 }
  50.                 Console.WriteLine("x = {0}\t y = {1} ",xValues[i], yValues[i]);
  51.                 lagrangePol += basicsPol * yValues[i];
  52.             }
  53.             Console.WriteLine("Конечный результат, сделанный на всякий случай");
  54.             return lagrangePol;
  55.         }
  56.  
  57.         static void Main(string[] args)
  58.         {
  59.             Func<double, double> F = MyFunc;
  60.             double a = (-1);        //нижняя граница
  61.             double b = 1;   //верхняя граница
  62.             double n = 10; //число шагов
  63.             int size = 10;
  64.             double h = (b - a) / n; //шаг          
  65.  
  66.             List<double> MyList = new List<double>(); //узлы интерполяции
  67.  
  68.             double x = a;
  69.  
  70.             for (double k = 0; k <= 10; k++)
  71.             {
  72.                 x = (a + b) / 2 + (b - a) / 2 * Math.Cos((2 * k + 1) * Math.PI / 2 / (n + 1));
  73.                 MyList.Add(x);
  74.             }
  75.  
  76.             double[] xValues = new double[size];
  77.             double[] yValues = new double[size];
  78.  
  79.             for (int i = 0; i < MyList.Count - 1; i++)
  80.             {
  81.                 xValues[i] = MyList[i];
  82.                 yValues[i] = MyFunc(MyList[i]);
  83.             }
  84.  
  85.             Console.WriteLine(InterpolationPolynomialLagrange(0.2, xValues, yValues, size));
  86.  
  87.             PiecewiseLinearInterpolation(MyList, MyFunc);
  88.             Console.Read();
  89.         }
  90.     }
  91. }
  92.  
  93. //double y0_0 = F(x0);
  94. //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