## fitting a parabola giving unreasonable answer

Asked by Michael Phillips

### Michael Phillips (view profile)

on 11 Apr 2019
Latest activity Answered by Alex Sha

### Alex Sha (view profile)

on 28 Apr 2019
Accepted Answer by Clay Swackhamer

### Clay Swackhamer (view profile)

Hello,
I'm trying to fit a parabola to 4 data points using the following equation:
y = a.*exp(((-4.*pi.*b.*6.022e23)./(8.314.*1623)).*(((c./2.*(c-x).^2) - (1/3.*(c-x).^3))));
I'm getting an unreasonable result, which looks like this:
I think the equation is correct because I copy and pasted it from a function that employs it to create this graph, which models the same points:

Michael Phillips

### Michael Phillips (view profile)

on 11 Apr 2019
I used the curve fitting tool, but here's a little script that does the same thing:
D = [0.027322404;1.798850575;1.33;0.11];
e = [0.003670419;0.000531607;0.0500;0.18];
r = [0.89;1.12;1.26;1.42].*1e-10;
b = [2;-1.15e21;1.2e-10];
modelfun = @(b, r) b(1).*exp(b(2)./(((b(3)./2.*(b(3)-r).^2) - (1/3.*(b(3)-r).^3))));
opts = statset('nlinfit');
opts.RobustWgtFun = 'bisquare';
lsp = nlinfit(r, D, modelfun,b, opts);
and my R-square is -0.09018.
Michael Phillips

### Michael Phillips (view profile)

on 11 Apr 2019
The 4 data points:
r = [0.89;1.12;1.26;1.42].*1e-10;
D = [0.027322404;1.798850575;1.33;0.11];
r is x and D is y.
John D'Errico

### John D'Errico (view profile)

on 12 Apr 2019
But your model is not a parabola. It is a nasty to compute exponential thing. (Nasty in double precision arithmetic.)
Seems confusing. I'd suggest your problem is the huge dynamic range of the parameters. That gets the solver in trouble.
b = [2;-1.15e21;1.2e-10];

R2018b

### Clay Swackhamer (view profile)

Answer by Clay Swackhamer

### Clay Swackhamer (view profile)

on 12 Apr 2019

Two things: I changed your independent values (r) to something that is not so small. Second, I made your equation more simple. I tried it with your original values but it didn't work for me. Hopefully this gets you off to a good start.
%Data
r = [0.89;1.12;1.26;1.42];
D = [0.027322404;1.798850575;1.33;0.11];
%Set up the fit
ft = fittype('a*r^2+b*r+c', 'independent', 'r');
opts = fitoptions('Method', 'NonlinearLeastSquares');
opts.Display = 'Off';
opts.StartPoint = [0.2, 0.2, 0.3];
%Conduct the fit
[fitresult, gof] = fit(r, D, ft, opts);
%Evaluate the function for plotting
a = fitresult.a;
b = fitresult.b;
c = fitresult.c;
r_model = linspace(min(r), max(r), 100); %create 100 points to evaluate the model on
D_model = a*r_model.^2+b*r_model+c;
%Make plots
plot(r, D, 'bo', 'markerSize', 6) %plot the data
hold on
plot(r_model, D_model, 'LineWidth', 2, 'Color', 'r') %plot the model
leg = legend('Data', 'Model');
leg.FontSize = 14;

Michael Phillips

### Michael Phillips (view profile)

on 12 Apr 2019
Hey thanks for this. I suppose it's not critical that I fit my particular equation to the data, but rather that I get a fit from which I can extract the necessary parameters (like the coordinates of the parabola vertex). Thanks!
Clay Swackhamer

### Clay Swackhamer (view profile)

on 12 Apr 2019
No problem. If this was helpful would you mind accepting the answer? Thanks
Michael Phillips

on 12 Apr 2019
yep no problem!

### Alex Sha (view profile)

Answer by Alex Sha

### Alex Sha (view profile)

on 28 Apr 2019

Note the part "b.*6.022e23" in the fitting function y = a.*exp(((-4.*pi.*b.*6.022e23)./(8.314.*1623)).*(((c./2.*(c-x).^2) - (1/3.*(c-x).^3))));
It means the value of parameter "b" may be very samll, so it's better to give a very samll initial start value of "b". The follows are code of 1stOpt
Parameter a,b=1E-25,c;
Function y = a*exp(((-4*pi*b*6.022e+23)/(8.314*1623))*(((c/2*(c-x)^2) - (1/3*(c-x)^3))));
Data;
x = [0.89,1.12,1.26,1.42];
y = [0.027322404,1.798850575,1.33,0.11];
Results:
Root of Mean Square Error (RMSE): 0.0344822464690547
Sum of Squared Residual: 0.00475610128621053
Correlation Coef. (R): 0.999029624841959
R-Square: 0.998060191311866
Parameter Best Estimate
---------- -------------
a 2.05245278276956
b 1.58789820232727E-19
c 1.17159717145173