欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python實(shí)現(xiàn)畫出e指數(shù)函數(shù)的圖像

 更新時(shí)間:2019年11月21日 15:26:42   作者:OliverkingLi  
今天小編就為大家分享一篇python實(shí)現(xiàn)畫出e指數(shù)函數(shù)的圖像,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

這里用Python逼近函數(shù)y = exp(x);同樣使用泰勒函數(shù)去逼近:

exp(x) = 1 + x + (x)^2/(2!) + .. + (x)^n/(n!) + ...

#!/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: # 處理負(fù)數(shù)
  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, 3, 20)
 t = np.concatenate((t1, t2))
 print(t)  # 橫軸數(shù)據(jù)
 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), '(真實(shí)值)')
  # print '誤差:', y[i] - math.exp(x)
 plt.figure(facecolor='w')
 mpl.rcParams['font.sans-serif'] = [u'SimHei']
 mpl.rcParams['axes.unicode_minus'] = False
 plt.plot(t, y, 'r-', t, y, 'go', linewidth=2)
 plt.title(u'Taylor展式的應(yīng)用 - 指數(shù)函數(shù)', fontsize=18)
 plt.xlabel('X', fontsize=15)
 plt.ylabel('exp(X)', fontsize=15)
 plt.grid(True)
 plt.show()

以上這篇python實(shí)現(xiàn)畫出e指數(shù)函數(shù)的圖像就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論