This commit is contained in:
25
Helpers/Interpolation.cs
Normal file
25
Helpers/Interpolation.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
|
||||
namespace MembranePoreTester.Helpers
|
||||
{
|
||||
public static class Interpolation
|
||||
{
|
||||
// 线性插值
|
||||
public static double Linear(double[] xs, double[] ys, double x)
|
||||
{
|
||||
if (xs.Length == 0 || ys.Length == 0 || xs.Length != ys.Length)
|
||||
throw new ArgumentException("数组长度不匹配或为空");
|
||||
|
||||
if (x <= xs[0]) return ys[0];
|
||||
if (x >= xs[^1]) return ys[^1];
|
||||
|
||||
int i = Array.BinarySearch(xs, x);
|
||||
if (i >= 0) return ys[i];
|
||||
|
||||
i = ~i; // 大于 x 的第一个索引
|
||||
double x0 = xs[i - 1], x1 = xs[i];
|
||||
double y0 = ys[i - 1], y1 = ys[i];
|
||||
return y0 + (y1 - y0) * (x - x0) / (x1 - x0);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user