0

谁能告诉我如何使用最小二乘曲线拟合函数 lsqcurvefit 的例子?在我尝试使用之前,nls()但我得到了类似的东西:

Error: step factor 0.000488281 reduced below 'minFactor' of 0.000976562

pracma中有函数 lsqcurvefit 但我不明白如何使用它。

例如我有一些数据:

time output_cut
1 0.627100000000041
4 -0.0988999999999578
7 0.257100000000037
10 0.614100000000008
13 1.62310000000002
16 -0.814899999999966
19 -0.321899999999914
22 2.10810000000004
25 -2.38589999999999
28 2.9941
31 1.97210000000007
34 -1.53689999999995
37 -1.36789999999996
40 1.0231
43 -0.24389999999994
46 -1.55189999999993
49 -0.330899999999929
52 -0.597899999999981
55 -0.254900000000021
58 0.587100000000078
61 1.2491
64 0.654100000000085
67 -0.916899999999941
70 -2.00889999999993
73 -1.83789999999999
76 -3.53089999999997
79 -2.72489999999993
82 -3.69989999999996
85 -2.53589999999997
88 -6.20389999999998
91 -4.93989999999997
94 -6.41489999999999
97 -5.08989999999994
100 -7.59589999999992
103 -8.29689999999994
106 -6.86889999999994
109 -8.30689999999993
112 -9.61989999999992
115 -6.96889999999996
118 -8.26289999999995
121 -7.74689999999998
124 -8.08989999999994
127 -9.08589999999992
130 -6.7989
133 -9.36789999999996
136 -7.93689999999992
139 -10.9019
142 -9.84089999999992
145 -9.30589999999995
148 -8.59289999999999
151 -9.73489999999993
154 -9.30089999999996
157 -10.3379
160 -10.1378999999999
163 -9.79089999999997
166 -10.0998999999999
169 -9.74090000000001
172 -10.4168999999999
175 -11.2479
178 -11.2089
181 -10.9529
184 -11.1279
187 -10.5958999999999
190 -8.12689999999998
193 -9.81189999999992
196 -12.6999
199 -9.18489999999997
202 -12.8949
205 -9.15589999999997
208 -12.2448999999999
211 -13.0979
214 -12.2609
217 -10.8849
220 -9.48789999999997
223 -11.3959
226 -10.9109
229 -11.4458999999999
232 -9.03890000000001
235 -12.1829
238 -9.72589999999991
241 -11.2719
244 -10.5908999999999
247 -10.9669
250 -9.01789999999994
253 -11.5138999999999
256 -10.9358999999999
259 -9.71589999999992
262 -9.65389999999991
265 -11.0708999999999
268 -9.0299
271 -9.7989
274 -11.0579
277 -11.2379
280 -10.9418999999999
283 -11.9038999999999
286 -11.0808999999999
289 -12.6269
292 -11.0149
295 -10.6649
298 -11.6659
301 -10.9178999999999

使用nls()我写:

model_iner2 <- nls(output_cut ~ k*(1 - 1/(con1 - con2) * (con1 * exp(-time/con1) + con2 * exp(-time/con2))), start=list(k=-3, con1=100, con2=2000), trace=T)

我已经搜索了一个示例,但仍然无法理解。抱歉,如果这是个愚蠢的问题,我是 R 新手

4

1 回答 1

1

我从未使用过 package pracma,也没有真正看到需要在这里使用它。该软件包似乎从 Matlab 移植功能?

nls如果给您带来麻烦,我建议使用包 minpack.lm 。它提供了nls使用 Levenberg-Marquardt 算法的修改版本。使用您的数据:

library(minpack.lm)
model_iner2 <- nlsLM(output_cut ~ k*(1 - 1/(con1 - con2) * (con1 * exp(-time/con1) + con2 * exp(-time/con2))), 
                     start=list(k=-100, con1=2000, con2=0), data=DF, trace=T)
summary(model_iner2)

# Parameters:
#        Estimate Std. Error t value Pr(>|t|)    
#   k     -14.337      1.184 -12.105  < 2e-16 ***
#   con1  149.426     24.073   6.207 1.30e-08 ***
#   con2   18.020      2.168   8.313 5.43e-13 ***

plot(output_cut ~ time,data=DF)
lines(DF$time,predict(model_iner2))

数据和预测值

请注意,我已经调整了起始值,并且拟合对起始值非常敏感。使用正确的起始值,您还可以使用nls. 如果您无法找到好的起始值,您可以使用包nls2在网格上使用蛮力搜索起始值。

于 2013-05-26T12:12:51.440 回答