欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

python 求定积分和不定积分示例

时间:2021-05-20 09:31:20|栏目:Python代码|点击:

求f(x) = sin(x)/x 的不定积分和负无穷到正无穷的定积分

sin(x)/x 的不定积分是信号函数sig ,负无穷到正无穷的定积分为pi

import math
import numpy as np
import matplotlib.pyplot as plt
from sympy import * #用于求导积分等科学计算
 
def draw_plot_set():#设置画图格式
  ax = plt.gca()
  #改变坐标轴位置
  ax.spines['right'].set_color('none')#删除原来轴
  ax.spines['top'].set_color('none')#删除原来轴
  ax.xaxis.set_ticks_position('bottom')#在0点处增加轴
  ax.spines['bottom'].set_position(('data',0))
  ax.yaxis.set_ticks_position('left')#在0点处增加轴
  ax.spines['left'].set_position(('data',0))
  #设置坐标名
  plt.ylabel('f(x)')
  plt.xlabel('x')
  plt.grid(True)#打开网格
 
def dif(left,right,step):#求导 左右区间以及间隔
  x,y = symbols('x y')#引入x y变量
  #expr = x*pow(E,x)#计算表达式
  expr = sin(x)/x
  x_value = [] #save x value
  y_value = [] #save x f(x) value
  y_value_int = [] #save x f(x)_dot value
  expr_int = integrate(expr,x)#求函数的不定积分 c=0
  print(integrate(expr,(x,-oo,oo)))#对x求定积分 负无穷到正无穷
  for i in np.arange(left,right,step):
    x_value.append(i)
    y_value.append(expr.subs('x',i))#将i值代入表达式
    y_value_int.append(expr_int.subs('x',i))#将i值代入积分表达式
 
  draw_plot_set()#设置画图格式
  plt.plot(x_value,y_value,"b-",linewidth=1,label='f(x)='+str(expr)) #画图
  plt.plot(x_value,y_value_int,"r-",linewidth=1,label='F(x)='+str(expr_int)) #画图
  
  plt.legend()#显示图例
  plt.show()#显示图像
  
 
 
if __name__ == '__main__':
  draw_plot_set()#设置画图格式
  dif(-30,30,0.1)

结果:

上一篇:python使用多进程的实例详解

栏    目:Python代码

下一篇:关于python中plt.hist参数的使用详解

本文标题:python 求定积分和不定积分示例

本文地址:http://www.codeinn.net/misctech/125512.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有