How to fit data with a 2+column matrix to a function?

7 Ansichten (letzte 30 Tage)
blaine_m
blaine_m am 10 Feb. 2019
Beantwortet: Szu-Yu Lee am 8 Apr. 2021
I am trying to fit several arrays of data to several premade functions using the fit function in order to obtain the goodnes-of-fit statistics for each function. Specifically, my data consists of a 19x101 array and a 19x1 array as inputs for my functions, and a 19x101 array which serves as the output of the function. This is one of the functions as an example, but they all follow the same format:
function y = pureSHG(x,z,s,a,b,d)
if ~ismatrix(x) || (~ismatrix(s) || ~ismatrix(z))
error('Inputs should be vectors')
end
a=double(a);
b=double(b);
d=double(d);
y = a./((b^2+x.^2).*(d^2+z.^2)).*(s).^2;
if ~ismatrix(y)
error('Output is not a matrix')
end
end
Here x corresponds to the 19x101 input array, s corresponds to the 19x1 array, and a, b and d are the fitting parameters. The scripts I have for generating a fittype for each function and fitting my data to said function, using the pureSHG function as an example, are as follows:
m2p = @(a,b,d,x,s) pureSHG(2*pi*c/n1.*(1./x-1/lambda1),2*pi*c/n2.*(2./x-1/lambda2),s,a,b,d);
mode_2_pure = fittype(m2p,'coefficients',{'a','b','d'},'independent',{'x','s'},'dependent','y');
[fit_2_pure, gof_2_pure] = fit([SHG_OSA,PowerInput_S], PowerOutput_S, mode_2_pure,'independent',{SHG_OSA,PowerInput_S});
where SHG_OSA is my 19x101 input array, PowerInput_S is the 19x1 inpout array and PowerOutput_S is the 19x101 output array. When I run the script, I get this error:
Error using fit>iFit (line 127)
X must be a matrix with one or two columns.
Error in fit (line 108)
[fitobj, goodness, output, convmsg] = iFit( xdatain, ydatain, fittypeobj, ...
Error in SHGTHGfit (line 34)
[fit_2_pure, gof_2_pure] = fit([SHG_OSA,PowerInput_S], PowerOutput_S, mode_2_pure,'independent',{SHG_OSA,PowerInput_S});
It seems that fit can only accept at most a two-column matrix for x, which doesn't work for my data. I've tried using prepareSurfaceData on my data, but because the arrays don't all have the same number of elements, I just get an error. I've also tried using the curve fitting toolbox, but that just gives the same error as above. I've also troubleshot all the steps in my code up to this point. Is there some way to allow fit to accept matrix arrays with more than two columns? If not, is there some other matlab function that will allow me to fit this data? Am I just missing something with how fit works?

Antworten (1)

Szu-Yu Lee
Szu-Yu Lee am 8 Apr. 2021
Hi, I guess you are trying to fit a large number of independent equations? MATLAB "fit" function do not support multiple independent curves fitting. I have created a function that takes in a 2D matrix, where each row is a curve to be fitted to some polynomial expression. The function does not use for-loop, so can work on large number of rows in a very short time.
https://www.mathworks.com/matlabcentral/fileexchange/90017-matrix-row-wise-polynomial-fit

Produkte


Version

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by