How to do numerical fit
8 views (last 30 days)
Show older comments
Alessandro Tartaglia
on 10 Jul 2014
Commented: Megan
on 9 Jun 2023
Hi all, I have some problems in fitting my experimental data. Say I have two columns of data, the independent variable X and the depedent one Y. I want to fit the following model: Y=y0 + a*X*integral(f(z);0,t*X), where integral(F(z);0,t*X) means that i'm integrating the function f(z) on the interval [0, t*X]. The parameters to be determined through the fitting are y0, a and t. The problem is that I have not an analytical expression for the function F: I have a file .dat containing two columns,expressing the value of F at certain points. The amount of points in which F is evaluated is very huge and I managed to do a very good cubic spline interpolation; however this does not solve my problem, because I don't know how to construct a fitting procedure for that model. Is there a way to do that with MATLAB?
1 Comment
Megan
on 9 Jun 2023
To get Matlab's "fit" function to work properly, your own fit function (let's say it is "piecewiseLine(x,a,b,c,d,k)")must be saved within your current directory, it cannot merely be defined within the confines of your code. This detail had me stuck for more time than I'd like to admit!
Accepted Answer
Michael Haderlein
on 10 Jul 2014
Edited: Michael Haderlein
on 10 Jul 2014
Dear Alessandro,
The fitting procedure can also handle non-analytical functions (that's what you need, if I got you correctly). You can use any function in the fitting equation, also self-defined ones:
>> f=fittype('a*myfun(x)');
>> c=fit(x,y,f);
In myfun , you can write whatever you want, may it be an analytical function, a complex code with iterations, or a database look-up. Just make sure to save it as *.m file at a path Matlab can find.
However, be aware that the function will be called pretty often, so make sure it's fast as possible. Thus, a database look-up might not be the best way, but your spline could do much better.
Best regards,
Michael
0 Comments
More Answers (1)
Michael Haderlein
on 10 Jul 2014
Please check out the help for the "fit"-function, there is a very helpful and intuitive example.
Easiest way is to save the file just in your default Matlab folder. Otherwise, you can add directories to the search path list by addpath or by right-clicking the desired folder in the Matlab-explorer window.
0 Comments
See Also
Categories
Find more on Regression in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!