Advertisement
TheDoskz

Untitled

May 17th, 2021
963
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.17 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Xml;
  5.  
  6. namespace VM
  7. {
  8.     internal class Program
  9.     {
  10.        
  11.         public static void Main(string[] args)
  12.         {
  13.             List<double> x = new List<double>();
  14.             double eps = Math.Pow(10, -6);
  15.             for(double k = 0; k <= 3; k += 0.4)
  16.                 x.Add(k);
  17.            
  18.             Console.WriteLine("Sin");
  19.             List<double> f = x.Select(t => Si(t,eps)).ToList();
  20.             Print(x, f);
  21.            
  22.             Console.WriteLine("\nИнтер. полином Ньютона");
  23.             List<double> Ln = x.Select((t, i) => Newton(x, f, f[i])).ToList();
  24.             Print(x,Ln);
  25.            
  26.             Console.WriteLine("Погрешности:");
  27.             for (int i = 0; i < x.Count; i++)
  28.             {
  29.                 Console.WriteLine(Math.Abs(f[i] - Ln[i]));
  30.             }
  31.  
  32.            
  33.         }
  34.  
  35.        
  36.         #region Newton
  37.         private static double RR(List<double> y, List<double> x)
  38.         {
  39.             if (y.Count > 2)
  40.             {
  41.                 var yleft = new List<double>(y);
  42.                 var xleft = new List<double>(x);
  43.                 xleft.RemoveAt(0);
  44.                 yleft.RemoveAt(0);
  45.                 var yright = new List<double>(y);
  46.                 var xright = new List<double>(x);
  47.                 xright.RemoveAt(y.Count - 1);
  48.                 yright.RemoveAt(y.Count - 1);
  49.                 return (RR(yleft, xleft) - RR(yright, xright)) / (x[x.Count - 1] - x[0]);
  50.             }
  51.             if (y.Count == 2)
  52.             {
  53.                 return (y[1] - y[0]) / (x[1] - x[0]);
  54.             }
  55.  
  56.             throw new Exception("Not available parameter");
  57.         }
  58.         private static double Newton(List<double> xPoints, List<double> yPoints, double x)
  59.         {
  60.             var res = yPoints[0];
  61.             for (var i = 1; i < yPoints.Count; i++)
  62.             {
  63.                 var xlist = new List<double>();
  64.                 var ylist = new List<double>();
  65.                 double buf = 1;
  66.                 for(var j = 0; j <= i; j++)
  67.                 {
  68.                     xlist.Add(xPoints[j]);
  69.                     ylist.Add(yPoints[j]);
  70.                     if (j<i)
  71.                         buf *= x - xPoints[j];
  72.                 }
  73.                 // Console.WriteLine(RR(Ylist, Xlist));
  74.                 res += RR(ylist, xlist)*buf;
  75.             }
  76.             return res;
  77.         }
  78.         #endregion
  79.         private static double Si(double x,double eps)
  80.         {
  81.             int i = 0;
  82.             double s = 0;
  83.             double ai = x;
  84.             double qn;
  85.             while (Math.Abs(ai) >= eps)
  86.             {
  87.                 s +=  ai;
  88.                 qn = -x * x * (2 * i + 1) / ((2 * i + 3) * (2 * i + 3) * (2 * i + 2));
  89.                 ai *= qn;
  90.                 i++;
  91.             }
  92.            
  93.             return s;
  94.         }
  95.         private static void Print(IReadOnlyList<double> x, IReadOnlyList<double> y)
  96.         {
  97.             for (var i = 0; i < x.Count; i++)
  98.             {
  99.                 Console.WriteLine($"{x[i]}:     {y[i]}");
  100.             }
  101.         }
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement