练习:Taylor展式的应用
#!/usr/bin/python
# -*- coding:utf-8 -*-
import numpy as np
import math
import matplotlib as mpl
import matplotlib.pyplot as plt
def calc_e_small(x):
n = 10
f = np.arange(1, n+1).cumprod()
b = np.array([x]*n).cumprod()
return np.sum(b / f) + 1
def calc_e(x):
reverse = False
if x < 0: # 处理负数
x = -x
reverse = True
ln2 = 0.69314718055994530941723212145818
c = x / ln2
a = int(c+0.5)
b = x - a*ln2
y = (2 ** a) * calc_e_small(b)
if reverse:
return 1/y
return y
if __name__ == "__main__":
t1 = np.linspace(-2, 0, 10, endpoint=False)
t2 = np.linspace(0, 4, 20)
t = np.concatenate((t1, t2))
print(t) # 横轴数据
y = np.empty_like(t)
for i, x in enumerate(t):
y[i] = calc_e(x)
print('e^', x, ' = ', y[i], '(近似值)\t', math.exp(x), '(真实值)')
# print '误差:', y[i] - math.exp(x)
plt.figure(facecolor='w')
mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['axes.unicode_minus'] = False
plt.plot(t, y, 'r-', t, y, 'go', linewidth=2)
plt.title('Taylor展式的应用 - 指数函数', fontsize=18)
plt.xlabel('X', fontsize=15)
plt.ylabel('exp(X)', fontsize=15)
plt.grid(True, ls=':')
plt.show()
#!/usr/bin/python
# -*- coding:utf-8 -*-
import numpy as np
import math
import matplotlib as mpl
import matplotlib.pyplot as plt
def calc_sin_small(x):
x2 = -x ** 2
t = x
f = 1
sum = 0
for i in range(10):
sum += t / f
t *= x2
f *= ((2*i+2)*(2*i+3))
return sum
def calc_sin(x):
a = x / (2*np.pi)
k = np.floor(a)
a = x - k*2*np.pi
return calc_sin_small(a)
if __name__ == "__main__":
t = np.linspace(-2*np.pi, 2*np.pi, 100, endpoint=False)
print(t) # 横轴数据
y = np.empty_like(t)
for i, x in enumerate(t):
y[i] = calc_sin(x)
print('sin(', x, ') = ', y[i], '(近似值)\t', math.sin(x), '(真实值)')
print('误差:', y[i] - math.sin(x))
mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['axes.unicode_minus'] = False
plt.figure(facecolor='w')
plt.plot(t, y, 'r-', t, y, 'go', linewidth=2)
plt.title('Taylor展式的应用 - 正弦函数', fontsize=18)
plt.xlabel('X', fontsize=15)
plt.ylabel('sin(X)', fontsize=15)
plt.xlim((-7, 7))
plt.ylim((-1.1, 1.1))
plt.grid(True)
plt.show()