如何使用`scipy.integrate.quad`计算一个函数的积分,这取决于另一个函数的积分 [英] How to use `scipy.integrate.quad` to compute integral of a function which depends on the integral of another function

查看:555
本文介绍了如何使用`scipy.integrate.quad`计算一个函数的积分,这取决于另一个函数的积分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对计算此积分的任何帮助,都使用f函数定义F函数,该函数涉及第一个积分,最后对F进行积分.

Any help to compute this integration, F function is defined using the f function which involves the first integration, finally, integrate F.

from scipy.integrate import quad

f = lambda x,a : a**2*x

def F(s,a):
  return quad(f,0,s,args=(a,))
quad(F,0,5,args=(4,))

得到错误:

      2 def F(s,a):
      3   return quad(f,0,s,args=(a,))
----> 4 quad(F,0,5,args=(4,))
      5 
    446     if points is None:
    447         if infbounds == 0:
--> 448             return _quadpack._qagse(func,a,b,args,full_output,epsabs,epsrel,limit)
    449         else:
    450             return _quadpack._qagie(func,bound,infbounds,args,full_output,epsabs,epsrel,limit)

TypeError: must be real number, not tuple

推荐答案

看看

返回:

y:float函数从a到b的积分.

y: float The integral of func from a to b.

abserr:float对结果中绝对误差的估计.

abserr: float An estimate of the absolute error in the result.

...

所以有多个返回值(一个元组),这就是为什么您收到TypeError: must be real number, not tuple消息的原因.

So there are multiple return values (a tuple) and that's why you're getting the TypeError: must be real number, not tuple message.

我想,您只是对整数值quad(...)[0]感兴趣,因此您的F应该返回以下内容:

I guess, you're just interested in the integral value quad(...)[0] so that's what your F should return:

from scipy.integrate import quad

f = lambda x, a: a**2 * x
F = lambda x, a: quad(f, 0, x, args=(a,))[0]

I = quad(F, 0, 5, args=(4,))
print(I)

哪些印刷品:

(333.33333333333337, 3.700743415417189e-12)

这篇关于如何使用`scipy.integrate.quad`计算一个函数的积分,这取决于另一个函数的积分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