scipy应用积分操作

1.什么是scipy?

SciPy是一款方便、易于使用、专为科学和工程设计的Python工具包.它包括统计,优化,整合,线性代数模块,傅里叶变换,信号和图像处理,常微分方程求解器等等.

 integrate是scipy中求积分的一个模块

import pandas as pd
import numpy as bp
from pandas import Series,DataFrame
from matplotlib.pyplot as plt
%matplotlib inline
from scipy import integrate

积分公式:

案例:求圆周率的值

 pi = S/r**2

先求出圆的面积

# 利用积分求圆周率
# pi = s/r**2
# 设r==1
# 即:pi = s
# 很坐标x范围:

x = np.linspace(-1,1,100)

#x的面积为-1到1,并且分成100份

# 园的公式:y**2+x**2 = 1

cilcre = lambda x:(1-x**2)**0.5

#定义横纵坐标映射关系

plt.figure(figsize=(5,5))

#将横纵坐标单位设置成一样长

# 画出圆的图形如下

plt.plot(x,cilcre(x))# 圆在x轴上半部分的图形

plt.plot(x,-cilcre(x)) #园在x轴下半部分的图形

 #quad()    计算定积分,三个参数,一个横纵坐标的关系映射,横坐标的最大值和最小值

S,errar= integrate.quad(cilcre,-1,1)            有两个返回值,一个是定积分的值S,另一个是定积分的误差error

print('圆周虑为:%0.16f'%(S*2))                      保留保留小数位数

输出结果:

圆周虑为:3.1415926535897967

猜你喜欢

转载自www.cnblogs.com/kuangkuangduangduang/p/10291201.html
今日推荐