Math.NET里面的多项式拟合求参数和求值

date
Dec 22, 2021
slug
10037
status
Published
tags
C#
Math.NET
summary
type
Post
using System;
using MathNet.Numerics;

namespace ConsoleApp6_mathdotnet
{
    class Program
    {
        static void Main(string[] args)
        {
            double[] x = new double[] { 1, 2, 3, 4, 5, 6 };
            double[] y = new double[] { 101, 22, 13, 4, 75, 79 };
            double[] res = Fit.Polynomial(x, y, 3); //其中 x 是 X 轴的数组, y 是 Y 轴的数组,第三个参数是多项式的阶数
                                                    //返回的结果res是最佳拟合参数的数组[p0,p1,p2,…,pk],
            foreach (var item in res)
            {
                Console.WriteLine(item);
            }



            double[] x1 = new double[] { 2, 4, -1, 5, 4 };//多项式求值,五个系数,f(x)=4x^4+5x^3-x^2+4x+2
            double eva = Polynomial.Evaluate(3, x1);//求f(3)的值,赋给eva
            Console.WriteLine(eva);//eva=464
        }
    }
}
 

© Wen Bo 2021 - 2022