how to find the two constants that suits the best non linear fitting
11 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Star Strider
on 8 Oct 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 Comments
More Answers (2)
Matt J
on 9 Oct 2014
Edited: Matt J
on 9 Oct 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 Comments
Matt J
on 9 Oct 2014
Edited: Matt J
on 9 Oct 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);
See Also
Categories
Find more on Descriptive Statistics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!