Below is an example of finding a fit with only one term of exponential term but I dont know how to find the fit of the curve when it has 2 degree of exponential term, i.e.[y = a*e^(bx) + c*e^(dx)]
example for y = a*e^(bx)
phi =[ones(size(xx)),xx];
aa=phi\log(yy);
yfit = exp(phi*aa);
plot(xx, yy, ro, xx, yfit, k-) ;
s=sprintf(y=%8fexp(%8fx)’,exp(aa(1)),aa(2));
exp.jpg

 Réponse acceptée

Star Strider
Star Strider le 15 Nov 2018
Modifié(e) : Star Strider le 15 Nov 2018
Try this:
filename1 = 'x2.mat';
D1 = load(filename1);
x = D1.x2;
filename2 = 'y2.mat';
D2 = load(filename2);
y = D2.y2;
fcn = @(b,x) b(1).*exp(b(2).*x) + b(3).*exp(b(4).*x);
[B,fval] = fminsearch(@(b) norm(y - fcn(b,x)), ones(4,1));
figure
plot(x, y, 'p')
hold on
plot(x, fcn(B,x), '-')
hold off
grid
The fitted parameters are:
B =
1.13024777245481
-2.75090020576997
-2.09865110252493
-5.48051415288241
How can I fit an exponential curve - 2018 11 14.png
EDIT — Added plot figure.

9 commentaires

fengsen huang
fengsen huang le 15 Nov 2018
Hi,
the code doesnt run, i copy and pasted exactly.
sorry i am new to matlab, please be patient with me :)
Image Analyst
Image Analyst le 15 Nov 2018
Did you see any error messages in the command window? Or absolutely nothing at all happened (impossible I think)?
Star Strider
Star Strider le 15 Nov 2018
@fengsen huang — That it ‘doesn’t run’ does not give me any useful information. My code ran successfully for me, and yielded acceptable parameter estimates and a good fit to your data. (I am running R2018b, however this code should work for the past several releases, and perhaps for all releases.)
What was the problem?
Did the load not work?
Did my code throw an error? If so, please copy and paste all the red text from your Command Window so I can understand the error.
The fminsearch function is part of core MATLAB, so I know everyone has it.
Image Analyst
Image Analyst le 15 Nov 2018
I get the plot, but when it hits fminsearch(), it says:
Exiting: Maximum number of function evaluations has been exceeded
- increase MaxFunEvals option.
Current function value: 0.854365
The fit does seem reasonable, though perhaps not as good for higher x as in my answer which used fitnlm().
I got the same message. I chose to ignore it.
Changing the fminsearch call to:
[B,fval] = fminsearch(@(b) norm(y - fcn(b,x)), [1; -1; -1; -1])
eliminates the message, and gives a slightly better (lower) residual norm, with:
B =
0.0123997313717212
6.04789376996529
-1.14123029526501
-12.6780841992101
fengsen huang
fengsen huang le 15 Nov 2018
Hi,
it worked now but is it possible to use the method I listed to find a fit.
Also how can I sprintf the equation out on the grapgh so I can see all the coefficient with all the exp.
it worked now but is it possible to use the method I listed to find a fit.’
Unfortunately, no. This is the sum of two nonlinear exponential functions, so even a transformed linear function will not be an appropriate model.
Also how can I sprintf the equation out on the grapgh so I can see all the coefficient with all the exp.’
To print it:
eqn = sprintf('y = %.3f * exp(%.3f * x) %+.3f * exp(%.3f * x)', B) % Printed
to display it on the plot:
figure
plot(x, y, 'p')
hold on
plot(x, fcn(B,x), '-', 'LineWidth',1.5)
hold off
grid
text(0.22, -0.42, sprintf('y = %.3f\\cdote^{%.3f\\cdotx} %+.3f\\cdote^{%.3f\\cdotx}', B)) % Displayed On Plot
You can position it anywhere you want.
fengsen huang
fengsen huang le 15 Nov 2018
Modifié(e) : madhan ravi le 15 Nov 2018
fcn = @(b,x) b(1).*exp(b(2).*x) + b(3).*exp(b(4).*x);
[B,fval] = fminsearch(@(b) norm(y - fcn(b,x)), ones(4,1))
Hi, can you kindly explain what those 2 lines mean
also how do you know this fit is the most appropriate i.e R^2 value or something?
Thank you so much appreciate it
Star Strider
Star Strider le 15 Nov 2018
Modifié(e) : Star Strider le 15 Nov 2018
This line:
fcn = @(b,x) b(1).*exp(b(2).*x) + b(3).*exp(b(4).*x);
is the objective function, the expression that describes the function to fit to the data.
This line:
[B,fval] = fminsearch(@(b) norm(y - fcn(b,x)), ones(4,1))
calls the fminsearch function to fit the function to the data. The norm function compares the function output to the data and returns a single scalar value (the square root of the sum of squares of the difference between the function evaluation and the data here), that fminsearch uses. I refer you to the documentation on fminsearch (link) for details on how it works.
The value would be calculated as:
Rsq = 1 - sum((y - fcn(B,x)).^2) / sum((y - mean(y)).^2)
returning:
Rsq =
0.980214434988184
We are not comparing models, so this is the only statistic available. There are several ways to compare models, a subject much more involved than I will go into here. See any good text on nonlinear parameter estimation for details.
@fengsen huang —
If my Answer helped you solve your problem, please Accept it!

Connectez-vous pour commenter.

Plus de réponses (2)

Image Analyst
Image Analyst le 15 Nov 2018
Here's another way using fitnlm(). I get
coefficients =
0.0124001386786833
6.04782212479857
-1.14123018111715
-12.6780685424329
formulaString =
'Y = 0.012 * exp(6.048 * X) + -1.141 * exp(-12.678 * X)'
Quite a bit different than Star's numbers.
0000 Screenshot.png
Arturo Gonzalez
Arturo Gonzalez le 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