拉格朗日内插C#版

把以前写的那个Delphi版本翻译成了C#语言。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 拉格朗日内插
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 拉格朗日内插
        /// </summary>
        /// <param name="fx">x序列</param>
        /// <param name="fy">y序列</param>
        /// <param name="xx">要插值的x序号</param>
        /// <param name="n">阶数</param>
        /// <returns></returns>
        public double lagrange(double[] fx, double[] fy, double xx, int n)
        {
            double[] a = new double[n + 1];
            double[] fx2 = new double[n + 1];
            double[] fy2 = new double[n + 1];
            int sum1 = 0;
            int half_n = n / 2;
            int len = fx.Length - 1;
            //===================BEGIN=========取nn个数参与插值,使要求的数在其中间========
            int st_n = Convert.ToInt32(Math.Truncate(xx - fx[0])) / (15 * 60); //时间处于第几个15分钟
            if (st_n >= len)   //超出所有
            {
                for (int j = len - n; j <= len; j++)
                {
                    fx2[sum1] = fx[j];
                    fy2[sum1] = fy[j];
                    sum1++;
                }
            }
            else if (st_n <= 0)  //低于所有
            {
                for (int j = 0; j <= n; j++)
                {
                    fx2[sum1] = fx[j];
                    fy2[sum1] = fy[j];
                    sum1++;
                }
            }
            else if (len - st_n < half_n)  //右边不够
            {
                for (int j = st_n; j <= len; j++)
                {
                    fx2[sum1] = fx[j];
                    fy2[sum1] = fy[j];
                    sum1++;
                }
                for (int j = len - n + 1; j <= st_n; j++)
                {
                    fx2[sum1] = fx[j - 1];
                    fy2[sum1] = fy[j - 1];
                    sum1++;
                }
            }
            else if (st_n < (n / 2))   //左边不够
            {
                for (int j = 0; j <= st_n; j++)
                {
                    fx2[sum1] = fx[j];
                    fy2[sum1] = fy[j];
                    sum1++;
                }
                for (int j = st_n + 1; j <= n; j++)
                {
                    fx2[sum1] = fx[j];
                    fy2[sum1] = fy[j];
                    sum1++;
                }
            }
            else   //两边都够
            {
                for (int j = (st_n - (n / 2)); j <= st_n; j++)
                {
                    fx2[sum1] = fx[j];
                    fy2[sum1] = fy[j];
                    sum1++;
                }
                for (int j = st_n + 1; j <= st_n + n - (n / 2); j++)
                {
                    fx2[sum1] = fx[j];
                    fy2[sum1] = fy[j];
                    sum1++;
                }
            }
            //========================END======取nn个数参与插值,使要求的数在其中间========
            double yy = 0;
            for (int i = 0; i <= n; i++)
            {
                a[i] = fy2[i];
                for (int j = 0; j <= n; j++)
                {
                    if (j != i)
                    {
                        a[i] = a[i] * (xx - fx2[j]) / (fx2[i] - fx2[j]);
                    }
                }
                yy = yy + a[i];
            }
            return yy;
        }
        //函数测试
        private void button1_Click(object sender, EventArgs e)
        {
            int N = 4;
            double[] fx = new double[5] { 0.4, 0.55, 0.8, 0.9, 1 };
            double[] fy = new double[5] { 0.41075, 0.57815, 0.88811, 1.02652, 1.17520 };
            double x = 0.5;
            MessageBox.Show(lagrange(fx, fy, x, N).ToString());
        }
    }
}