nls 中的错误 - 迭代次数超过最大值

Error in nls - number of iterations exceeded maximum

我在尝试拟合数据时遇到问题。 这是数据:

x = c(1, 1.071519305, 1.148153621, 1.230268771, 1.318256739, 1.412537545, 1.513561248, 1.621810097, 1.737800829, 1.862087137, 1.995262315, 2.13796209, 2.290867653, 2.454708916, 2.630267992, 2.818382931, 3.01995172, 3.235936569, 3.467368505, 3.715352291, 3.981071706, 4.265795188, 4.570881896, 4.897788194, 5.248074602, 5.623413252, 6.025595861, 6.45654229, 6.918309709, 7.413102413, 7.943282347, 8.511380382, 9.120108394, 9.77237221, 10.47128548, 11.22018454,    12.02264435, 12.88249552, 13.80384265, 14.79108388, 15.84893192, 16.98243652, 18.19700859, 19.498446, 20.89296131, 22.38721139, 23.98832919, 25.70395783, 27.54228703, 29.51209227, 31.6227766, 33.88441561, 36.30780548, 38.9045145, 41.68693835, 44.66835922, 47.86300923, 51.2861384, 54.95408739, 58.88436554, 63.09573445, 67.60829754, 72.44359601, 77.62471166, 83.17637711, 89.12509381,    95.4992586, 102.3292992, 109.6478196, 117.4897555, 125.8925412, 134.8962883, 144.5439771, 154.8816619, 165.9586907, 177.827941, 190.5460718, 204.1737945, 218.7761624, 234.4228815, 251.1886432, 269.1534804, 288.4031503, 309.0295433, 331.1311215, 354.8133892, 380.1893963, 407.3802778, 436.5158322, 467.7351413, 501.1872336, 537.0317964, 575.4399373, 616.5950019, 660.693448, 707.9457844, 758.577575, 812.8305162, 870.96359, 933.2543008, 1000)

y = c(0, 39.42531967, 81.67031097, 126.9366341, 179.8504534, 237.9146471, 300.9332733, 373.9994125, 452.2911911, 544.5717812, 644.4305916,757.5670443, 880.1954813, 1015.045167, 1160.563695, 1316.477197, 1483.424418, 1668.380672, 1869.099593, 2083.298305, 2308.72922, 2552.533248, 2806.782363, 3074.749213, 3354.913032, 3653.567198, 3961.982443, 4285.416754, 4625.505185, 4974.839962, 5329.418374, 5696.722268, 6069.748689, 6447.903256, 6826.334958, 7218.057591, 7607.64304, 8005.992733, 8403.318251, 8798.355661, 9201.456877, 9613.1821, 10022.47749, 10430.83497, 10841.5067, 11256.68048, 11675.94707, 12085.72448, 12500.17168, 12905.54582, 13311.92593, 13707.0245, 14089.76524, 14459.48122, 14813.21421, 15145.30591, 15459.10593, 15752.7922, 16023.09928, 16269.888, 16493.69043, 16693.68774, 16869.79643, 17021.69506, 17154.34004, 17264.76423, 17355.82129, 17427.48725, 17486.7706, 17530.49824, 17563.61638, 17588.39795, 17605.32753, 17617.36935, 17624.3971, 17629.48694, 17632.2512, 17633.91595, 17634.67971, 17635.11862, 17635.35591, 17635.4941, 17635.61014, 17635.64404, 17635.66099, 17635.67794, 17635.67794, 17635.67794, 17635.67794, 17635.67794, 17635.67794, 17635.67794, 17635.67794, 17635.67794, 17635.67794, 17635.67794, 17635.67794, 17635.67794, 17635.67794, 17635.67794, 17635.67794)

编辑

迭代次数增加到 1000。

这是我在解释系数结果时与具有物理意义的方程一起使用的代码:

# tanh function definition:
ftanh = function(x, x0, a, b, k) {
  (1/2) * a * (1 + (tanh(k*(x-x0)))) + b
}

# Fitting code using nonlinear least square:
nlc <- nls.control(maxiter = 1000)
fitmodel <- nls(y ~ ftanh(x, x0, a, b, k), control=nlc, start=list(x0 = 7, a = -29500, b = 17500, k = -0.032))

# Plotting fitted cumulative function

options(scipen = 10) 

plot(x, predict(fitmodel), type="l", log = "x", col="blue", xlab = "x", ylab = "y")

points(x, y, col = "red")

legend("topleft", inset = .05, legend = c("exp","fit"),
       lty = c(NA,1), col = c("red", "blue"), pch = c(1,NA), lwd = 1, bty = "n")

summary(fitmodel) 

这是我使用 Excel 仅通过猜测初始值(橙色线)得到的最佳拟合:

我假设我的初始值不是最好的,但我一直在循环中,似乎没有出路。我很确定 b 值没问题,而且 x0a 都在我的初始值附近,但是 k 实际上是在确定陡度。任何的想法?

问题是你的拟合函数没有描述你的数据。在对数图中它看起来不错,但实际上更像是一个简单的 tanh。由于弯曲不同于 tanh 更好的模型是类型 a tanh(b*(x-c)**d)**(1/d)

不使用 r 但简单 python 看起来像:

import matplotlib
matplotlib.use('Qt4Agg')

from matplotlib import pyplot as plt
import numpy as np
from scipy.optimize import curve_fit 


def func1(x, a, b, c):
    return a*np.tanh(b*(x-c)) 

def func2(x, a, b, c,d):
    return a*np.tanh(b*np.fabs((x-c))**d)**(1/d) 


initialGuess1=[y[-1], 1, 0]
initialGuess2=[y[-1], 1, 0,1]

popt1,pcov1 = curve_fit(func1, x, y,initialGuess1)
popt2,pcov2 = curve_fit(func2, x, y,initialGuess2)

print popt1
print popt2

fittedData1=[func1(s, *popt1) for s in x]
fittedData2=[func2(s, *popt2) for s in x]

fig=plt.figure()
ax=fig.add_subplot(1,1,1)
ax.plot(x,y)
ax.plot(x,fittedData1)
ax.plot(x,fittedData2)
ax.set_xscale('log')

plt.show()

>> [  1.74207552e+04   3.53554258e-02   2.20477585e-01]
>> [  1.76893061e+04   1.90416542e-01   1.19819247e+00   5.59529032e-01]

蓝色为数据,tanh橙色为拟合,绿色为修改后的拟合函数。

初步猜测很简单。 (您可以尝试一下以摆脱零附近的扭结)