how to find the two constants that suits the best non linear fitting
10 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
LE FOU
el 8 de Oct. de 2014
Comentada: Star Strider
el 9 de Oct. de 2014
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
0 comentarios
Respuesta aceptada
Star Strider
el 8 de Oct. de 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 comentarios
Más respuestas (2)
Stephen23
el 8 de Oct. de 2014
Editada: Stephen23
el 8 de Oct. de 2014
3 comentarios
Stephen23
el 8 de Oct. de 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:
Matt J
el 9 de Oct. de 2014
Editada: Matt J
el 9 de Oct. de 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 comentarios
Matt J
el 9 de Oct. de 2014
Editada: Matt J
el 9 de Oct. de 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);
Ver también
Categorías
Más información sobre Descriptive Statistics 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!