【发布时间】:2015-12-24 10:43:30
【问题描述】:
erfc Matlab 函数是互补误差函数,定义为:
erfc(x) = 2/sqrt(pi) * integral from x to inf of exp(-t^2) dt
我想把这个函数改成:
erfc_change(a+b*R) = 1/2*sqrt(pi) * integral from (a+b*vec_x) to inf of exp(-t^2/2) dt %note here exp(-t^2/2) and vec_x is a vector of values that represent the values of x in the erfc function
用于最小二乘误差曲线拟合(常数“a”和“b”应近似)。
所以我做了一个变量更改,我得到了这个结果:
erfc_change(a+b*vec_x) = (sqrt(2*pi)/2)*erfc(a+b*vec_x/sqrt(2))
我不确定我更改变量的可靠性。
在此之后,我像这样执行我的最小二乘拟合 ==> 这是我可运行的代码:
vec_x=
[0;0.4636;0.6616;0.8225;0.1095;0.1706;0.2302;0.1603;
0.2392;0.3245;0.3741;0.5376;0.6675;0.1308;0.1881;
0.2296;0.03740;0.002600;0.04530;0.02660;0.02990;0.0297];
vec_y=[3.3010;5.5840;7.2970;8.8660;4.1200;5.4140;7.1710;
4.5820;6.5400;6.8220;5.6220;8.0110;8.6600;
3.4010;3.7460;4.7180;2.9260;3.4290;4.2780;2.2480;3.8900;4.359];
options = optimoptions('lsqcurvefit','Algorithm','levenberg-marquardt');
f = @(x,vec_x)(1/sqrt(2))*erfc(x(1)+(x(2)*vec_x/sqrt(2)));
lsqcurvefit(f,-2,vec_x,vec_y); % the approximation will starts from -2
这里出现错误(执行上述代码后):
Index exceeds matrix dimensions.
Error in @(x,fmpd_dinosaur)(1/sqrt(2))*erfc(x(1)+
(x(2)*fmpd_dinosaur/sqrt(2)))
Error in lsqcurvefit (line 199)
initVals.F = feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
Caused by:
Failure in initial user-supplied objective function evaluation.
LSQCURVEFIT cannot continue.
我不明白这个错误来自哪里。有任何想法吗 ?
【问题讨论】:
-
请分享一些可重现和清晰的代码。您可以使用换行符后跟四个连续的空格来格式化为代码。这样看起来会干净得多。
-
请提供可运行代码,包括您如何调用
lsqcurvefit,以便可以复制错误。此外,您更改的函数似乎与它下面的代码不匹配。 -
好的,我添加了一个可运行的代码。谢谢
标签: matlab