python用quad、dblquad实现一维二维积分的实例详解

yipeiwu_com5年前Python基础

背景:

python函数库scipy的quad、dblquad实现一维二维积分的范例。需要注意dblquad的积分顺序问题。

代码:

import numpy as np
from scipy import integrate
 
 
def half_circle(x):
  """
  原心:(1,0),半径为1
  半圆函数:(x-1)^2+y^2 = 1
  """
  return (1-(x-1)**2)**0.5
 
"""
梯形法求积分:半圆线和x轴包围的面积
"""
N = 10000
x = np.linspace(0,2,num=N)#,endpoint=True)
dh = (2-0)/N
y = half_circle(x)
"""
梯形法求积分:(上底+ 下底)*高/2
"""
S = sum((y[1:]+y[:-1])*dh/2)
 
print("=========%s=========="%"梯形法")
print("面积:%f"%S)
 
"""
直接调用intergrate的积分函数quad
"""
S2,err = integrate.quad(half_circle,0,2)
 
print("=========%s=========="%"quad")
print("面积:%f"%S2)
 
 
"""
多重定积分:注意积分顺序
"""
def half_sphere(y,x):
  """
  球心:(1,0,0)
  半径:1
  半球:(x-1)^2+y^2+z^2=1
  """
  return (1-(x-1)**2-y**2)**0.5
 
"""
积分顺序:
v = V x in [0,2] :V y in [-g(x),h(x)]
"""
V3,err = integrate.dblquad(half_sphere,0,2,lambda x:-half_circle(x),lambda x:half_circle(x))
print("========%s==========="%"dblquad")
print("体积:%f"%V3)

结果:

========
=========梯形法==========
面积:1.570638
=========quad==========
面积:1.570796
========dblquad===========
体积:2.094395

以上这篇python用quad、dblquad实现一维二维积分的实例详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python使用pip安装报错:is not a supported wheel on this platform的解决方法

Python使用pip安装报错:is not a supported wheel on this platform的解决方法

本文讲述了Python使用pip安装报错:is not a supported wheel on this platform的解决方法。分享给大家供大家参考,具体如下: 可能的原因1:安...

把django中admin后台界面的英文修改为中文显示的方法

创建一个django工程,我使用的django 1.8.2,创建工程后,settings.py中设置中文显示支持定义 LANGUAGE_CODE = 'en-us'#改为zh-Han...

python Pandas 读取txt表格的实例

运行环境 Python 2.7 操作实例 1.原始文本格式:空格分隔的txt,例如 2016-03-22 00:06:24.4463094 中文测试字符 2016-03-22 00...

tf.truncated_normal与tf.random_normal的详细用法

本文介绍了tf.truncated_normal与tf.random_normal的详细用法,分享给大家,具体如下: tf.truncated_normal 复制代码 代码如下: tf....

PyCharm+PySpark远程调试的环境配置的方法

PyCharm+PySpark远程调试的环境配置的方法

前言:前两天准备用 Python 在 Spark 上处理量几十G的数据,熟料在利用PyCharm进行PySpark远程调试时掉入深坑,特写此博文以帮助同样深处坑中的bigdata&mac...