How do I fit to 2D data using a custom function defined in a separate file?

32 次查看(过去 30 天)
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!

回答(1 个)

dpb
dpb 2016-11-23
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 个评论
dpb
dpb 2016-11-30
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
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Linear and Nonlinear Regression 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by