python画函数等高图

编程入门 行业动态 更新时间:2024-10-14 14:20:15

python画<a href=https://www.elefans.com/category/jswz/34/1771370.html style=函数等高图"/>

python画函数等高图

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, 3, 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'] = [u'SimHei']

mpl.rcParams['axes.unicode_minus'] = False

plt.plot(t, y, 'r-', t, y, 'go', linewidth=2)

plt.title(u'Taylor展式的应用 - 指数函数', fontsize=18)

plt.xlabel('X', fontsize=15)

plt.ylabel('exp(X)', fontsize=15)

plt.grid(True)

plt.show()

更多推荐

python画函数等高图

本文发布于:2024-03-10 11:11:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1727814.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   等高   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!