# Polynomial Fitting with Least Squares using Numpy and Scipy

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!

• python4mpia.github.io/fitting_data/least-squares-fitting.html – T-800 Jun 9 '14 at 23:49
• Can you post some code? – Max Hutchinson Jun 10 '14 at 3:29
• 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. – OscarB Jun 10 '14 at 10:58
• @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. – T-800 Jun 10 '14 at 14:21
• @MaxHutchinson, thanks for your help but I have just solved the issue by subtracting the 'offset' values. – T-800 Jun 10 '14 at 14:22