Ask for fitting data points by power function

3 views (last 30 days)
Khoa Tran
Khoa Tran on 23 Mar 2015
Answered: Khoa Tran on 24 Mar 2015
Hi every one, I have a small problem with data after experiment. I need to fit it with the power law , when I try with Fit options in Curve fitting tool of Matlab R2013a, I don't know how to Optimize the parameter sets such as StartPoint, Lower, Upper,... to obtain the best fitting curve. Can anybody show me how to do that? I tried to change the value of parameters in Dialog box of Fit options but the fitting curve doesn't seem to change at all :((. I have the data here.
*x = [ 5.45 6.2 10.15 10.9 11.65 15.55 16.3 17.05 20.83 21.58 22.33 26.16 26.91 27.67 31.8 32.6 33.3 37.15 37.9 38.6];
*y = [ 11.23 7.22 6.95 6.7 6.66 5.82 5.76 5.8 5.3 5.11 5.63 5.44 5.82 5.79 5.44 5.65 5.52 5.59 5.55 5.51 ]; Thank you very much. :D

Answers (2)

Alan Weiss
Alan Weiss on 23 Mar 2015
Edited: Alan Weiss on 23 Mar 2015
Your data as given doesn't lead to a very good fit. Here is what I found:
xdata = [ 5.45 6.2 10.15 10.9 11.65 15.55 16.3 17.05 20.83 21.58 22.33 26.16 26.91 27.67 31.8 32.6 33.3 37.15 37.9 38.6];
ydata = [ 11.23 7.22 6.95 6.7 6.66 5.82 5.76 5.8 5.3 5.11 5.63 5.44 5.82 5.79 5.44 5.65 5.52 5.59 5.55 5.51 ];
fun = @(x,xdata)x(1)*xdata.^x(2);
x0 = [2,-0.5];
[x,res] = lsqcurvefit(fun,x0,xdata,ydata)
Local minimum possible.
lsqcurvefit stopped because the final change in the sum of squares relative to
its initial value is less than the default value of the function tolerance.
<stopping criteria details>
x =
14.4106 -0.2930
res =
11.6140
However, plotting the curve showed that the first point looked like an outlier.
plot(xdata,ydata,'ko',xdata,fun(x,xdata),'b-')
Removing the first point gives a better fit.
xd2 = xdata(2:end);
yd2 = ydata(2:end);
[x2,res2] = lsqcurvefit(fun,x0,xd2,yd2)
Local minimum possible.
lsqcurvefit stopped because the final change in the sum of squares relative to
its initial value is less than the default value of the function tolerance.
<stopping criteria details>
x2 =
9.5469 -0.1618
res2 =
1.6879
figure;plot(xd2,yd2,'ko',xd2,fun(x2,xd2),'b-')
Not great, but better.
I did this using the Optimization Toolbox lsqcurvefit function, but the idea is the same no matter what you use to do the fit.
Alan Weiss
MATLAB mathematical toolbox documentation

Khoa Tran
Khoa Tran on 24 Mar 2015
Hi, Mr Alan Weiss. Thanks for your quick help, I review your code and realize that it's still not acceptable yet, I am so sorry to say that. Because I still want to find out somehow to make the fitting curve closer to the data point. Let me explain you briefly why I still have a headache with it.
  • 1, Initially, I fit this data with function of form y = a*x.^b + c*x.^d, and the fitting looks good.But such kind of function is not mentioned in any model and theory but y = a*x.^b. So I can only change it to the single term function y = a*x.^b.
  • 2, The data I gave you is the velocity vs time which is reduced from the data of position vs time.These two sets of data correlate each other by getting the 1st derivative of position in time, we will have the velocity.Additionally, the position vs time data here is officially described in book under Power law function y = a*x^.b
  • 3, Due to sudden change in the velocity at early time, it's rather tricky to use this Power law fitting for all of my data.So I think of to divide the data points in two subset. One set will contain the first 5 points, the second set will cover the rest. With this way, the second set will be easily fitted by such power law but not the first set. I am freshman with Matlab in compiling code for finding optimal set of parameters for the first set by power law fitting. So can you help me this?
  • 4, My point is to keep two axes to run from 0 as the graph is plotted, so please help me do that in your master-piece. One more problem is to control the meeting point of the two fitting curves which are for the two sets of data points. Since the experimental phenomenon is continous, that's why the fitting curves should be more or less smooth at the intersecting point.
  • 5, I heard from friends that we can change parameters in the Fit Options dialog box to control the fitting curve but you know nothing happens whatever I tried to control the values there :(( . Do you have any experience with Curve fitting Tool?
  • Thanks so much. :D

Community Treasure Hunt

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

Start Hunting!