How to do curve fitting by a user defined function

86 views (last 30 days)
Hi all,
I am trying to use the process of curve fitting via a user defined function/code I wrote ( using MATLAB) to extroplate values from an experimental data shown Beolw ( in bold). Any assistance would be greatly appreciated.
My User defined function
parametersused for my code:
Kb=1.38e-23;Ps=0.89;Wb=1.2765104e+7;Vo=1.00e-13;V=1.00e-26;sigma=0.05;mu=0;
t=logspace(log10(0.00000226),log10(2),50);
D=@(V,t)(1./(V.*sigma.*sqrt(2.*pi)).*exp(-log(V)-mu).^2/(2.*sigma^2)).*(2*Ps.*(1-exp(-(t)./(Vo*exp((Wb-Ps*11300000)*V/(Kb*80))))));
>> s= arrayfun(@(t) integral(@(V) D(V, t)/(2e+55*Ps), 0.85e-26, 1.15e-26), t);
>> plot(log(t),s2)
Above code wouold generate a curve, which I intend to curve fit with the data from experiment below....
Data from experiment
>> x1=[-6.7 -6.5 -6.3 -6.1 -5.9 -5.7 -5.5 -5.3 -5.1 -5 -4.7 -4.5 -4.3 -4 -3.7 -3.5 -3.3 -3.1];
>> y1=[0.06 0.09 0.1 0.12 0.14 0.18 0.22 0.28 0.34 0.38 0.5 0.58 0.62 0.68 0.72 0.73 0.75 0.77];
>> plot(x1,y1)

Accepted Answer

John D'Errico
John D'Errico on 7 Jun 2021
Edited: John D'Errico on 7 Jun 2021
How far out do you want to extrapolate? This curve does NOT look like a sigmoid, because a sigmoidal curve will go flat at each end. And this seems to be still increasing and decreasing at each end. At both ends, the curve seems to be almost linear.
And that suggests an approach that uses my SLM toolbox might be right.
x1=[-6.7 -6.5 -6.3 -6.1 -5.9 -5.7 -5.5 -5.3 -5.1 -5 -4.7 -4.5 -4.3 -4 -3.7 -3.5 -3.3 -3.1];
y1=[0.06 0.09 0.1 0.12 0.14 0.18 0.22 0.28 0.34 0.38 0.5 0.58 0.62 0.68 0.72 0.73 0.75 0.77];
slm = slmengine(x1,y1,'plot','on','endconditions','nat','extrap','linear')
hold on
x2 = linspace(-9,-7);
x3 = linspace(-3,0);
set(gca,'Xlim',[-9,0])
plot(x2,slmeval(x2,slm),'b--',x3,slmeval(x3,slm),'b--')
I used the natural end conditions to force the curve to have zero second derivative at each end. As you can see, the extrapolant is linear at each end, which seems perfectly consistent with your data.
You can find SLM on the file exchange for (free) download here:
Now, CAN you fit this with a variation of a sigmoid? Yes. Just add a linear term to the function. Thus...
mdl = fittype('a + b*x + c/(1 + d*exp(-k*x))','indep','x')
mdl =
General model:
mdl(a,b,c,d,k,x) = a + b*x + c/(1 + d*exp(-k*x))
Note my use of a far simpler sigmoidal shape than what you used. It seems entirely adequate.
fittedmdl = fit(x1',y1',mdl,'start',[.5,.05,.1,.1,1])
fittedmdl =
General model:
fittedmdl(x) = a + b*x + c/(1 + d*exp(-k*x))
Coefficients (with 95% confidence bounds):
a = 0.5608 (0.4026, 0.7191)
b = 0.07411 (0.05003, 0.0982)
c = 0.4395 (0.3587, 0.5203)
d = 3.455e-07 (-4.28e-07, 1.119e-06)
k = 3.023 (2.58, 3.467)
plot(fittedmdl)
hold on
plot(x1,y1,'ro')
And that fit also seems acceptable to my eyes.
In the first case, the slope of the extrapolated linear spline will be:
slmeval(x1([1,end]),slm,1)
ans =
0.082837 0.077077
So 0.082 on the left end, and 0.077 on the right. I did not enforce that the slopes must be the same at each end in the spline.
We can see from the nonlinear fit, the slope of the linear asymptotes will be 0.074, so reasonably close to the spline estimates, especially when you look at the confidence limits on b from the fit.
  1 Comment
Alex Sha
Alex Sha on 29 Sep 2022
For the model suggested by John, there will be one more solution:
Sum Squared Error (SSE): 0.000370209893736447
Root of Mean Square Error (RMSE): 0.00453510929512085
Correlation Coef. (R): 0.999843991752112
R-Square: 0.999688007842798
Parameter Best Estimate
--------- -------------
a 1.00442867121814
b 0.075404659346166
c -0.434981823753697
d 3460097.03129248
k -3.0597040040656

Sign in to comment.

More Answers (1)

Mudaga Andrew Nomuoja
Mudaga Andrew Nomuoja on 8 Jun 2021
Thank you so much for your swift reply. I will attempt to use your approach to formulate a model for my research work. I am immensely grateful.
Regards
Andrew

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!