- #1
marco1235
Good afternoon,
I have a problem which I haven't solved yet, regarding a non-linear fit to a set of experimental data. I tried to solve it in Matlab, which I handle a little bit.
I have a sensor, which has been designed to have 4 different filters in front of it. By making a sweep with the incident radiation, I collect the amount of transmitted light for every filter. So, I have a 1-D array for my increasing Flux, which is a 18x1 array; then, stored in a single matrix, I have a 18x4 output values for the four different filters. In addition I have a 1x4 array specifying the background sensed by the sensor for every filter (called V in my program).
The problem is that now I have an analytical function that I want to fit which is of the form:
y(1) = V(1) + ( b1 - V1 )( 1 - exp ( -( b4*x ) b3 ) ) b2
This is just the first part of the function, namely the function for the first filter, the other 3 (y2, y3, y4) have the same form but different value of the background (V) and different value of b4. Thus, I have 3 shared parameters that are equal for my 4-equations, and the other ones are filter-specific.
To conclude, I have also a 18x4 matrix relative to the weights of my data, in order to perform a weighted non-linear fit.
I would expect that the fit retrieves me an array with all the 7 parameters, together with their standard error or confidence interval.
Below, two different approaches
that I tried but that didn't work for my purposes.
Thanks in advance!
I have a problem which I haven't solved yet, regarding a non-linear fit to a set of experimental data. I tried to solve it in Matlab, which I handle a little bit.
I have a sensor, which has been designed to have 4 different filters in front of it. By making a sweep with the incident radiation, I collect the amount of transmitted light for every filter. So, I have a 1-D array for my increasing Flux, which is a 18x1 array; then, stored in a single matrix, I have a 18x4 output values for the four different filters. In addition I have a 1x4 array specifying the background sensed by the sensor for every filter (called V in my program).
The problem is that now I have an analytical function that I want to fit which is of the form:
y(1) = V(1) + ( b1 - V1 )( 1 - exp ( -( b4*x ) b3 ) ) b2
This is just the first part of the function, namely the function for the first filter, the other 3 (y2, y3, y4) have the same form but different value of the background (V) and different value of b4. Thus, I have 3 shared parameters that are equal for my 4-equations, and the other ones are filter-specific.
To conclude, I have also a 18x4 matrix relative to the weights of my data, in order to perform a weighted non-linear fit.
I would expect that the fit retrieves me an array with all the 7 parameters, together with their standard error or confidence interval.
Below, two different approaches
Code:
%%Simpler case with just 2 filter
Xdata1 = D;
Xdata2 = D;
Ydata1 = OD(:,1);
Ydata2 = OD(:,2);
%% Define fitting functions and parameters
mdl1 = @(beta,x) V(1,1)+(beta(1)-V(1,1)).*(1-exp(-(beta(4)*x).^beta(3))).^beta(2);
mdl2 = @(beta,x) V(1,2)+(beta(1)-V(1,2)).*(1-exp(-(beta(5)*x).^beta(3))).^beta(2);
%% Prepare input for NLINMULTIFIT and perform fitting
x_cell = {Xdata1, Xdata2};
y_cell = {Ydata1, Ydata2};
mdl_cell = {mdl1, mdl2};
beta0 = [10, 1, 1, 1E-4, 1E-4];
[beta,r,J,Sigma,mse,errorparam,robustw] = nlinmultifit(x_cell, y_cell, mdl_cell,
beta0);
Thanks in advance!
Code:
Dose = D;
Filter1 = OD(:,1);
Filter2 = OD(:,2);
Filter3 = OD(:,3);
Filter4 = OD(:,4);
f = @(x)nonlinmodel(x,Dose,Filter1,Filter2,Filter3,Filter4,V);
x0 = [10;1;1;1E-4;1E-4;1E-4;1E-4];
options = optimoptions(@lsqnonlin,'Algorithm','trust-region-reflective');
[K,resnorm,resid,exitflag,output,lambda,J] = lsqnonlin(f,x0,[],[],options);function y = nonlinmodel(x, Dose, Filter1, Filter2, Filter3, Filter4, V)
y = [Filter1 - V(1)+(x(1)-V(1)).*(1-exp(-(x(4)*Dose).^x(3))).^x(2); ...
Filter2 - V(2)+(x(1)-V(2)).*(1-exp(-(x(5)*Dose).^x(3))).^x(2); ...
Filter3 - V(3)+(x(1)-V(3)).*(1-exp(-(x(6)*Dose).^x(3))).^x(2); ...
Filter4 - V(4)+(x(1)-V(4)).*(1-exp(-(x(7)*Dose).^x(3))).^x(2)];
end