Least squares fit of user defined function to data

3 views (last 30 days)
Hi! I have the following problem, I have a set of data, two columns. And I define a function using "fittype", which contains 4 free parameters which I want to determine. I then use the "fit" function to fit this function to my data. When I run the program it does so without any errors, however when plotting the function it creates it is completely different than my data. Does anyone know what I am doing wrong?
en=[8.3, 8.4, 8.7, 8.9, 9.2, 9.5, 10.1, 10.7, 11.4, 12.1, 12.8, 13.5];
cs=[2.4, 6.5, 14.2, 18.2, 26.7, 33.5, 47.8, 79.5, 118.4, 173.9, 199.8, 193.1];
xh=transpose(en);
y=transpose(cs);
t=fittype('a*((x-8.1)/8.1)^b+c*((x-8.1)/8.1)^d','coefficients', {'a','b','c','d'},'indep','x');
tilp=fit(xh,y,t,'StartPoint', [0.9, 0.5, 0,1.5]);
plot(xh,y,'o');
xt=8:0.00100:15;
plot(xh,tilp(xh),'x');

Answers (2)

Matt J
Matt J on 21 Jun 2013
Edited: Matt J on 21 Jun 2013
I don't know if this explains the failure of your fit, but your model does look like it contains insufficient information. How can "a" be distinguished from "c" or "b" distinguished from "d"? They play completely symmetric and interchangeable roles in the model.
  3 Comments
Matt J
Matt J on 21 Jun 2013
Edited: Matt J on 21 Jun 2013
Well, you understand, I assume, that the fitting algorithm is running some kind of iterative function minimization algorithm that uses the derivatives of your curve fit error.
So, just as an example, if at some iteration, the algorithm lands on a set of parameters the form
a=c, b=d
then the derivatives with respect to (a,b) will be the same as the derivatives with respect to (c,d). Therefore, (a,b) will get modified in that iteration identically to (c,d) so that another point of the form
a=c, b=d
is reached. This would continue iteratively never allowing the fitting algorithm to explore combinations of parameters of any other form. So, when it stops, you get something that doesn't fit your model very well.
Matt J
Matt J on 21 Jun 2013
The first therm is suppose to go as ~^0.5 so that it will follow the first data points, while the second term should produce numbers for c and d that correspond to a steeper function that follows the higher datapoints, before it goes to zero.
Additionally, how is the algorithm supposed to know that the contribution of the (a,b) term gets priority when fitting the lower data while the (c,d) term gets priority for the higher data? Also, how does the algorithm know that the curve is supposed to eventually go to zero. Your data are montonically increasing.

Sign in to comment.


Matt J
Matt J on 21 Jun 2013
Edited: Matt J on 21 Jun 2013
I have used this FEX file
to perform my own fit of your data and I don't see terrible disagreement with your data. It's not a perfect fit, obviously.
xh=en.'; y=cs.';
N=length(xh);
z=(xh-8.1)/8.1;
funlist={@(bd,z) z.^bd(1), @(bd,z) z.^bd(2)};
[bd,ac]=fminspleas(funlist,[0.5,1.5],z,y);
a=ac(1); c=ac(2);
b=bd(1); d=bd(2);
yh=a*z.^b + c*z.^d;
plot(xh,y,xh,yh); legend('Samples','Fit')
One thing to note, however, is that it finds a solution of the form b=d, so that the model really simplifies down to something of the form
y=p*((x-8.1)/8.1).^q

Community Treasure Hunt

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

Start Hunting!