Fitting a curve as defined by a file, for the integral value
5 次查看(过去 30 天)
显示 更早的评论
Hello,
I am looking to fit the integral of an assumed analytical curve. and would like to use the curve fitting toolbox to do so. I seem to be getting an weired error, since my function can run.
I am trying to curve fit to this data
x = [1 2 3 4 5 6 7 8 9 10] (Abitrary position)
I = [10 9 8 7 6 5 4 3 2 1 ] (Integral of a data)
and would like to find the coefficients a & b which best recreate the I data values.
xrange = 1e3:1:1e5; y = a*exp(-xrange./b)
[mt,c] = meshgrid(x,xrange.*4); c = c'
I am creating a custom curve fitting file / function, where:
x is an arbitrary position array that matches the output size of the integral data,
xrange is an assumed analytical function that upon integration will provide an approximating to the "I" data array.
a & b are the coefficients I would like to find
c is an known array that is constant
function I = FitFile(x,xrange,a,b,c)
I = zeros(size(x));
fun = @(xrange) a*exp(-xrange./b)
for i = 1:numel(x)
I(i) = trapz(xrange,fun(xrange).*c(i,:))
end
end
but matlab is being silly and won't let me do least squares fitting objects in this fashion.
I have accomidated the curve fitting toolbox by providing a dummy x input as the same length as the desired output array, but it gives this error:
fittype('FitFile(x,xrange,a,b,c)')
Error using fittype/testCustomModelEvaluation (line 12)
Expression FitFile(x,xrange,a,b,c) is not a valid MATLAB expression, has non-scalar coefficients, or cannot be evaluated:
Error in fittype expression ==> FitFile(x,xrange,a,b,c)
??? Dimension argument must be a positive integer scalar within indexing range.
Any help of where to go with this would be much appreciated.
Thanks!
2 个评论
采纳的回答
更多回答(2 个)
Matt J
2022-2-11
编辑:Matt J
2022-2-11
Your Fitfile doesn't match the model previously described y = a*exp(-x./b)+c so I'm not sure which one is supposed to be correct.
Using a custom model might be unnecessary, though. If I assume I are measurements of y = a*exp(-x./b)+c integrated with respect to x, I can do the integral analytically to obtain,
I=-b*a*exp(-x/b) + c*x + C
Since c*x is known, it can be subtracted from both sides and the right hand side can be reparametrized to obtain,
Y = A*exp(B*x) + C
where Y=I-c*x is known, A=-b*a, and B=-1/b. The reformulated model can be fit with,
fit(x,Y,'exp2','Lower',[-inf,-inf,-inf,0],'Upper',[+inf,0,+inf,0])
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Get Started with Curve Fitting Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!