How to do exponential curve fitting like y=a*exp(b*x)+c
37 views (last 30 days)
Show older comments
Hi guys,
I have a set of data x and y, which is given below: x=[10 12.5 15 17.5 20 22.5 25 27.5 30 32.5 35 37.5 40 42.5 45 47.5 50]; y=[62.1 77.3 92.5 104 112.9 121.9 125 129.4 134 138.2 142.3 143.2 144.6 147.2 147.8 149.1 150.9];
I'd like to to have a curve fitting like y=a*exp(b*x)+c. I tried to use cftool box (custom equation). However, it didn't work well. I am wandering if someone could help me with this.
Thanks
1 Comment
Arturo Gonzalez
on 1 Sep 2020
Edited: Arturo Gonzalez
on 1 Sep 2020
clear all;
clc;
% get data
dx = 0.02;
x = (dx:dx:1.5)';
y = -1 + 5*exp(0.5*x) + 4*exp(-3*x) + 2*exp(-2*x);
% calculate integrals
iy1 = cumtrapz(x, y);
iy2 = cumtrapz(x, iy1);
iy3 = cumtrapz(x, iy2);
% get exponentials lambdas
Y = [iy1, iy2, iy3, x.^3, x.^2, x, ones(size(x))];
A = pinv(Y)*y;
lambdas = eig([A(1), A(2), A(3); 1, 0, 0; 0, 1, 0]);
lambdas
%lambdas =
% -2.9991
% -1.9997
% 0.5000
% get exponentials multipliers
X = [ones(size(x)), exp(lambdas(1)*x), exp(lambdas(2)*x), exp(lambdas(3)*x)];
P = pinv(X)*y;
P
%P =
% -0.9996
% 4.0043
% 1.9955
% 4.9999
Accepted Answer
Star Strider
on 23 Feb 2018
Try this:
x=[10 12.5 15 17.5 20 22.5 25 27.5 30 32.5 35 37.5 40 42.5 45 47.5 50];
y=[62.1 77.3 92.5 104 112.9 121.9 125 129.4 134 138.2 142.3 143.2 144.6 147.2 147.8 149.1 150.9];
f = @(b,x) b(1).*exp(b(2).*x)+b(3); % Objective Function
B = fminsearch(@(b) norm(y - f(b,x)), [-200; -1; 100]) % Estimate Parameters
figure
plot(x, y, 'pg')
hold on
plot(x, f(B,x), '-r')
hold off
grid
xlabel('x')
ylabel('f(x)')
text(27, 105, sprintf('f(x) = %.1f\\cdote^{%.3f\\cdotx}%+.1f', B))

14 Comments
Kate Leary
on 9 Nov 2022
Thank you thank you! Is there a way to calculate prediction bounds as well? I know of predint but can't figure out how to use is without having a cfit or sfit object. Again, thank you for your help!
Star Strider
on 9 Nov 2022
My pleasure!
They’re actually plotted (as ‘95% CI’, just so narrow that it’s difficult to see them here. the predict function call in my code calculates them:
[yv,yci] = predict(mdl,xv(:));
and they are then plotted in:
hp{3} = plot(xv, yci, ':r', 'DisplayName', '95% CI');
If you have any further questions, please feel free to follow up here.
.
More Answers (0)
See Also
Categories
Find more on Linear Least Squares in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!