How do I fit to 2D data using a custom function defined in a separate file?
조회 수: 33 (최근 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!
댓글 수: 0
답변 (1개)
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
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
2021년 3월 20일
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 Center 및 File Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!