0

这是我的代码:

import math    
x=float(( input ('x ? ' )))  
n  = 1000   #a big number 
b=0      
for i in range (n):    
   a=(((((-1)**i))*(x**((2*i)+1)))/(math.factorial((2*i)+1)))   
   b+=a     
print (b)

但它不起作用并显示此错误:

"OverflowError: long int too large to convert to float"
4

2 回答 2

2

这仅用于绘图:

import numpy as np
vmysin = np.vectorize(mysin, excluded=['order'])

x = np.linspace(-80, 80, 500)
y2 = vmysin(x, 2)
y10 = vmysin(x, 10)
y100 = vmysin(x, 100)
y1000 = vmysin(x, 1000)
y = np.sin(x)

import matplotlib.pyplot as plt
plt.plot(x, y, label='sin(x)')
plt.plot(x, y2, label='order 2')
plt.plot(x, y10, label='order 10')
plt.plot(x, y100, label='order 100')
plt.plot(x, y1000, label='order 1000')
plt.ylim([-3, 3])
plt.legend()
plt.show()

绘制罪 x

它存在数值不稳定和下溢,因为一段时间后(~100 个循环,取决于xa变为 0。

于 2013-11-15T21:15:58.110 回答
1

正弦泰勒级数的正确答案

# calculate sin taylor series by using for loop in python
from math import*

print "sine taylor series is="

x=float(raw_input("enter value of x="))

for k in range(0,10,1):
    y=((-1)**k)*(x**(1+2*k))/factorial(1+2*k)

    print y
于 2017-03-20T05:31:02.667 回答