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.Text;
- using System.Threading.Tasks;
- using System.IO;
- namespace interpolationLagranzh
- {
- class Program
- {
- static double func(double x)
- {
- return Math.Log(x)+Math.Cosh(Math.Atan(x));
- }
- static double InterpolateLagrange(double x, double[] x_values, double[] y_values, int size)
- {
- double lagrange_pol = 0;
- double basics_pol;
- for (int i = 0; i < size; i++)
- {
- basics_pol = 1;
- for (int j = 0; j < size; j++)
- {
- if (j == i) continue;
- basics_pol *= ((x - x_values[j]) / (x_values[i] - x_values[j]));
- }
- lagrange_pol += (basics_pol * y_values[i]);
- }
- return lagrange_pol;
- }
- static string pathIn = @"F:\ИТМО\2 курс\ВМ\interpolationLagranzh\interpolationLagranzh\IO\input.txt";
- //static string pathOut = @"F:\ИТМО\2 курс\ВМ\interpolationLagranzh\interpolationLagranzh\IO\output.xlx";
- static int n = 10;
- static double[] x = new double[n];
- static double[] y = new double[n];
- static void Main(string[] args)
- {
- #region Writer
- using (StreamWriter sw = new StreamWriter(pathIn))
- {
- for (int i = 1; i <= n; i++)
- {
- sw.WriteLine(i + "\t" + func(i));
- }
- }
- #endregion
- #region Reader
- using (StreamReader sr = new StreamReader(pathIn))
- {
- for (int i = 1; i <= n; i++)
- {
- string[] s = sr.ReadLine().Split('\t');
- x[i - 1] = double.Parse(s[0]);
- y[i - 1] = double.Parse(s[1]);
- }
- }
- #endregion
- for (double i = 1; i <= 10; i += 0.1)
- {
- double InterpolateLagrange_ans = InterpolateLagrange(i, x, y, n);
- double theory = func(i);
- Console.WriteLine("x = {0:0.0} Lagr ={1:0.000000} Correct = {2:0.000000} delta = {3}", i, InterpolateLagrange_ans, func(i), (InterpolateLagrange_ans - theory));
- }
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement