泰勒公式求正余弦

正弦

sinx=x-x^3/3!+x^5/5!-.....+(-1)^(m-1)*x^(2*m-1)/(2*m-1)!

# _*_ coding:utf-8 _*_

import numpy as np

def jie(i):
    s = 1
    for j in range(1, i+1):
        s = s * j
    return s

if __name__ == '__main__':
    x = int(input("请输入x="))
    n = int(input("请输入n="))
    s = 0
    for i in range(1, n+1):
        s = s + np.power(-1, i-1) * np.power(x, 2*i-1)/jie(i*2-1)
    #print(np.sin(x))
    print(s)

余弦

cosx=1-x^2/x!+x^4/4!-.......+(-1)^m*x^(2*m)/(2*m)!

# _*_ coding:utf-8 _*_

import numpy as np

def jie(i):
    s = 1
    for j in range(1, i+1):
        s = s * j
    return s

if __name__ == '__main__':
    x = int(input("请输入x="))
    n = int(input("请输入n="))
    s = 1
    for i in range(1, n+1):
        s = s + np.power(-1, i) * np.power(x, 2*i)/jie(i*2)
    #print(np.cos(x))
    print(s)

你可能感兴趣的:(数学)