using System using System IO using System Collections Generic using Sy

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Линейная_интерполяция
{
class Program
{
static void Main(string[] args)
{
int n = 5;
double[] x = new double[] { -2, 0, 1, 4, 5};
double[] y = new double[] { -5.2502, -1.2502, -0.0002, 18.7498, 34.9998 };
int m = 5;
double[] x_interp = new double[] {0.06, 0, 0.5, 1.0001, 2 };
double[] y_interp = new double[m];
Console.WriteLine("Итерполированная функция:");
int i;
for (i = 0; i < m; i++)
{
y_interp[i] = interp(n, x_interp[i], x, y);
Console.WriteLine("x = {0:f4} y = {1:f4}", x_interp[i], y_interp[i]);
}
Console.ReadLine();
}
static double interp(int n, double xs, double[] x, double[] y)
{
int i = 1;
for (int j = 1; j < n; j++)
if (xs > x[j])
{
i = j;
break;
}
return (double)(y[i] + (y[i + 1] - y[i]) * (xs - x[i]) / (x[i + 1] - x[i]));
}
}
}