Python 微分法计算定积分,以及用lambda函数作函数的形参

简介: Python 微分法计算定积分,以及用lambda函数作函数的形参

微分法计算定积分


所有积分区间都分割成100万份,计算以下四个常用积分:


20210226105207432.png

from math import sin
from math import log
from math import exp
from math import pi
from math import e
N=1000000
def interalSin(a,b):
    '''sin(x)在区间[a,b]上的定积分'''
    d=(b-a)/N
    r=0
    for i in range(N):
        r+=d*sin(a+i*d+d/2)
    return r
def interalLn(a,b):
    '''ln(x)在区间[a,b]上的定积分'''
    d=(b-a)/N
    r=0
    for i in range(N):
        r+=d*log(a+i*d+d/2)
    return r
def interalAbs(a,b):
    '''ln(x)在区间[a,b]上的定积分'''
    d=(b-a)/N
    r=0
    for i in range(N):
        r+=d*abs(a+i*d+d/2)
    return r
def interalExp(a,b):
    '''e^x在区间[a,b]上的定积分'''
    d=(b-a)/N
    r=0
    for i in range(N):
        r+=d*exp(a+i*d+d/2)
    return r
print(interalSin(0,pi/2))
print(interalLn(1,e))
print(interalAbs(-1,1))
print(interalExp(-1,1),end='\n\n')
print('e-1/e=')
print(e-1/e)
#执行结果:
'''
1.0000000000000981
1.0000000000000977
1.0000000000000002
2.3504023872872146
e-1/e=
2.3504023872876028
>>> 
'''

注:积分区间微分成100万份,就能得到小数点后12位的精度。



自定义函数作函数的形参:

from math import sin
from math import log
from math import exp
from math import pi
from math import e
N=1000000
def Sinx(x):
    return sin(x)
def Ln_x(x):
    return log(x)
def Absx(x):
    return abs(x)
def Expx(x):
    return exp(x)
def Interal(func,a,b):
    '''func(x)在区间[a,b]上的定积分'''
    d=(b-a)/N
    r=0
    for i in range(N):
        r+=d*func(a+i*d+d/2)
    return r
print(Interal(Sinx,0,pi/2))
print(Interal(Ln_x,1,e))
print(Interal(Absx,-1,1))
print(Interal(Expx,-1,1))



直接用库函数作函数的形参:

from math import sin
from math import log
from math import exp
from math import pi
from math import e
N=1000000
def Interal(func,a,b):
    '''func(x)在区间[a,b]上的定积分'''
    d=(b-a)/N
    r=0
    for i in range(N):
        r+=d*func(a+i*d+d/2)
    return r
print(Interal(sin,0,pi/2))
print(Interal(log,1,e))
print(Interal(abs,-1,1))
print(Interal(exp,-1,1))



lambda函数表达复合函数,并做形参计算定积分:


用以下两个题目做示例:

20210316143915563.png


from math import pi
from math import sin
from math import cos
from math import log
from math import sqrt
N=1000000
fx=lambda x:sqrt(1-sin(2*x))
gx=lambda x:sin(x)/(1+sin(x)+cos(x))
def Interal(func,a,b):
    '''func(x)在区间[a,b]上的定积分'''
    d=(b-a)/N
    r=0
    for i in range(N):
        r+=d*func(a+i*d+d/2)
    return r
print(Interal(fx,0,pi/2))
print(2*sqrt(2)-2) #对比答案
print()
print(Interal(gx,0,pi/2))
print((pi/2-log(2))/2) #对比答案
'''
0.8284271247463156
0.8284271247461903
0.43882457311750606
0.43882457311747564
>>>
'''




附:形参个数不定


可以使用*args或**args,但是一定要把*args放到最后面。

def plus(*args):
    tmp=0
    for i in args:
        tmp+=i
    return tmp
def mul_p(fn,x,*args):
    return x*fn(*args)
print(plus(1,2,3,4,5,6,7,8,9,10))
print(mul_p(plus,10,1,2,3,4,5,6,7,8,9,10))
#执行结果:
'''
55
550
>>> 
'''




目录
相关文章
|
5天前
|
数据挖掘 数据处理 索引
python常用pandas函数nlargest / nsmallest及其手动实现
python常用pandas函数nlargest / nsmallest及其手动实现
20 0
|
17天前
|
Python
python函数的参数学习
学习Python函数参数涉及五个方面:1) 位置参数按顺序传递,如`func(1, 2, 3)`;2) 关键字参数通过名称传值,如`func(a=1, b=2, c=3)`;3) 默认参数设定默认值,如`func(a, b, c=0)`;4) 可变参数用*和**接收任意数量的位置和关键字参数,如`func(1, 2, 3, a=4, b=5, c=6)`;5) 参数组合结合不同类型的参数,如`func(1, 2, 3, a=4, b=5, c=6)`。
16 1
|
10天前
|
Serverless 开发者 Python
《Python 简易速速上手小册》第3章:Python 的函数和模块(2024 最新版)
《Python 简易速速上手小册》第3章:Python 的函数和模块(2024 最新版)
40 1
|
10天前
|
索引 Python
Python高维变量选择:SCAD平滑剪切绝对偏差惩罚、Lasso惩罚函数比较
Python高维变量选择:SCAD平滑剪切绝对偏差惩罚、Lasso惩罚函数比较
11 0
|
10天前
|
Python Serverless API
Python风险价值计算投资组合VaR、期望损失ES
Python风险价值计算投资组合VaR、期望损失ES
25 0
Python风险价值计算投资组合VaR、期望损失ES
|
12天前
|
Python
python学习-函数模块,数据结构,字符串和列表(下)
python学习-函数模块,数据结构,字符串和列表
57 0
|
12天前
05-python之函数-函数的定义/函数的参数/函数返回值/函数说明文档/函数的嵌套使用/函数变量的作用域
05-python之函数-函数的定义/函数的参数/函数返回值/函数说明文档/函数的嵌套使用/函数变量的作用域
|
13天前
|
Python
python学习10-函数
python学习10-函数
|
13天前
|
Python
python学习4-内置函数range()、循环结构、循环控制语句、else语句、嵌套循环
python学习4-内置函数range()、循环结构、循环控制语句、else语句、嵌套循环
|
16天前
|
测试技术 开发者 Python
Python中的装饰器:优雅而强大的函数修饰工具
在Python编程中,装饰器是一种强大的工具,用于修改函数或方法的行为。本文将深入探讨Python中装饰器的概念、用法和实际应用,以及如何利用装饰器实现代码的优雅和高效。