Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Xml;
- namespace VM
- {
- internal class Program
- {
- public static void Main(string[] args)
- {
- List<double> x = new List<double>();
- double eps = Math.Pow(10, -6);
- for(double k = 0; k <= 3; k += 0.4)
- x.Add(k);
- Console.WriteLine("Sin");
- List<double> f = x.Select(t => Si(t,eps)).ToList();
- Print(x, f);
- Console.WriteLine("\nИнтер. полином Ньютона");
- List<double> Ln = x.Select((t, i) => Newton(x, f, f[i])).ToList();
- Print(x,Ln);
- Console.WriteLine("Погрешности:");
- for (int i = 0; i < x.Count; i++)
- {
- Console.WriteLine(Math.Abs(f[i] - Ln[i]));
- }
- }
- #region Newton
- private static double RR(List<double> y, List<double> x)
- {
- if (y.Count > 2)
- {
- var yleft = new List<double>(y);
- var xleft = new List<double>(x);
- xleft.RemoveAt(0);
- yleft.RemoveAt(0);
- var yright = new List<double>(y);
- var xright = new List<double>(x);
- xright.RemoveAt(y.Count - 1);
- yright.RemoveAt(y.Count - 1);
- return (RR(yleft, xleft) - RR(yright, xright)) / (x[x.Count - 1] - x[0]);
- }
- if (y.Count == 2)
- {
- return (y[1] - y[0]) / (x[1] - x[0]);
- }
- throw new Exception("Not available parameter");
- }
- private static double Newton(List<double> xPoints, List<double> yPoints, double x)
- {
- var res = yPoints[0];
- for (var i = 1; i < yPoints.Count; i++)
- {
- var xlist = new List<double>();
- var ylist = new List<double>();
- double buf = 1;
- for(var j = 0; j <= i; j++)
- {
- xlist.Add(xPoints[j]);
- ylist.Add(yPoints[j]);
- if (j<i)
- buf *= x - xPoints[j];
- }
- // Console.WriteLine(RR(Ylist, Xlist));
- res += RR(ylist, xlist)*buf;
- }
- return res;
- }
- #endregion
- private static double Si(double x,double eps)
- {
- int i = 0;
- double s = 0;
- double ai = x;
- double qn;
- while (Math.Abs(ai) >= eps)
- {
- s += ai;
- qn = -x * x * (2 * i + 1) / ((2 * i + 3) * (2 * i + 3) * (2 * i + 2));
- ai *= qn;
- i++;
- }
- return s;
- }
- private static void Print(IReadOnlyList<double> x, IReadOnlyList<double> y)
- {
- for (var i = 0; i < x.Count; i++)
- {
- Console.WriteLine($"{x[i]}: {y[i]}");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement