MATLAB Curve fitting with custom equation

Hello,
I have a data (x,y) points and I want to fit it with custom equation as y = a*(1-exp(-b*x))+c*(exp(d*x)-1). I want to get a, b, c, d values after fitting with custom equation.The problem I am facing is that, the curve fitting happens to be good in the initial part of the data points, then at the later points of data, it does not fit properly. How to tweak with fitoptions so that I can fit my data exactly with custom equation. Also, if there needs any change to be done in the script please help me with that too, so that I can fit it exactly. If you can provide me snaps of the script that you are following to fit kindly share that too as it will help me to understand properly.
I am attaching the data file for your reference.
Thank you

14 Comments

J. Alex Lee
J. Alex Lee on 29 Oct 2020
Edited: J. Alex Lee on 29 Oct 2020
  1. You can't ever fit your data "exactly" (in principle), unless you only have as many data points as you have free parameters, or if your data was generated from the model. And if you did, some types of simple optimizers may fail. You should look for a rootfinding algorithm if you have as many data points as parameters.
  2. It may be that your model is not appropriate, so you will never be able to match the "shape". Do you know indepedently that your proposed model should work (your data is very clean...was it generated from a model?)
  3. I'm not sure if this will help, but can you combine the offsets a-c into a 5th parameter f:
Thank you Alex for your reply.
Actually the data was generated from an experiment. Yes I do know the proposed model should fit the data. I will try your suggested model equation to fit the data.
Thank you
Hi, Onkar, your data and equation are fit good enough as below:
Root of Mean Square Error (RMSE): 0.000583470511926158
Sum of Squared Residual: 0.000230476416520552
Correlation Coef. (R): 0.999845492636139
R-Square: 0.999691009144803
Adjusted R-Square: 0.999690092257993
Parameter Best Estimate
---------- -------------
a -0.00124853917372643
b -0.0803208242522166
c 1.51360739455312E-14
d 0.506966190396915
Dear Alex Sha,
Can you tell me how did you guessed the initial guess? I have to fit few more data, so can you tell me how to go about guessing.
Thank you
Hi, Onkar, the result is obtained by a software named 1stOpt, guessing of initial start-values is not required anymore by end-users, maybe because this software emploies an unique global optimization algorithm. There is also a Global Optimization toolbox in Matlab, theoretically, global optimization algorithms are not dependent on initial start-values, however, the results from Global Optimization toolbox in Matlab have still a considerable gap compared with 1stOpt.
Hello Alex Sha,
Thank you for your response.
It seems 1stOpt software needs license for the access,my university doesnt have the access for that. However, I will check with Matlab Global Optimization toolbox as you suggested.
Your help is really appreciated. Thank you Alex.
You are welcome. Actually, there is one more solution (the two sets of results seem to be symmetrical):
Root of Mean Square Error (RMSE): 0.000583470511925808
Sum of Squared Residual: 0.000230476416520275
Correlation Coef. (R): 0.999845492596033
R-Square: 0.999691009064604
Adjusted R-Square: 0.999690092177556
Parameter Best Estimate
---------- -------------
a -1.51369467033431E-14
b -0.50696517676424
c 0.00124854020427691
d 0.0803208040636604
That's great, I will try using second solution set too. In future if I need help, I hope you will help me to figure it out. In case for communication can I get your email id, that will really keep communication transparent. My email id: khadke.onkar4@gmail.com
Thank you
Dear Alex Sha,
I have some more data. I was able to fit for few of the data, but for some set of data, I am not able to fit. Will you help me in fitting the data using 1stOpt software ? If yes then I will share the data with you. It would be a great help for me. I hope you would help me with this.
Thank you
Hi, postout your data and fitting equation here, I will try for you
Dear Alex Sha,
Thank you for your response.
I am attaching three excel sheets containing data. I was unable to fit it properly using MATLAB global optimization tool. I hope 1stOpt would solve the fitting.
The equation with which I want to fit the data is as follows: y = a*(1 - exp(-b*x)) + c*(exp(d*x) - 1). I am interested to get a, b, c, d values after fitting the data with equation mentioned.
I am really grateful to you for the help.
Thank you
Sincerely
Onkar Khadke
Hi, Onkar, the amount of data for 180_Nimon is very large, take too much time, so I evenly extract one-thousandth of data points, you can take the results from 1stOpt as the initial start-values and do again in Matlab for all your data.
180_Nimon:
Root of Mean Square Error (RMSE): 0.000767578707146424
Sum of Squared Residual: 0.0639068586093132
Correlation Coef. (R): 0.997544863719544
R-Square: 0.995095755133243
Adjusted R-Square: 0.995095664703245
Parameter Best Estimate
---------- -------------
a 0.216233837855244
b -0.0036792514520028
c 0.216554776487748
d 0.00367854558388458
300_Nimon (All data):
Root of Mean Square Error (RMSE): 0.00125060834729753
Sum of Squared Residual: 0.00395697373297554
Correlation Coef. (R): 0.998488831980626
R-Square: 0.996979947590035
Parameter Best Estimate
---------- -------------
a 0.0563668259802289
b -0.0117549539832182
c 0.0129130829009419
d 0.0549829731742771
350_Nimon(All data):
Root of Mean Square Error (RMSE): 0.00161465848055046
Sum of Squared Residual: 0.00219519673142098
Correlation Coef. (R): 0.999142995467777
R-Square: 0.998286725392322
Parameter Best Estimate
---------- -------------
a -0.0261576770771583
b -0.143454016006819
c 0.0425407232155395
d -0.114127486555251
Dear Alex Sha,
Thank you for your response. I am grateful that you helped me in getting the coefficients from the fit.
Surely, I can use the fitted coefficients by you as a starting guess to again try fitting in Matlab. I appreciate your devoted time given to help me in fitting the data.
Thank you
Sincerely
Onkar Khadke
Pleasure, you are welcome!

Sign in to comment.

 Accepted Answer

If you have the curve fitting toolbox, I suggest using fittype and fit.
fittype will allow you to define a custom equation to fit to your data, and fit will try to fit that equation to your data. Just a caution, your model is much more complex than your data requires. This means you will need to provide reasonable estimates for your initial guesses (StartPoint) for coefficients a-d.
You can find a good example here.
data=readtable("675_base.xlsx");
ft = fittype('a*(1-exp(-b*x))+c*(exp(d*x)-1)',...
'dependent',{'y'},'independent',{'x'},...
'coefficients',{'a','b','c','d'});
f = fit(data.x,data.y,ft,'StartPoint',[0,0,0,0.25])
f =
General model: f(x) = a*(1-exp(-b*x))+c*(exp(d*x)-1) Coefficients (with 95% confidence bounds): a = 0 b = 0 c = 0.0006586 (0.0006251, 0.0006921) d = 0.0946 (0.09362, 0.09558)
plot(f,data.x,data.y)

5 Comments

this fit looks worse than that of Alex Sha...but your comment about overfitting looks like it is demonstrated by Alex Sha's fitted value of c~0.
I never said it was good. My goal was not to solve the problem, only provide direction. I'll let the OP worry about getting good results.
Thank you Cris LaPierre for your inputs. I will work on your suggested points.
@Cris LaPierre What method does fit use internally to fit equation with a data ?
You could use the following syntax to determine what algorithm has been used to fit the data (output.algorithm).
You can then search how that algorithm performs fitting. In the example above, it is using the trust-region-reflective algorithm. You can use the fitOptions function to set desired algorithm options.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2020a

Community Treasure Hunt

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

Start Hunting!