MATLAB Answers

3-term exponential fit

92 views (last 30 days)
Hi, I want to fit a 3-term exponential function i.e.
y = a*exp(-b*x) + c*exp(-d*x) + e*exp(-f*x)
to get coefficients b,d,f.
Matlab's curvefitting toolbox is great for 2 term fitting, but that is it's limit. Tried log fits, polyfit fit but had no luck. Any ideas?
Thanks, Will

Accepted Answer

Star Strider
Star Strider on 6 Apr 2014
I don’t have the Curve Fitting Toolbox (Optimization and Statistics instead). This uses fminsearch:
y = @(b,x) b(1).*exp(-b(2).*x) + b(3).*exp(-b(4).*x) + b(5).*exp(-b(6).*x);
p = [3; 5; 7; 11; 13; 17]*1E-1; % Create data
x = linspace(1, 10);
yx = y(p,x) + 0.1*(rand(size(x))-0.5);
OLS = @(b) sum((y(b,x) - yx).^2); % Ordinary Least Squares cost function
opts = optimset('MaxFunEvals',50000, 'MaxIter',10000);
B = fminsearch(OLS, rand(6,1), opts); % Use ‘fminsearch’ to minimise the ‘OLS’ function
fcnfit = y(B,x); % Calculate function with estimated parameters
figure(1)
plot(x, yx, '*b')
hold on
plot(x, fcnfit, '-r')
hold off
grid
  3 Comments
Star Strider
Star Strider on 13 Apr 2014
The best model and fit will actually require you to go back to the differential equations that model the process you’re fitting, integrate them (analytically if possible), and fit that solution. Considering your data demonstrate an exponential-periodic behaviour, chances are that the parameters are not actually independent and in all likelihood are functionally related. I would not consider the oscillations ‘noise’ unless you know they are an artifact of your instrumentation or some such.

Sign in to comment.

More Answers (2)

John D'Errico
John D'Errico on 13 Apr 2014
Note that polyfit is meaningless in this context. You can't fit an exponential with a polynomial tool!
Next, fitting sums of exponentials is one form of an ill-posed problem. When you have too many terms in that sum (and 3 terms is starting to be a fair amount) then the problem starts to get nasty. Think of it like this, we are trying to form a linear combination of terms that all look pretty much alike! Estimation of the coefficients will generate singular (or nearly singular) matrices. If the rate parameters of several of the terms are too close, the problem will become difficult to solve using double precision arithmetic.
  1 Comment
John D'Errico
John D'Errico on 13 Apr 2014
I'll try to add an example later when I have a chance. I'll also show how the fit process can be made a bit easier using a partitioned least squares.

Sign in to comment.


Arturo Gonzalez
Arturo Gonzalez on 8 Sep 2020
Per this answer, you can do it with the following matlab code
clear all;
clc;
% get data
dx = 0.001;
x = (dx:dx:1.5)';
y = -1 + 5*exp(0.5*x) + 4*exp(-3*x) + 2*exp(-2*x);
% calculate n integrals of y and n-1 powers of x
n = 3;
iy = zeros(length(x), n);
xp = zeros(length(x), n+1);
iy(:,1) = cumtrapz(x, y);
xp(:,1) = x;
for ii=2:1:n
iy(:, ii) = cumtrapz(x, iy(:, ii-1));
xp(:, ii) = xp(:, ii-1) .* x;
end
xp(:, n+1) = ones(size(x));
% get exponentials lambdas
Y = [iy, xp];
A = pinv(Y)*y;
Ahat = [A(1:n)'; [eye(n-1), zeros(n-1, 1)]];
lambdas = eig(Ahat);
lambdas
% get exponentials multipliers
X = [ones(size(x)), exp(lambdas'.*x)];
P = pinv(X)*y;
P
% show estimate
y_est = X*P;
figure();
plot(x, y); hold on;
plot(x, y_est, 'r--');

Community Treasure Hunt

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

Start Hunting!

Translated by