3-term exponential fit
조회 수: 35 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
Star Strider
2014년 4월 6일
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
Star Strider
2014년 4월 13일
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.
추가 답변 (2개)
John D'Errico
2014년 4월 13일
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
John D'Errico
2014년 4월 13일
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.
Arturo Gonzalez
2020년 9월 8일
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--');
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!