python实现画出e指数函数的图像

作者:OliverkingLi 时间:2023-09-24 22:12:10 

这里用Python逼近函数y = exp(x);同样使用泰勒函数去逼近:

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: # 处理负数
 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实现画出e指数函数的图像

来源:https://blog.csdn.net/OliverkingLi/article/details/79156073

标签:python,e指数函数,图像
0
投稿

猜你喜欢

  • 不用mod_rewrite直接用php实现伪静态化页面代码

    2023-11-01 07:07:45
  • asp.net实现存储和读取数据库图片

    2024-01-19 06:56:32
  • python格式的Caffe图片数据均值计算学习

    2022-10-28 07:44:19
  • python使用matplotlib绘制雷达图

    2022-10-10 16:37:41
  • 高效管理http连接的方法

    2022-02-16 23:12:14
  • django实现模型字段动态choice的操作

    2023-08-13 07:30:31
  • django的聚合函数和aggregate、annotate方法使用详解

    2023-06-27 16:07:46
  • python 获取星期字符串的实例

    2022-08-17 19:14:37
  • python中PyQuery库用法分享

    2023-12-05 03:08:31
  • pytest官方文档解读fixtures的调用方式

    2022-01-18 18:40:49
  • python中urlparse模块介绍与使用示例

    2021-08-08 10:13:18
  • FrontPage XP设计教程2——网页的编辑

    2008-10-11 12:16:00
  • php json_encode与json_decode详解及实例

    2023-07-04 22:46:27
  • python 中的 super详解

    2023-09-07 01:27:35
  • 使用PyInstaller将Python程序文件转换为可执行程序文件

    2023-03-28 11:35:34
  • php全局变量和类配合使用深刻理解

    2023-11-18 19:50:17
  • python中sample函数的介绍与使用

    2021-02-02 15:38:56
  • Python中xml和json格式相互转换操作示例

    2023-04-26 18:02:39
  • 浅谈如何使用python抓取网页中的动态数据实现

    2021-10-19 08:47:18
  • 如何通过命令行进入python

    2022-10-28 22:12:56
  • asp之家 网络编程 m.aspxhome.com