how to fit exponantial function matlab ?

hi everyone i have this exponantial function and i want fit it so i can get the A,B and C values
can you please help me with this. thanks for advance

2 comentarios

Walter Roberson
Walter Roberson el 22 de Abr. de 2017
Is that "+ C" inside the summation or outside it? Is it part of the exponent of the exp() ?
best16 programmer
best16 programmer el 22 de Abr. de 2017
no it is not part of the exponent

Iniciar sesión para comentar.

Respuestas (2)

Walter Roberson
Walter Roberson el 22 de Abr. de 2017

0 votos

Fitting a sum of exponentials is difficult to do well.
I suggest you start with cftool, request a custom equation, and enter your model. Then click on "Fit Options" and enter whatever constraint information you have available.
The underlying routine for this will be lsqnonlin()
lsqnonlin() does not necessarily find the best fit inside the given bounds: instead it goes for 95% confidence intervals. Once you have an estimate of the parameters, you might be able to use fmincon or fminsearch to refine the estimate.

1 comentario

best16 programmer
best16 programmer el 22 de Abr. de 2017
thank you for the answer,let's say i did fit this function, how can i get all the values of B in one column?

Iniciar sesión para comentar.

John D'Errico
John D'Errico el 22 de Abr. de 2017
Editada: John D'Errico el 22 de Abr. de 2017

0 votos

In theory, this is a problem solvable using the curvefitting toolbox, or lsqnonlin (optimization TB), lsqcurvefit(optimization TB), nlinfit(stats TB),etc. So it depends on what toolboxes you have available, although each of those tools has a somewhat similar calling structure. You could also use fminsearch to do the optimization if you have no more than about two terms in the sum, since fminsearch tends to be poor for large problems in general.
In practice, sums of exponentials problems quickly become impossible for more than a couple of exponential terms. You need to have great data, with very low noise. Lots of data is good too, as long as it is low noise. Even then, you will ABSOLUTELY need good starting estimates for the parameters.
The problem is this quickly becomes highly ill-posed. Any noise at all will blow it out of the water, yielding garbage for results. If you think you can solve this with n=5 or so, you are just wasting your time.

22 comentarios

