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

yipeiwu_com6年前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 list语法学习(带例子)

创建:list = [5,7,9]取值和改值:list[1] = list[1] * 5列表尾插入:list.append(4)去掉第0个值并返回第0个值的数值:list.pop(0)去...

Python单例模式的两种实现方法

Python单例模式的两种实现方法 方法一  import threading class Singleton(object): __instance = N...

python练习程序批量修改文件名

复制代码 代码如下:# encoding:utf-8 ### 文件名如:# 下吧.mp3##import os,re fs=os.listdir('xb')for f in fs:&nb...

Python 生成 -1~1 之间的随机数矩阵方法

Python 生成 -1~1 之间的随机数矩阵方法

1. 使用函数 np.random.random 由于 np.random.random() 默认生成 0~1 之间的小数,因此需要转换一下 如生成 3*3 的 -1~1 之间的随机数...

Python的Django框架使用入门指引

Python的Django框架使用入门指引

 前言 传统 Web 开发方式常常需要编写繁琐乏味的重复性代码,不仅页面表现与逻辑实现的代码混杂在一起,而且代码编写效率不高。对于开发者来说,选择一个功能强大并且操作简洁的开发...