Suggestions for a non-linear fit model in Matlab

In summary, the forum user has a problem with non-linear fitting of experimental data. They have tried different approaches, such as using a custom function and different fitting algorithms, but have not been successful. The function they are trying to fit has seven parameters, three of which are shared among four different filters. It is important to have a good initial guess for these parameters and choose the right fitting function and algorithm. Outliers in the data should also be identified and removed before fitting. A good understanding of the data and the underlying physical processes is also crucial.
  • #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
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);
that I tried but that didn't work for my purposes.

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
 
Physics news on Phys.org
  • #2

Thank you for sharing your problem with us. Non-linear fitting can be a challenging task, but with the right approach and tools, it can be solved. From your forum post, it seems like you have a good understanding of the problem and have tried different approaches to solve it. I would like to offer some suggestions that may help you in solving this problem.

Firstly, it is important to understand the function that you are trying to fit and its parameters. In your case, you have a function with seven parameters, three of which are shared among all four filters. It is essential to have a good initial guess for these parameters before starting the fitting process. From your code, it seems like you have already defined the initial guess (beta0) for the shared parameters, which is a good start.

Next, it is important to choose the right fitting function for your data. In your case, the non-linear function you are using seems appropriate, but it is always good to explore different functions and see which one gives the best fit. You can also try using a custom function that takes into account the shared parameters and the different filters.

Another important aspect of non-linear fitting is the choice of fitting algorithm. Different algorithms may work better for different types of data. In your code, you have used the "trust-region-reflective" algorithm, which is a good choice for non-linear fitting problems. However, you can also try other algorithms and see if they give better results.

One thing to keep in mind while fitting non-linear functions is the presence of outliers in the data. These outliers can significantly affect the fitting results and should be removed before fitting. You can use different techniques to identify and remove outliers, such as visual inspection, statistical methods, or using robust fitting algorithms.

Lastly, it is always good to have a good understanding of the data and the physical processes involved in your experiment. This will help you in choosing appropriate models and fitting parameters, as well as in interpreting the results.

I hope these suggestions will help you in solving your non-linear fitting problem. Keep exploring different approaches and algorithms, and I am sure you will find a solution. Good luck!
 

What is a non-linear fit model in Matlab?

A non-linear fit model in Matlab is a mathematical model that is used to describe the relationship between two or more variables that are not linearly related. It is used to analyze data that does not follow a straight line trend and is often used in fields such as biology, economics, and engineering.

How do I choose the best non-linear fit model in Matlab?

Choosing the best non-linear fit model in Matlab depends on the type of data you have and the relationship between the variables. One way to choose the best model is by visually inspecting the data and selecting a model that fits the data well. Another approach is to use statistical tests such as the Akaike information criterion (AIC) or the Bayesian information criterion (BIC) to compare the goodness of fit of different models.

Can I customize my non-linear fit model in Matlab?

Yes, you can customize your non-linear fit model in Matlab by adjusting the parameters and constraints of the model. Matlab offers a variety of built-in non-linear models, but you can also create your own custom models by specifying the functional form and parameters.

What are some common challenges when using non-linear fit models in Matlab?

One common challenge when using non-linear fit models in Matlab is overfitting, which occurs when the model fits the training data too closely and does not generalize well to new data. Other challenges include choosing the appropriate model and dealing with missing or noisy data. It is important to carefully select and evaluate the model to ensure reliable results.

Are there any resources available to help with using non-linear fit models in Matlab?

Yes, there are many resources available to help with using non-linear fit models in Matlab. The Matlab documentation provides detailed information on available functions and how to use them. There are also online tutorials, forums, and communities where you can ask for help and learn from others' experiences. Additionally, there are books and courses available that cover non-linear modeling in Matlab.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
997
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
126
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
873
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
Back
Top