当我尝试运行此代码时:
Data1 = FinancialData["^DJI", {{2003, 10, 21}, {2007, 10, 09}}];
Data2 = {{{{AbsoluteTime[#1] - AbsoluteTime[{2003, 10, 21}]}/
86400}, #2} & @@@ Data1};
Data3 = Data2 //. {x_List} :> x;
Data4 = Data3 //. {x_List} :> x;
Data5 = Data4 //. {x_List} :> x;
Data6 = ArrayReshape[Data5 = Data4 //. {x_List} :> x, {1000, 2}];
NonlinearModelFit[Data6, {10^(a + b u^z +
c u^z Cos[\[Phi] + \[Omega] Log10[u]]), {b < 0, -1 < c < 1, 0.1` <= z <=
0.9`, 4.8` <= \[Omega] <= 13,0 <= \[Phi] <= 2 \[Pi]}}, {a, b, c, z, \
[Omega], \[Phi]}, u]
我收到以下错误消息:
在{a,b,c,z,ω,Φ} = {1 ,,-1.,0.8,0.82,5.62,0.628319}处,梯度不是实数向量。 在{1。,-1.,0.8,0.82,5.62,0.628319}处对函数Experiment`NumericalFunction [<< 1 >>]的梯度的评估失败。
是什么原因造成的?
答案 0 :(得分:1)
First[Data6]
(* {0, 9747.64} *}
具有u = 0
,并且要安装的功能具有Log10[u]
。将u
的值移动1。
Data7 = {First[#] + 1, Last[#]} & /@ Data6
在a
上添加约束并拟合
fit = NonlinearModelFit[
Data7, {10^(a + b u^z + c u^z Cos[\[Phi] + \[Omega] Log10[u]]), {a <
10, b < 0, -1 < c < 1, 0.1` <= z <= 0.9`,
4.8` <= \[Omega] <= 13, 0 <= \[Phi] <= 2 \[Pi]}}, {a, b, c,
z, \[Omega], \[Phi]}, u]
绘制数据并拟合
Show[Plot[fit[u], {u, 1, 1450}], ListPlot[Data7]]
这很不合适。可以通过更改约束条件来获得更好的拟合度。