fitting a dual exponential function using nlinfit

8 ビュー (過去 30 日間)
Angelavtc
Angelavtc 2020 年 1 月 13 日
コメント済み: the cyclist 2020 年 1 月 15 日
I am trying to use nlinfit to find the function that best fits the following set of points:
According to some litterature review this curve could be best described by a dual exponential model defined as:
where a, b, c, d an h are parameters that need to be calibrated using nlinfit.
However, when I use nlinfit my calibration goes terrible and I when I plot the approximation I get something like this:
when what I want is to get something like this:
Is there a way to know which could be the ideal initial conditions?
This is the code that I use to fit the function:
where y axis=Price and x axis=x
%Defining the function to fit
f = @(c,x) (c(5)+(exp((x-c(1))./c(2))-exp(-(x-c(3))./c(4)))./2);
%Setting a guess to start the fit
a=max(x)/10;
initials=[a a a a a];
%Making the curve fit
new_coeffs=nlinfit(x,Price,f,initials)
%Evaluate the new coefficients into the function
new_y=f(new_coeffs,x);
%Plot the approximation
figure(2)
plot(x,Price,...
x,new_y)
Any suggestion of how to get me closer to what I want are really welcome!
Thanks in advance!
  3 件のコメント
Angelavtc
Angelavtc 2020 年 1 月 13 日
Thanks for your answer, as you can see I uploaded my code. Yes, the lines going back and forth are due to a not monotonically increasing movement. But how can I change this to see what the fit curve really looks like?
the cyclist
the cyclist 2020 年 1 月 14 日
編集済み: the cyclist 2020 年 1 月 14 日
Uploading the data as well would be helpful.
Please take my advice about plotting S(t) for different parameters, and using that to guide your initial guess at parameters (your variable initials). Those guesses are probably terrible. Are you getting convergence warnings from nlinfit?
To plot the fit, don't use the original data. Just create values of x that are uniformly spaced. For example
new_x = 1.e4 * (1:0.05:4);
new_y=f(new_coeffs,x);
and then plot the data as points, and the fit as a line:
plot(x,Price,'o',...
x,new_y,'-')

サインインしてコメントする。

採用された回答

the cyclist
the cyclist 2020 年 1 月 14 日
編集済み: the cyclist 2020 年 1 月 14 日
I made up some pretend data that looks a little bit like yours, and did your fit. Here is my code:
% Make up some pretend data
rng default
N = 1000;
xL = 1e4 * (2.1 + 1.5*rand(N,1));
PriceL = 0 + 3000*rand(N,1);
xR = 1e4 * (1.2 + 1.1*rand(N,1));
PriceR = 0 - 3000*rand(N,1);
x = [xL;xR];
Price = [PriceL; PriceR];
%Defining the function to fit
f = @(c,x) c(5) + (exp((x-c(1))./c(2)) - exp(-(x-c(3))./c(4)))./2;
%Setting a guess to start the fit
a=max(x)/10;
initials=[a a a a a];
%Making the curve fit
new_coeffs=nlinfit(x,Price,f,initials);
% Define x values for plotting the fit
fit_x = 1.e4 * (1:0.05:3.6);
% What the initial coefficients produce
init_y = f(initials,fit_x);
% %Evaluate the new coefficients into the function
new_y=f(new_coeffs,fit_x);
% %Plot the approximation
figure
% plot(x,Price,...
% x,new_y)
plot(x,Price,'o',...
fit_x,new_y,'-',...
fit_x,init_y)
legend({'data','new coef','initial coef'},'Location','NorthWest')
Here is what the output looks like:
nlinfit does give a warning about the fit:
==========================================
Warning: Some columns of the Jacobian are effectively zero at the solution, indicating that the model is insensitive to some of its parameters. That may be because those parameters
are not present in the model, or otherwise do not affect the predicted values. It may also be due to numerical underflow in the model function, which can sometimes be avoided by
choosing better initial parameter values, or by rescaling or recentering. Parameter estimates may be unreliable.
==========================================
If I plot the output from the initial coefficients against the random x (instead of monotonically increasing x, as I suggested), it looks like this:
So, it definitely looks like your attempt at fitting is somehow not getting anything much different from your initial (poor) guess. I'm not sure why.
You might want to try my code on your original data.
  2 件のコメント
Angelavtc
Angelavtc 2020 年 1 月 15 日
Your answer was really helpful, you are right about plotting S(t) for different parameters, I think the problem starts there but still dont know how to fix it. According to the paper that I am trying to do when the initial values are: 0.694, 0.063,1.144, 0.145 and 53.658 I should get the following graph, but when I try to do it, it does not appear, therefore something should be wrong:
See just the dual-exponential shape and ignore the data points...
Captura de pantalla 2020-01-15 a la(s) 3.53.30 p. m..png
any other ideas? Thanks a lot @thecyclist
the cyclist
the cyclist 2020 年 1 月 15 日
This code
guess = [0.694, 0.063,1.144, 0.145, 53.658];
x_test = 0.4 : 0.05 : 1;
f = @(c,x) c(5) + (exp((x-c(1))./c(2)) - exp(-(x-c(3))./c(4)))./2;
figure
plot(x_test,f(guess,x_test))
produces this plot for me:
Screen Shot 2020-01-15 at 10.48.18 AM.png
which looks as expected.
But the x values for your actual fit are 4 orders of magnitude larger than the example plot. So, you'll need to re-scale either your data or the coefficients.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with Curve Fitting Toolbox についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by