使用Math.Net的基于指数的曲线拟合

eol*_*eol 7 c# matlab curve-fitting mathnet-numerics

我是Math.Net库的新手,我在尝试基于指数函数进行曲线拟合时遇到了问题.更具体地说,我打算使用这个功能:

f(x) = a*exp(b*x) + c*exp(d*x)
Run Code Online (Sandbox Code Playgroud)

使用MATLAB我得到了相当不错的结果,如下图所示:

胡说

MATLAB计算以下参数:

f(x) = a*exp(b*x) + c*exp(d*x)
Coefficients (with 95% confidence bounds):
a =   29.6       ( 29.49     , 29.71)
b =    0.000408  (  0.0003838,  0.0004322)
c =   -6.634     ( -6.747    , -6.521)
d =   -0.03818   ( -0.03968  , -0.03667)
Run Code Online (Sandbox Code Playgroud)

是否可以使用Math.Net实现这些结果?

Ste*_*der 1

不,目前似乎没有指数支持。然而,Math.NET 论坛上有一个讨论,维护者提出了一种解决方法:

https://discuss.mathdotnet.com/t/exponential-fit/131

内容重复,以防链接损坏:

您可以通过对其进行变换,类似于通过变换对非线性模型进行线性化。类似以下内容应该有效:

double[] Exponential(double[] x, double[] y,
    DirectRegressionMethod method = DirectRegressionMethod.QR)
{
    double[] y_hat = Generate.Map(y, Math.Log);
    double[] p_hat = Fit.LinearCombination(x, y_hat, method, t => 1.0, t => t);
    return new[] {Math.Exp(p_hat[0]), p_hat[1]}; 
}
Run Code Online (Sandbox Code Playgroud)

用法示例:

double[] x = new[] { 1.0, 2.0, 3.0 };
double[] y = new[] { 2.0, 4.1, 7.9 };
double[] p = Exponential(x,y); // a=1.017, r=0.687
double[] yh = Generate.Map(x,k => p[0]*Math.Exp(p[1]*k)) // 2.02, 4.02, 7.98
Run Code Online (Sandbox Code Playgroud)