# Quadratic/Exponential Best Fit Curve

17 views (last 30 days)
Hamish Brown on 16 Nov 2021
Answered: John D'Errico on 16 Nov 2021
i'd like to add a quadratic or exponential best fit curve to my scatter graph. I cant figure out how to do it. I've tried the following but it doesn't give the type of curve i'd like. I also have the curve-fitting package installed.
y = [0.1 0.2 0.3 1.3];
x = [180 598 2259 8853];
plot(x,y,'x')
p = polyfit(x,y,7)
f = polyval(p,x)
hold on
plot(x,f,'--r')
hold off

John D'Errico on 16 Nov 2021
First, you have 4 data points. Trying to fit a degree 7 (SEVEN) polynomial to that is just silly. Yes, you say quadratic. But your words don't fit what you did.
Putting multiple fits on one plot is easy though.
y = [0.1 0.2 0.3 1.3];
x = [180 598 2259 8853];
plot(x,y,'x') Lets be completely serious. You want to use those 4 data points to try to fit anything? These 4 points do not have the shape of a quadratic polynomial. They lack the shape of an exponential function. And that means what you get out will be complete garbage for a model.
You could arguably get just as valid a fit from a straight line through the data.
mdlpoly1 = fit(x',y','poly1');
mdlpoly2 = fit(x',y','poly2');
You don't say WHICH exponential model you want to use. And with only 4 data points, hoping to do any such fit is problematic for an exponential model.
expft = fittype('a*exp(b*x)','indep','x');
mdlexp = fit(x',y',expft,'start',[1 0.0001])
mdlexp =
General model: mdlexp(x) = a*exp(b*x) Coefficients (with 95% confidence bounds): a = 0.1555 (0.04168, 0.2693) b = 0.00024 (0.0001543, 0.0003257)
Anyway, now we can plot the results.
plot(mdlpoly1,'m')
hold on
plot(mdlpoly2,'b')
plot(mdlexp,'g')
plot(x,y,'ro') I have little confidence about which of those curves fits the data better. They are jerked around by the influence of the first and third data points, but which of those two points is noisy? Or is it the second point that is garbage?

R2021b

### Community Treasure Hunt

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

Start Hunting!