Sebastian Kramer
Sebastian Kramer

Reputation: 719

Numpy and matlab polyfit results differences

I'm getting different results when calling numpy.polyfit and matlab polyfit functions on exemplary set of data:

Python3.2:

(Pdb) a_array = [1, 2, 4, 6, 8,7, 9]
(Pdb) numpy.polyfit( range (len (a_array)), a_array, 1)
array([ 1.35714286,  1.21428571])

Matlab:

a_array = [1, 2, 4, 6, 8,7, 9]
polyfit(1:1:length(a_array), a_array, 1)

ans =
    1.3571   -0.1429

This is obviously not a numerical error.

I assume that the default value of some special option (like ddof in std function) differs between Python and matlab but I can't find it. Or maybe I should use another version of Python's polyfit?

How can I get the same polyfit results in both, Python Numpy and Matlab?

Upvotes: 8

Views: 4368

Answers (1)

M4rtini
M4rtini

Reputation: 13539

This gives the same result.

In [10]: np.polyfit(range(1, len(a_array)+1), a_array, 1)
Out[10]: array([ 1.35714286, -0.14285714])

range(...) starts from zero if you don't give it a start argument, and the end point is not included.

1:1:length(a_array) this in Matlab should give you 1 to the length of a_array with both ends included. If I remember Matlab correctly)

The difference in the constant of the interpolated line was simply because of difference in the start value in the x-axis.

Upvotes: 6

Related Questions