how to find the two constants that suits the best non linear fitting
조회 수: 9 (최근 30일)
이전 댓글 표시
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
채택된 답변
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
추가 답변 (2개)
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
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 Center 및 File Exchange에서 Descriptive Statistics에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!