MATLAB Answers

# MATLAB Curve fitting with custom equation

108 views (last 30 days)
Onkar Khadke on 29 Oct 2020 at 6:49
Commented: Alex Sha on 11 Nov 2020 at 13:31
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

Alex Sha on 10 Nov 2020 at 2:55
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
Onkar Khadke on 11 Nov 2020 at 5:13
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
Alex Sha on 11 Nov 2020 at 13:31
Pleasure, you are welcome!

Sign in to comment.

### Accepted Answer

Cris LaPierre on 29 Oct 2020 at 12:46
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)

#### 3 Comments

J. Alex Lee on 29 Oct 2020 at 13:12
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.
Cris LaPierre on 29 Oct 2020 at 13:27
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.
Onkar Khadke on 29 Oct 2020 at 14:14
Thank you Cris LaPierre for your inputs. I will work on your suggested points.

Sign in to comment.

R2020a

### Community Treasure Hunt

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

Start Hunting!

Translated by