亚欧色一区w666天堂,色情一区二区三区免费看,少妇特黄A片一区二区三区,亚洲人成网站999久久久综合,国产av熟女一区二区三区

  • 發布文章
  • 消息中心
點贊
收藏
評論
分享
原創

高精度計算泰勒展開及其誤差

2024-11-29 09:11:14
2
0
# cat demo1.py
import numpy as np
import math


def f(x):
return np.exp(x)


def derivative(x):
return f(x)


def taylor_expand(f, x0, n, x):
r = f(x0)
for i in range(1, n + 1):
r += derivative(x0) * ((x - x0) ** i) / math.factorial(i)
return r


x = 1
x0 = 5
n = 40
r1 = taylor_expand(f, x0, n, x)
r2 = np.exp(x)
print("taylor-expand:", r1)
print("origin-result:", r2)
print("Rn(x) Estimation:", (x0 - x) ** (n + 1) * np.exp(x0) / math.factorial(n + 1))
print("diff=", (r2 - r1))

"""
目的: 用 e^x 在 x0=5處的展開,估算e^x在(x=1)處的數據, 并計算估計誤差Rn(x)
taylor-expand: 2.718281828459021
origin-result: 2.718281828459045
Rn(x) Estimation: 2.1453745731609467e-23
diff= 2.398081733190338e-14

疑問: Rn(x)誤差估計是 10^(-23) 可是實際上 diff是10^(-14) , 這個差距有點大,why
猜測: 1. 累加計算過程有誤差 2. np計算本身有誤差 3.程序邏輯有問題

"""



# cat demo2.py
import mpmath

# 設置精度(位數)
mpmath.mp.dps = 50 # 設置為 50 位小數


# 定義函數 f(x) = exp(x)
def f(x):
return mpmath.exp(x)


def derivative(x):
return f(x)


# 計算階乘
def factorial(n):
return mpmath.factorial(n)


# 泰勒展開函數
def taylor_expand(f, x0, n, x):
r = f(x0)
for i in range(1, n + 1):
r += derivative(x0) * ((x - x0) ** i) / factorial(i)
return r


x = mpmath.mpf(1)
x0 = mpmath.mpf(5)
n = 40

# 計算泰勒展開
r1 = taylor_expand(f, x0, n, x)
r2 = mpmath.exp(x)
r3 = ((x0 - x) ** (n + 1)) * mpmath.exp(x0) / mpmath.factorial(n + 1)
print("taylor-expand:", r1)
print("origin-result:", r2)
print("Rn(x) Estimation:", r3)
print("diff=", (r2 - r1))

"""
taylor-expand: 2.7182818284590452353603070560849832858516189537689
origin-result: 2.7182818284590452353602874713526624977572470937
Rn(x) Estimation: 2.1453745731609467149330950275147177017147805869338e-23
diff= -1.958473232078809437186006894647201943730120045111e-23
這里數量級非常接近了10^(-23)次方誤差是一樣的
"""



0條評論
作者已關閉評論
Top123
32文章數
3粉絲數
Top123
32 文章 | 3 粉絲
Top123
32文章數
3粉絲數
Top123
32 文章 | 3 粉絲
原創

高精度計算泰勒展開及其誤差

2024-11-29 09:11:14
2
0
# cat demo1.py
import numpy as np
import math


def f(x):
return np.exp(x)


def derivative(x):
return f(x)


def taylor_expand(f, x0, n, x):
r = f(x0)
for i in range(1, n + 1):
r += derivative(x0) * ((x - x0) ** i) / math.factorial(i)
return r


x = 1
x0 = 5
n = 40
r1 = taylor_expand(f, x0, n, x)
r2 = np.exp(x)
print("taylor-expand:", r1)
print("origin-result:", r2)
print("Rn(x) Estimation:", (x0 - x) ** (n + 1) * np.exp(x0) / math.factorial(n + 1))
print("diff=", (r2 - r1))

"""
目的: 用 e^x 在 x0=5處的展開,估算e^x在(x=1)處的數據, 并計算估計誤差Rn(x)
taylor-expand: 2.718281828459021
origin-result: 2.718281828459045
Rn(x) Estimation: 2.1453745731609467e-23
diff= 2.398081733190338e-14

疑問: Rn(x)誤差估計是 10^(-23) 可是實際上 diff是10^(-14) , 這個差距有點大,why
猜測: 1. 累加計算過程有誤差 2. np計算本身有誤差 3.程序邏輯有問題

"""



# cat demo2.py
import mpmath

# 設置精度(位數)
mpmath.mp.dps = 50 # 設置為 50 位小數


# 定義函數 f(x) = exp(x)
def f(x):
return mpmath.exp(x)


def derivative(x):
return f(x)


# 計算階乘
def factorial(n):
return mpmath.factorial(n)


# 泰勒展開函數
def taylor_expand(f, x0, n, x):
r = f(x0)
for i in range(1, n + 1):
r += derivative(x0) * ((x - x0) ** i) / factorial(i)
return r


x = mpmath.mpf(1)
x0 = mpmath.mpf(5)
n = 40

# 計算泰勒展開
r1 = taylor_expand(f, x0, n, x)
r2 = mpmath.exp(x)
r3 = ((x0 - x) ** (n + 1)) * mpmath.exp(x0) / mpmath.factorial(n + 1)
print("taylor-expand:", r1)
print("origin-result:", r2)
print("Rn(x) Estimation:", r3)
print("diff=", (r2 - r1))

"""
taylor-expand: 2.7182818284590452353603070560849832858516189537689
origin-result: 2.7182818284590452353602874713526624977572470937
Rn(x) Estimation: 2.1453745731609467149330950275147177017147805869338e-23
diff= -1.958473232078809437186006894647201943730120045111e-23
這里數量級非常接近了10^(-23)次方誤差是一樣的
"""



文章來自個人專欄
文章 | 訂閱
0條評論
作者已關閉評論
作者已關閉評論
0
0