8

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?

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I've missed that tiny detail!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.