Hi,

I have data in the form:

rpm Torque
1000 36
2000 40
3000 45
4000 50
.
.

I need to find the torque at a specific rpm.

I found numpy.interp() which will give me a linear interpolation, but my tutor said it wouldn't be accurate enough.

I found numpy.polyfit() which would be sufficient, but I can't seem to get a specific output e.g. for rpm = 12, torque = 37.

I can only find:

``````x = np.array([0.0, 1.0, 2.0, 3.0,  4.0,  5.0])
y = np.array([0.0, 0.8, 0.9, 0.1, -0.8, -1.0])
z = np.polyfit(x, y, 3)
print z``````

output: array([ 0.08703704, -0.81349206, 1.69312169, -0.03968254])

which I don't understand.
(from http://docs.scipy.org/doc/numpy/reference/generated/numpy.polyfit.html?highlight=interpolation)

Either polynomial fit or cubic spline would be great.
Can you guys help?

The only problem I see is that your np arrays don't match your description of the problem. The following works for me

``````import numpy as np
data = """
1000 36
2000 40
3000 45
4000 50
""".strip().splitlines()
data = zip(*(map(float, item.split()) for item in data))
x, y = (np.array(z) for z in data)
print x
print y
z = np.polyfit(x, y, 3)
print z
print np.polyval(z, 1200.0)

""" my output -->
[ 1000.  2000.  3000.  4000.]
[ 36.  40.  45.  50.]
# this is a, b, c, d for polynomial a x^3 + b x^2 + c x + d
[ -1.66666667e-10   1.50000000e-06   6.66666667e-04   3.40000000e+01]
# evaluation at rpm = 1200
36.672
"""``````

Or may be I missed something ?

Thanks for the reply, I'll give this a try when I'm home :D.

Yeah sorry, I got the example from the Python website and hadn't changed the values.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.