如何通过Python中已定义函数的平方计算积分?

问题描述

我在使用Python计算积分时遇到问题。

所以,我定义了函数:

def function_f(x):
    if x>=0 and x<=1: 
        return ((1/3)**x)
    else:
        return 0

我知道使用以下代码来计算简单的平方(无平方):

from scipy import integrate
integrate.quad((function_f),1)

但是问题是我需要根据该函数的平方计算积分,因此我需要根据(function_f)^ 2进行积分。

我希望您能对我的建议有所帮助。

解决方法

我不习惯scipy.integrate,但是根据您的解释,您应该可以做到:

integrate.quad(lambda x:function_f(x)**2,1)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...