使用 nls - 奇异梯度将 erf 拟合到 R 中的几个实验数据点

问题描述 投票:0回答:1

我需要拟合一个描述数据物理模型的误差函数,使其仅包含 6 个实验数据点。

误差函数为:

func_erf <- function(x, #m
                 D,     #m2/s
                 t,     #s
                 s      #m
){ 
  result = (erf((x+s)/sqrt(8*D*t)) - erf((x-s)/sqrt(8*D*t)))/(2*erf(s/sqrt(8*D*t)))
  return(result)
}

我想要拟合的数据是:

> data_exp
1 1.000000000 0.000
2 0.766766619 0.001
3 0.252337795 0.002
4 0.098405369 0.003
5 0.046523446 0.004
6 0.004363998 0.005

通过实验,我们知道 t = 6.504601e-05 秒。所以我们只需要将参数 D 和 s 拟合到我们的实验数据即可。

假设拟合曲线的起始参数有点接近实验数据,并用 darta 点绘制初始拟合猜测,我得到:

但是,nls 拟合过程总是会导致奇异梯度矩阵的误差。

coef_fit_erf_guess = c(1e-8,       #D, #m2/s
                       0.75*1e-3   #s, #m
)
t_exp = 6.504601e-05 #seconds

fit_nls_erf<- nls(y~func_erf(x,D,t= t_exp, s),data=data_exp,
                             start=list(D = coef_fit_erf_guess [1],
                                        s = coef_fit_erf_guess [2]))


为什么这个安装程序不起作用?我可以改进什么?有没有办法找到更好的拟合猜测或以迭代“手动”方式拟合?

非常感谢您的帮助!

r curve-fitting nls convergence
1个回答
0
投票

误差面看起来很奇怪。您可以使用以下代码获得可旋转的绘图:

data_exp <- structure(list(id = c(1, 2, 3, 4, 5, 6), 
  y = c(1, 0.766766619,0.252337795, 0.098405369, 0.046523446, 0.004363998), 
  x = c(0, 0.001, 0.002, 0.003, 0.004, 0.005)), 
  class = "data.frame", row.names = c(NA, -6L))

func_erf <- function(x, #m
                     D,     #m2/s
                     t,     #s
                     s      #m
){ 
  result = (erf((x+s)/sqrt(8*D*t)) - erf((x-s)/sqrt(8*D*t)))/(2*erf(s/sqrt(8*D*t)))
  return(result)
}

SS <- function(D, s) {
  with(data_exp, sum((y - func_erf(x, D, t = 6.504601e-05, s))^2))
}

SS <- Vectorize(SS)
library(rgl)
library(pracma)
persp3d(SS, xlim = c(0, 1e-5), ylim = c(0.00095, 0.003))

创建于 2023-11-14,使用 reprex v2.0.2

我并不感到惊讶

nls()
遇到了麻烦。

© www.soinside.com 2019 - 2024. All rights reserved.