I am trying to fit data to a polynomial using Python - Numpy. The points, with lines sketched above them are as in the picture.

I am trying to fit those points to a polynomial of 4. or 5. degree. However, all I can get is nothing more than a line. The coefficients of other than linear terms are too small. I tried to do that both with Numpy and Scipy. In Numpy, I have used the polyfit function and followed the example given in this link and in Scipy used the optimization.curve_fit function and followed the page in the comment below. (I cannot post more than two links!).

My question is: why do I get a linear fit for such data? I provide high enough data (about 70 points). And how can I improve my fit?

Any suggestions are highly appreciated!

  • $\begingroup$ python4mpia.github.io/fitting_data/least-squares-fitting.html $\endgroup$ – T-800 Jun 9 '14 at 23:49
  • $\begingroup$ Can you post some code? $\endgroup$ – Max Hutchinson Jun 10 '14 at 3:29
  • 1
    $\begingroup$ From the picture you show, it would appear as if you have quite high $x$ and $y$ values. Consider scaling, i.e. dividing $x$ and $y$ with, say, 1e6, then apply your polynomial fit to those scaled values, and multiply the output points (not the coefficients of the polynomial) with 1e6. $\endgroup$ – OscarB Jun 10 '14 at 10:58
  • $\begingroup$ @OscarB, thanks for your comment. I used the curvefit Toolbox of Matlab and I got similar results. Then I have subtracted the minimum value for both x and y values and then I could get really nice fits both in MATLAB and Python. $\endgroup$ – T-800 Jun 10 '14 at 14:21
  • $\begingroup$ @MaxHutchinson, thanks for your help but I have just solved the issue by subtracting the 'offset' values. $\endgroup$ – T-800 Jun 10 '14 at 14:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.