best16 programmer
best16 programmer el 22 de Abr. de 2017
thank you for the answer here's the model that i defined for this function :
fun = @(const,n) (const(1)*exp(-n./(const(2)))) + const(3);
x = lsqcurvefit(fun,[1,1,1],n,y)
n=[-25;-20;-15;-10;-5;0;10;15;20;25;30];
y=10^(-3)*[ 0.1400;0.2200;0.3000;0.3400;0.4000;0.4000;0.4400;0.4600;0.4800;0.4800;0.5200;0.5200 ]
I need to plot(V,-1/const(2)).how can i do this
What is V ?
You have 11 entries for n but 12 entries for y. Is that a mistake?
Could you confirm that your independent variable is n, and your dependent variable is y, and you are trying to find the a, b, c in
y = a*exp(-n./b) + c
? That is, a single sum of exponentials?
If so then because you have negative n, if you have positive b, those negative n terms contribute so much as to overwhelm the rest, leading to a useless fit. If you have a negative b, then the positive n terms contribute so much as to overwhelm the rest, leading to a useless fit.
You can get a meaningful fit if you change the equation to
n = a*exp(-y./b) + c
best16 programmer
best16 programmer el 23 de Abr. de 2017
thank you very much,i meaned by V the n column in x axis.there is a mistake in entries they should be 12 entries in n and y.how can i add the sum to the fit and obtain the values of b?
best16 programmer
best16 programmer el 23 de Abr. de 2017
what about y(x)=a0+a1*exp(-x/b1)+a2*exp(-x/b2)+...+an*exp(-x/bn)
In your duplicate post you indicated you are using something of the form
y(x)=a0+a1*exp(-x/b1)+a2*exp(-x/b2)+...+an*exp(-x/bn)
As John indicates, "In theory, this is a problem solvable using the curvefitting toolbox, or lsqnonlin (optimization TB), lsqcurvefit(optimization TB), nlinfit(stats TB)"
Which of those toolboxes do you have available?
How many exp() terms are you adding? Remember that if you have N exp() terms then because you have the a0 term to calculate as well, you would need at least N+1 y values, each associated with a different value of the independent variable x .
best16 programmer
best16 programmer el 23 de Abr. de 2017
i have lsqcurvefit and the nlinfit,i want 9 terms of exponentials how can i do this in matlab?
thanks for advance
John D'Errico
John D'Errico el 23 de Abr. de 2017
Editada: John D'Errico el 23 de Abr. de 2017
A big reason I did not show how to do the calculation, since it depends on what toolbox will be used.
That thee should be 12 "entries" in n I assume means that there are 12 exponential terms, plus a constant. I will claim flat out that if that is true, then it will NEVER be possible to do the estimation. The result will be meaningless.
You need to understand here that you are asking to compute 25 unknowns, done in double precision arithmetic, using real world data. Do I need to really prove to you the impossibility of such a task? Give me some time, as it will require some code to be written and create an example. (About an hour to write the code and explain what I am showing.)
best16 programmer
best16 programmer el 23 de Abr. de 2017
Editada: best16 programmer el 23 de Abr. de 2017
what i really need is that Bi values(b1,b2,b3,.........) is there any way to do a for loop so i can get those b values and use only one exponential term?
okey take your time and thank you
John D'Errico
John D'Errico el 23 de Abr. de 2017
Editada: John D'Errico el 23 de Abr. de 2017
It does not matter what you want. You cannot compute what you want. No computer in the world can do the computation you want to do. And I am in the process of writing some code to explain why not. But as I said, it will take a bit of time.
No. There is simply NO way that you can use a for loop. That is completely impossible.
Walter Roberson
Walter Roberson el 23 de Abr. de 2017
It looks to me as if there are 19 unknowns: a0 to an, and b1 to bn, where n = 9.
Walter Roberson
Walter Roberson el 23 de Abr. de 2017
best16: Earlier you posted 12 y values. Are there additional y values available? You cannot fit 19 coefficients to only 12 data points.
best16 programmer
best16 programmer el 23 de Abr. de 2017
Editada: Walter Roberson el 23 de Abr. de 2017
here's the code that i'm using :
n=[-25;-20;-15;-10;-5;0;5;10;15;20;25;30]
m=10^(-3)*[ 0.1400;0.2200;0.3000;0.3400;0.4000;0.4000;0.4400;0.4600;0.4800;0.4800;0.5200;0.5200]
fh = @(n,p) p(1) + p(2)*exp(-n./p(3));
errfh = @(p,x,y) sum((y(:)-fh(x(:),p)).^2)
p0 = [mean(m) (max(m)-min(m)) (max(n) - min(n))/2];
P = fminsearch(errfh,p0,[],n,m)
%%%Write the function
fprintf('Best fit: x = %g + %g exp(-t/%g) nn',P(1),P(2),P(3))
%%%Plot the result
plot(n,m,'bo',n,fh(n,P),'r-')
xlabel('n')
ylabel('m')
legend('data','fit')
John D'Errico
John D'Errico el 23 de Abr. de 2017
Editada: John D'Errico el 23 de Abr. de 2017
I just did the computations, that showed for beyond THREE exponential terms, the condition number of the Jacobian is greater than 1e17. For 12 terms, IF I choose to redo the computation symbolically, the condition number will be something on the rough order of 10^55.
Do you have MORE than 55 digits of precision in your data? If not, then you cannot even try to do the computation.
Worse, trying to use fminsearch on something like this is literally impossible.
That appears to be attempting to fit something of the form
a0 + a1 * exp(-x/b1)
With your data, the best fit is effectively a1 = 0, b1 = anything non zero, a0 = mean(m) .
John D'Errico
John D'Errico el 23 de Abr. de 2017
It looks like you are trying to estimate those separate terms in a loop? Again, this is not possible. Argh. Please just slow down. Read what I am saying.
best16 programmer
best16 programmer el 23 de Abr. de 2017
Editada: best16 programmer el 23 de Abr. de 2017
so what do you propose,is there other solution,cause in some software such as clampfit it uses only one term exponential p(1) + p(2)*exp(-n./p(3)); and it gets p(3) values i don't know how it can do it
best16 programmer
best16 programmer el 23 de Abr. de 2017
what about Varpro algorithm??
Walter Roberson
Walter Roberson el 23 de Abr. de 2017
Editada: Walter Roberson el 23 de Abr. de 2017
The model
a0 + a1*exp(-x/b1) + a2*exp(-x/b2)
has minima with values in the range
a0 = -29.4822302448910136 to -27.6351763004644368
a1 = -7.28594569417768304e-17 to 2.01541109754776393
b1 = -0.000153296198318632869 to -8.3610908715198867e-06
a2 = -7.61657101908586556e-17 to 1.99470091538253769
b2 = -0.000153111238342277058 to -8.33054180630094524e-06
At the moment the single best point is
-28.3695471679136517, 1.69495180258791223, -0.000146245661010571616, -4.8115133148789194e-26, -8.73388922639887038e-06
I am continuing to search for improvements.
best16 programmer
best16 programmer el 23 de Abr. de 2017
is it possible to obtain these values for each element of y,or only for the whole y column?
You appear to be asking to fit a model with N data points and 2*N+1 coefficients. There is no way to obtain a meaningful fit for more than N coefficients if you have N data points. For example with 12 data points in your y, you would not be able to fit more than 12 coefficients, which would imply a model
a0 + a1*exp(-x/b1) + a2*exp(-x/b2) + a3*exp(-x/b3) + a4*exp(-x/b4) + a5*exp(-x/b5)
That would give you 11 coefficients to fit with 12 data points. Notice there would only be floor((12-1)/2) = 5 "b" values, not one b value for each y.
best16 programmer
best16 programmer el 23 de Abr. de 2017
i will upload this picture so i can tell you what to get
this software can use this function and get the Tau for the wanted signal.
My tests for the form a0 + a1 * exp(-x/b1) suggest
a0 = 0.000549262354656014
a1 = -0.000133005477037469
b1 = 22.3758394031684
You can get these values with cftool and a custom equation, but you need to apply some boundary conditions, such as 0..10, -2..10, -10..50

Iniciar sesión para comentar.

Categorías

Más información sobre Get Started with Curve Fitting Toolbox en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 22 de Abr. de 2017

Comentada:

el 24 de Abr. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by