Using the fit() function to find an exponential gives me a straight line..?
Show older comments
I am trying to find the best fit equation using a set of data points, but when I use the fit() function and plot it, it gives me a straight line. I plotted the equation using the given a and b values on an online grapher, and it was a straight line as well. However, when I plot the original data it is not a straight line. I am not sure why the function is not working properly, so any help would be appreciated.

7 Comments
Adam Danz
on 1 Feb 2021
What fittype did you use?
Did you specify any options in fit()?
Zuzanna Fernandez
on 1 Feb 2021
Adam Danz
on 1 Feb 2021
And how did you plot the results (red line)?
the cyclist
on 1 Feb 2021
Can you upload the data and the code you used?
Adam Danz
on 1 Feb 2021
fitnlm doesn't do any better
mdl = @(b,x)b(1).*exp(b(2).*x);
f = fitnlm(tnew, v2new, mdl, [20,.001])
coef = f.Coefficients.Estimate;
x = linspace(min(tnew), max(tnew));
y = mdl(coef, x);
plot(tnew, v2new, 'b-')
hold on
plot(x,y,'r-')
axis equal

Well, hold on. It's not a straight line. It's an exponential decay curve defined by
f =
General model Exp1:
f(x) = a*exp(b*x)
Coefficients (with 95% confidence bounds):
a = 20.83 (20.72, 20.93)
b = -0.002372 (-0.00245, -0.002295)
In fact, you can fit a straight line to the curve and plot the residuals,
betas = polyfit(tnew, f(tnew), 1);
y = betas(1).*tnew + betas(2);
figure()
stem(tnew, f(tnew)-y)

And if you use a different set of x values where y increases more dramatically, you can see the decay curve much better,
plot(-1000:200, f(-1000:200))
axis equal

Accepted Answer
More Answers (0)
Categories
Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

