How do I fit to 2D data using a custom function defined in a separate file?
Afficher commentaires plus anciens
I have a set of 2D data, which exists in a 2D array of size (m,n). I'd like to fit a surface to this data, and to generate the surface, I have to run a computationally-heavy routine, which takes place in some matlab code named 'surfaceCreator.m'. The details of the routine aren't important (possibly not true), but it basically takes about 40s on my laptop to create a single possible surface, to which the data might match. In the end, I predict the whole procedure to take about an hour.
The problem lies in defining the fittype. At the moment, this is my situation...
load('/Users/person/data.mat'); % <-- loads the data as variable 'I' in workspace.
% x, y, and fitParam are all defined before the following line...
ft = fittype('surfaceCreator(x,y,fitParam(1),fitParam(2),fitParam(3),fitParam(4))');
f = fit( [x,y],I, ft, 'StartPoint', [fitParam(1) fitParam(2) fitParam(3) fitParam(4)] );
The specific syntax of what I've done I've taken from the 'fittype' page, but I'm unsure about it, as I get the following error on running the line that defines 'ft'...
The name y cannot be used for both a coefficient and the dependent variable.
Which makes me think that this form of using 'fittype' doesn't account for 2D data. So, I found the following amendment (with the resulting error)...
ft = fittype('surfaceCreator(x,y,fitParam(1),fitParam(2),fitParam(3),fitParam(4))','numindep',2);
Error in fittype expression ==> surfaceCreator(x,y,fitParam(1),fitParam(2),fitParam(3),fitParam(4))
??? Index exceeds matrix dimensions.
At this point, I can't tell if it's just syntax I'm getting wrong, or the approach in the first place. I can't place the function itself within the 'fittype' parentheses, as the function is an integral of a function, plus some other complications, but 'surfaceCreator' does have a simple (m,n) sized array as the output.
Any ideas / clarifications? Thanks!
Réponses (1)
dpb
le 23 Nov 2016
fittype tries to be too clever by far, imo. By default, "x is the independent variable, y is the dependent variable, and all other variables are coefficients of the model." Hence, without using the optional name-value pair arguments to define terms.
I think your case should be sotoo:
ft=fittype('surfaceCreator(x,y,a,b,c,d)', ...
'independent', {'x','y'}, ...
'coefficients',{'a','b','c','d'})
The function will have to be written to accept four coefficients rather than an array; I'm not sure there's any way around that.
5 commentaires
Oliver
le 24 Nov 2016
Seems to be so, the parsing of the expression for independent and dependent variables appears to be independent of the definition of the other whether explicit or not...I successfully created a slightly simplified function that fittype accepted...
>> type surfaceCreator
function Z=surfaceCreator(X,Y,a,b)
R = sqrt(a*X.^2 + b*Y.^2) + eps;
Z = sin(R)./R;
First with only independent variables explicitly given--
>> ft=fittype('surfaceCreator(x,y,a,b)','independent',{'x','y'})
Error using fittype>iTestCustomModelParameters (line 814)
The name y cannot be used for both the independent and the dependent variables.
...
it still expects y to be the dependent even though y has been used as you've noted. So, beat it over the head w/ a (what seems as should be unnecessary) club--
>> ft=fittype('surfaceCreator(x,y,a,b)','independent',{'x','y'},'dependent','z')
ft =
General model:
ft(a,b,x,y) = surfaceCreator(x,y,a,b)
>>
Ok, it seems to have finally gotten the message.
While somewhat inconvenient, one way around this is to use uppercase X,Y for the variables so the builtin lowercase aren't found at all...then it lets you get by without the 'dependent' parameter explicit at the expense of use alternate variables than those you might prefer.
>> ft=fittype('surfaceCreator(X,Y,a,b)','independent',{'X','Y'})
ft =
General model:
ft(a,b,X,Y) = surfaceCreator(X,Y,a,b)
>>
OK, it takes X,Y as the independent and internally, though, it thinks y is the dependent, so may have "issues" when comes to estimation time, I'm not sure.
I've really not tried to use the fit object; I'm "old school" and generally just think it's simpler to write the functional form and use the appropriate solver directly; the user-convenience interface seems to me more complex than to "just do it", but perhaps that's owing to >20-yr history w/ Matlab...yeah, I'm a geezer. :)
ADDENDUM
NB: In use don't overlook that ft has rearranged the order of the variables in the argument list from (x,y,a,b) in the target function to (a,b,x,y).
Oliver
le 30 Nov 2016
dpb
le 30 Nov 2016
Indeed, that's the alternative I mentioned earlier of simply using the solver directly. Seems no less complicated than the gyrations to get around the assumptions made in fittype to me...
Alessandro Maria Laspina
le 20 Mar 2021
What did you write for fitParams? I cannot find the handle in the documentation... neither the fact that the y data needs to go at the end.
Catégories
En savoir plus sur Get Started with Curve Fitting Toolbox dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!