fitting a dual exponential function using nlinfit
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Angelavtc
el 13 de En. de 2020
Comentada: the cyclist
el 15 de En. de 2020
I am trying to use nlinfit to find the function that best fits the following set of points:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/261419/image.jpeg)
According to some litterature review this curve could be best described by a dual exponential model defined as:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/261420/image.png)
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/261421/image.jpeg)
when what I want is to get something like this:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/261422/image.png)
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 comentarios
the cyclist
el 14 de En. de 2020
Editada: the cyclist
el 14 de En. de 2020
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,'-')
Respuesta aceptada
the cyclist
el 14 de En. de 2020
Editada: the cyclist
el 14 de En. de 2020
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/261606/image.png)
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/261607/image.png)
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 comentarios
the cyclist
el 15 de En. de 2020
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](https://www.mathworks.com/matlabcentral/answers/uploaded_files/261921/Screen%20Shot%202020-01-15%20at%2010.48.18%20AM.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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Get Started with Curve Fitting Toolbox en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!