Fitting a curve as defined by a file, for the integral value

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 Comments

Expression FitFile(x,xrange,a,b,c) is not a valid MATLAB expression, has non-scalar coefficients, or cannot be evaluated:
That much is true for the code as you've shown it. This line,
I = zeros(size(x);
is missing a closing ')' and will throw an error.
Sorrry for the splattering of typo's on the question.

Sign in to comment.

 Accepted Answer

Quantum_C
Quantum_C on 22 Feb 2022
Edited: Quantum_C on 22 Feb 2022
Followup to help others,
It appears that to solve this problem:
1. fit( & fittype( -> custom model evaluations are poorly suited for this type of problem, i.e. fitting a integral as fittype does some kind poorly written model evaluation which imposes additional constraints on the function as well as doesn't give users the line # of where the custom fit function fails. (Very poor coding for trace back, cannot believe that that passed Mathworks internal quality review).
2. I followed the example as provided by https://www.mathworks.com/matlabcentral/answers/229110-fitting-to-a-curve-defined-by-an-integral#answer_427759 which was quite helpful and broke the problem up in this way, and it easily takes in a variety of x&y values, doesn't care about how many itterations are requested on the for loop etc. (as in the cases where I only wanted specific iteration numbers from the trapz for loop from above).
3. Summary: USE lsqcurvefit( if you have an integral and ignore matlabs fit( & fittype( functions entirely, they just don't work for this.

More Answers (2)

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])

1 Comment

Hey thanks for the recommendation, much appreciated
So my typo was is definetly in the worst place, in general I am solving some f(x) with an arbitrary number of coeffients (I simplified the function to an exponetial, to keep the math straight forward), so a couple of the functions which I am solving could be integrated, but since I need to do trapz(x,f(x).*c(i,:)) as size(c ) == size(x), I canont to Integration By Parts for this type of solution.

Sign in to comment.

Quantum_C
Quantum_C on 13 Feb 2022
Edited: Quantum_C on 13 Feb 2022
So after some digging,
  1. it seems like Matlab doesn't like a matrix being passed into the 'problem' parameters of a fit? for some reason this breaks the Fit to a file script
  2. Also, I would like ot pass a zero length array, but this also doesn't seem to be allowed.
  3. Stuctures also seem like they are not allowed to be passed into the 'problem' variable space.
Right now I am by-passing this issues with assigning the "c" variable to global and the other variable (not included in the question) to global as well

Products

Release

R2021b

Asked:

on 11 Feb 2022

Edited:

on 22 Feb 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!