用arctan逼近曲线

时间:2018-05-05 11:53:45

标签: r

我有一组数据点,看起来很像arctan(x)的某个版本。

xs <- seq(1000,50000,by=1000)

data <- c(0.101, 0.169, 0.209, 0.256, 0.289, 0.373, 0.391, 0.418, 0.477, 0.528, 0.579, 0.570, 0.602, 0.657, 0.690, 0.720, 0.747, 0.764, 0.781, 0.811, 0.842, 0.847, 0.864, 0.889, 0.871, 0.894, 0.897, 0.915, 0.926, 0.931, 0.932, 0.940, 0.950, 0.963, 0.956, 0.967, 0.967, 0.963, 0.971, 0.980, 0.986, 0.988, 0.986, 0.985, 0.993, 0.992, 0.994, 0.991, 0.995, 0.993)

plot(xs,data)

在圣人身边摆弄一下后,我想出了曲线y = 2*atan(xs/1000)/pi

然而,1000只是猜测。

近似证明是合理的

model <- lm(data ~ I(2*atan(xs/10000)/pi))

points(xs,model$fitted.values, pch=20)

我的问题是,我怎样才能得到R&#34;猜测&#34;曲线y = 2*atan(x/a)/pi中的值a?

谢谢!

1 个答案:

答案 0 :(得分:1)

我们可以编写一个损失函数(在下面的代码中,均方误差),并获得optim以最小化损失:

form <- function(a, xs) 2 * atan(xs / a) / pi
loss <- function(a, xs, data) mean((form(a, xs) - data)^2)
optimized <- optim(1000, loss, xs = xs, data = data, method = "Brent", lower = 1, upper = 10000)
a <- optimized$par
a
# [1] 7069.42