2

I had a question concerning the results of a polynomial equation while using python/numpy. I have defined a function using a polynomial having a rather small leading coefficient. The following is my code and the equation:

import matplotlib.pyplot as plt
import numpy as np 

def myfunction(X):
    return 9.06043895*10**(-9)*X**6-1.67073053*10**(-6)*X**5 + \
          7.49688511*10**(-5)*X**4 + 7.97984114*10**(-4)*X**3 - \
          6.07927087*10**(-2)*X**2 - 0.415861627*X + 80.62563

Input = np.arange(0, 100, 1)
Output = myfunction(Input)

plt.plot(Input, Output)

This yields the following plot:

enter image description here

The error seems to be in the way that the resultant Y is calculated as the array itself is off. (The values match the plot, but are in correct)

Sample of the exact same equation plotted using WolframAlpha: (literal copy/paste from above minus the \'s and returns)

enter image description here

Can anyone help shed some light on this problem?

I have a feeling there may be some truncations possibly while handling the function, but I feel that this equation is not that bad...

Thank you all for your time and hopefully assistance.

4

1 回答 1

4

这是一个intfloat问题。你得到整数溢出:

>>> np.int32(100)**6
-727379968
>>> np.float32(100)**6
1000000000000.0

因为np.arange(0, 100, 1)给你整数。

你可以随心所欲地让它们漂浮——我通常使用np.arange(0, 100, 1.0),但老实说我更喜欢np.linspace

使用花车,我得到:

修正曲线

于 2014-04-02T19:10:08.610 回答