Azzera filtri
Azzera filtri

how to find the two constants that suits the best non linear fitting

1 visualizzazione (ultimi 30 giorni)
Dear All,
I have a series of three different parameters (y, x & t, please see the excel file attached)), that have to fit the following model :
y= a*sqrt(x)*exp(b*t).
My question is the following, how do I find the best values for the a and b, by the best I mean that corresponds to the best fitting for the points. Is there a function in Matlab that does this automatically like for example the polyfit that gives you a, b and c (for a second degree polynomial, a*x^2 +b*x+c), if not then please can someone help me ?
Thanking you in advance,
Le Fou

Risposta accettata

Star Strider
Star Strider il 8 Ott 2014
Fitting two variables with either lsqcurvefit or nlinfit with two independent variables requires that you combine the independent variables into one variable and then address them separately in your objective function.
This provides a decent fit:
D = matfile('matlab_1.mat');
x = D.x;
t = D.t;
y = D.y;
xt = [x t];
fxt = @(b,xt) b(1).*sqrt(xt(:,1)).*exp(b(2).*xt(:,2));
x0 = [0.01; 0.01];
B = lsqcurvefit(fxt, x0, xt, y);
ye = fxt(B,xt);
figure(1)
plot3(x, t, y, '.b')
hold on
plot3(x, t, ye, '.r')
hold off
grid on
xlabel('X')
ylabel('T')
zlabel('Y')
legend('Data', 'Regression Fit', 'Location', 'NE')
It is necessary to combine x and t into one matrix, xt here. The function you are fitting (I called it ‘fxt’) then uses x=xt(:,1) and t=xt(:,2). After that, everything is relatively routine. I got a reasonably good fit with the ‘x0’ values I chose, producing a=0.53 and b=0.0013 with respect to the variables in your original equation.
  4 Commenti

Accedi per commentare.

Più risposte (2)

Stephen23
Stephen23 il 8 Ott 2014
Modificato: Stephen23 il 8 Ott 2014
You should have a look at the examples too.
  3 Commenti
LE FOU
LE FOU il 8 Ott 2014
I need to use the following model because it is the one that fits best my data, but what I don't know is how to find the best values of a and b, that will allow me to get the best fit, please see the attachment, I already have the values of y, x and t:
y= a*sqrt(x)*exp(b*t)
Thanks
Stephen23
Stephen23 il 8 Ott 2014
Please read the examples that are in the link I gave. MATLAB documentation gives complete, working examples, which you can copy and try yourself.
If the link did not work, have a look at this example:

Accedi per commentare.


Matt J
Matt J il 9 Ott 2014
Modificato: Matt J il 9 Ott 2014
Your model is loglinear
log(y)= A + .5*log(x) + b*t
where A=log(a). It might be enough to solve these linear equations, if your errors aren't too large,
[logX,T]=ndgrid(log(x)/2,t);
rhs=log(y(:)) - logX(:);
lhs=reshape( [ones(size(T)), T] , [],2);
p=lhs\rhs;
A=p(1);
a=exp(A);
b=p(2);
The above assumes that your y-data is indexed y(x,t).
If nothing else, it should provide a systematic initial guess for the solvers the others have recommended. You could also use fminspleas ( Download ) which can take advantage of the fact that y is linear in a.
fun=@(b,xd) reshape( sqrt(x(:))*exp(b*t(:).') ,[],1);
[b,a]=fminspleas({fun},p(2),[],y(:));
  2 Commenti
LE FOU
LE FOU il 9 Ott 2014
Hi Matt J
my data is not indexed y(x,t), so the solution above does not work.
what about the second one, when I am trying to apply it it is telling me that p is not defined, please check the error below
Undefined function 'p' for input arguments of type 'double'.
thanks,
Matt J
Matt J il 9 Ott 2014
Modificato: Matt J il 9 Ott 2014
my data is not indexed y(x,t),
So, it is indexed y(t,x)? If so, it's just a 1-line modification,
[T,logX]=ndgrid(t,log(x)/2);
what about the second one, when I am trying to apply it it is telling me that p is not defined
The "second one" is not separate from the first. p was computed using the loglinear model in the first code segment I gave you. If you already have a good intial guess for b, though, you can give that instead. Additionally, if y is indexed y(t,x), then you must modify fun,
fun=@(b,xd) reshape( exp(b*t(:))*sqrt(x(:).') ,[],1);

Accedi per commentare.

Categorie

Scopri di più su Descriptive Statistics in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by