Improve fit quality for a custom function with near-perfect starting values
I’m trying to fit data with a relatively complicated custom function (combined logistic+linear function; combined effect of my data+noise). By trail-and-error I can manually find a good fit (indicated below), but I want MATLAB to do the final fine-tuning. Instead, the MATLAB fit is way worse than what I can do manually. As this takes minutes to do by hand and I will need to process hundreds of images, I really want to automate this.
This is the starting fit I provide manually:
<</matlabcentral/answers/uploaded_files/120202/fitInit.png>>
This is the fit after MATLAB is done (using the manual fit values as startpoint):
<</matlabcentral/answers/uploaded_files/120203/fitDone.png>>
I tried:
* Setting lower and upper bounds
* Parameter scaling (all coefficients between 1E-2 and 1E2)
* Changing algorithm
* Setting DiffMinChange and DiffMaxChange to small values
* Changing TolX and TolFun
*The main thing I don’t understand is why MATLAB worsens the fit.* Especially with low DiffMinChange and DiffMaxChange, I would expect MATLAB to give the starting fit or something better.
In case people want to try for themselves, below I provide the data and custom function.
The data-to-fit is in ‘data.txt’, while the function I use to fit it is:
I=@(Ac,b,AE,bg,bgx,x) AE.*(1./(1+1E15./Ac.*exp(-b.*x)))+bg+bgx.*x;.
The manual startpoint is
start=[1.75 0.065 14 16 0.015];
To help with interpretation of the function:
* It is a logistic function ‘1/(1+1E15./Ac*exp(-b*x))’,
* a scale-factor for the logistic function ‘AE’, (the 1E15 is to scale the parameter)
* a background offset ‘bg’, and
* a background slope ‘bgx’.I’m trying to fit data with a relatively complicated custom function (combined logistic+linear function; combined effect of my data+noise). By trail-and-error I can manually find a good fit (indicated below), but I want MATLAB to do the final fine-tuning. Instead, the MATLAB fit is way worse than what I can do manually. As this takes minutes to do by hand and I will need to process hundreds of images, I really want to automate this.
This is the starting fit I provide manually:
<</matlabcentral/answers/uploaded_files/120202/fitInit.png>>
This is the fit after MATLAB is done (using the manual fit values as startpoint):
<</matlabcentral/answers/uploaded_files/120203/fitDone.png>>
I tried:
* Setting lower and upper bounds
* Parameter scaling (all coefficients between 1E-2 and 1E2)
* Changing algorithm
* Setting DiffMinChange and DiffMaxChange to small values
* Changing TolX and TolFun
*The main thing I don’t understand is why MATLAB worsens the fit.* Especially with low DiffMinChange and DiffMaxChange, I would expect MATLAB to give the starting fit or something better.
In case people want to try for themselves, below I provide the data and custom function.
The data-to-fit is in ‘data.txt’, while the function I use to fit it is:
I=@(Ac,b,AE,bg,bgx,x) AE.*(1./(1+1E15./Ac.*exp(-b.*x)))+bg+bgx.*x;.
The manual startpoint is
start=[1.75 0.065 14 16 0.015];
To help with interpretation of the function:
* It is a logistic function ‘1/(1+1E15./Ac*exp(-b*x))’,
* a scale-factor for the logistic function ‘AE’, (the 1E15 is to scale the parameter)
* a background offset ‘bg’, and
* a background slope ‘bgx’. I’m trying to fit data with a relatively complicated custom function (combined logistic+linear function; combined effect of my data+noise). By trail-and-error I can manually find a good fit (indicated below), but I want MATLAB to do the final fine-tuning. Instead, the MATLAB fit is way worse than what I can do manually. As this takes minutes to do by hand and I will need to process hundreds of images, I really want to automate this.
This is the starting fit I provide manually:
<</matlabcentral/answers/uploaded_files/120202/fitInit.png>>
This is the fit after MATLAB is done (using the manual fit values as startpoint):
<</matlabcentral/answers/uploaded_files/120203/fitDone.png>>
I tried:
* Setting lower and upper bounds
* Parameter scaling (all coefficients between 1E-2 and 1E2)
* Changing algorithm
* Setting DiffMinChange and DiffMaxChange to small values
* Changing TolX and TolFun
*The main thing I don’t understand is why MATLAB worsens the fit.* Especially with low DiffMinChange and DiffMaxChange, I would expect MATLAB to give the starting fit or something better.
In case people want to try for themselves, below I provide the data and custom function.
The data-to-fit is in ‘data.txt’, while the function I use to fit it is:
I=@(Ac,b,AE,bg,bgx,x) AE.*(1./(1+1E15./Ac.*exp(-b.*x)))+bg+bgx.*x;.
The manual startpoint is
start=[1.75 0.065 14 16 0.015];
To help with interpretation of the function:
* It is a logistic function ‘1/(1+1E15./Ac*exp(-b*x))’,
* a scale-factor for the logistic function ‘AE’, (the 1E15 is to scale the parameter)
* a background offset ‘bg’, and
* a background slope ‘bgx’. curve fitting MATLAB Answers — New Questions