簡體   English   中英

python 泰勒系列cos x 展開

[英]Taylor series of cos x expansion in python

我想計算 cosx 系列的總和(同時保持 x 為弧度)。 這是我創建的代碼:

import math
def cosine(x,n):
    sum = 0
    for i in range(0, n+1):
        sum += ((-1) ** i) * (x**(2*i)/math.factorial(2*i))
    return sum

我使用math.cos()檢查了它。 當我嘗試小數字時它工作得很好:

print("Result: ", cosine(25, 1000))
print(math.cos(25))

output:

Result: 0.991203540954667 0.9912028118634736

數量還是差不多的。 但是當我嘗試更大的數字時,即 40,它只是返回一個完全不同的值。

Result: 1.2101433786727471 -0.6669380616522619

任何人都知道為什么會這樣?

泰勒展開式的誤差項隨着距離展開點的距離越來越遠(在本例中為x_0 = 0 )。 為了減少錯誤,通過僅在區間[0, 2 * pi]內評估來利用周期性和對稱性:

def cosine(x, n):
    x = x % (2 * pi)
    total = 0
    for i in range(0, n + 1):
        total += ((-1) ** i) * (x**(2*i) / math.factorial(2*i))
    return total

這可以進一步改進為[0, pi/2]

def cosine(x, n):
    x = x % (2 * pi)
    if x > pi:
        x = abs(x - 2 * pi)
    if x > pi / 2:
        return -cosine(pi - x, n)
    total = 0
    for i in range(0, n + 1):
        total += ((-1) ** i) * (x**(2*i) / math.factorial(2*i))
    return total

與您得到的答案相反,無論參數有多大,這個泰勒級數都會收斂。 項的分母中的階乘最終使項變為 0。

階乘部分占主導地位之前,項的絕對值會變得越來越大。 本機浮點數沒有足夠的精度來為低位保留足夠的信息。

這是一種不會丟失任何精度的方法。 它不實用,因為它很慢。 當我告訴您時請相信我,學習如何編寫實用、快速、高質量的數學庫通常需要多年的經驗。

def mycos(x, nbits=100):
    from fractions import Fraction
    x2 = - Fraction(x) ** 2
    i = 0
    ntries = 0
    total = term = Fraction(1)
    while True:
        ntries += 1
        term = term * x2 / ((i+1) * (i+2))
        i += 2
        total += term
        if (total // term).bit_length() > nbits:
            break
    print("converged to >=", nbits, "bits in", ntries, "steps")
    return total

然后是你的例子:

>>> mycos(25)
converged to >= 100 bits in 60 steps
Fraction(177990265631575526901628601372315751766446600474817729598222950654891626294219622069090604398951917221057277891721367319419730580721270980180746700236766890453804854224688235663001, 179569976498504495450560473003158183053487302118823494306831203428122565348395374375382001784940465248260677204774780370309486592538808596156541689164857386103160689754560975077376)
>>> float(_)
0.9912028118634736
>>> mycos(40)
converged to >= 100 bits in 82 steps
Fraction(-41233919211296161511135381283308676089648279169136751860454289528820133116589076773613997242520904406094665861260732939711116309156993591792484104028113938044669594105655847220120785239949370429999292710446188633097549, 61825710035417531603549955214086485841025011572115538227516711699374454340823156388422475359453342009385198763106309156353690402915353642606997057282914587362557451641312842461463803518046090463931513882368034080863251)
>>> float(_)
-0.6669380616522619

注意事項:

  • 全精度結果需要很多位。

  • 四舍五入為浮點數,它們與您從math.cos()獲得的內容完全匹配。

  • 它不需要接近 1000 步就可以收斂。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM