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

조회 수: 9 (최근 30일)
LE FOU
LE FOU 2014년 10월 8일
댓글: Star Strider 2014년 10월 9일
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

채택된 답변

Star Strider
Star Strider 2014년 10월 8일
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
LE FOU
LE FOU 2014년 10월 9일
ok thanks a lot it is solved

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Stephen23
Stephen23 2014년 10월 8일
편집: Stephen23 2014년 10월 8일
You should have a look at the examples too.
  댓글 수: 3
LE FOU
LE FOU 2014년 10월 8일
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 2014년 10월 8일
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
Matt J 2014년 10월 9일
편집: Matt J 2014년 10월 9일
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
LE FOU
LE FOU 2014년 10월 9일
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 2014년 10월 9일
편집: Matt J 2014년 10월 9일
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);

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by