How to force a exponential fit to go through a preestablish point (x,y)?

37 vues (au cours des 30 derniers jours)
Hi, I have some data X and Y and I want to fit it to an exponential function but giving it an initial point (Xo,Yo):
f = fit(x,y,'exp1','StartPoint',[0 2])
Is there any way to do it? I think I've tried everything I could.

Réponse acceptée

Torsten
Torsten le 17 Oct 2018
x0 = ...; % x coordinate of preestablished point
y0 = ...; % y coordinate of preestablished point
g = @(p,x)y0*exp(-p*(x-x0));
f = fit(x,y,g)
plot(f,x,y)

Plus de réponses (1)

John D'Errico
John D'Errico le 17 Oct 2018
'StartPoint' refers to the initial parameter guess for the model, since this is an optimization problem. Not a point the curve must pass through.
The exp1 model refers to this model:
fittype('exp1')
ans =
General model Exp1:
ans(a,b,x) = a*exp(b*x)
Now, you want that curve to pass through the point
y0 = a*exp(b*x0)
So you can reduce the model by one parameter, using the above information. That is,
a = y0/exp(b*x0)
Therefore, your model is now:
y = yo/exp(b*x0) * exp(b*x)
We can rewrite it in a slightly less complicated form:
y = yo*exp(b*(x - x0))
See that there is n0 a term out front. The model is just as I wrote it, with only b as an unknown, and x0 and y0 as knowns. So we could use that model, now with x0 and y0 fixed, in custom model. Easy enough to do.
ft = fittype('y=y0*exp(b*(x-x0))','independent','x','dependent','y',...
'problem',{'x0','y0'},'coefficients','b');
x0 = 0;
y0 = 2;
mdl = fit(x,y,ft,'problem',[x0,y0]);
Best is is you provide some intelligent value for b as a starting guess, thus use 'StartPoint' in fit.
Since you did not provide any data, I'll make some up.
n = 10;
x = rand(n,1);
y = 2*exp(x) + randn(n,1)/100;
plot(x,y,'o')
grid on
<</matlabcentral/answers/uploaded_files/136822/untitled.jpg>>
We know the curve should pass though the point (0,2).
ft = fittype('y0*exp(b*(x-x0))','independent','x',...
'problem',{'x0','y0'},'coefficients','b');
x0 = 0;
y0 = 2;
mdl = fit(x,y,ft,'problem',{x0,y0},'StartPoint',0.5);
mdl
mdl =
General model:
mdl(x) = y0*exp(b*(x-x0))
Coefficients (with 95% confidence bounds):
b = 1.002 (0.9999, 1.005)
Problem parameters:
x0 = 0
y0 = 2
I guessed the initial value for b as 0.5. In general, always provide intelligent starting values for an optimization.

Catégories

En savoir plus sur Linear and Nonlinear Regression dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by