我是Math.Net库的新手,我在尝试基于指数函数进行曲线拟合时遇到了问题.更具体地说,我打算使用这个功能:
f(x) = a*exp(b*x) + c*exp(d*x)
使用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)
是否可以使用Math.Net实现这些结果?
解决方法
没有看起来目前没有指数支持.然而,有一个关于Math.NET论坛的讨论,其中维护者提出了一个解决方法:
https://discuss.mathdotnet.com/t/exponential-fit/131
You can,by transforming it,similar to Linearizing non-linear models
by transformation. Something along the lines of the following should
work:
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]}; }
Example usage:
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