scipy.integrate.quad 报错:给定无效可调用对象。

huangapple go评论102阅读模式
英文:

scipy.integrate.quad giving ValueError: invalid callable given

问题

I have to define a function that includes an integral but it keeps giving me this error
ValueError: invalid callable given

Here is the code I used
import numpy as np
from scipy.integrate import quad

def w(x, w0):
return w0*(1+x)

def fun(x, w0, H_i):
f = ( 3 * (1+w(w0, x)) ) / ( 1+x )
return quad(f,np.inf,0) + 2*np.log(H_i)

When I try this function with any value it always gives me that error. What can I do to fix this? Thank you very much and have a nice day scipy.integrate.quad 报错:给定无效可调用对象。

英文:

I have to define a function that includes an integral but it keeps giving me this error

ValueError: invalid callable given

Here is the code I used

import numpy as np
from scipy.integrate import quad

def w(x, w0):
    return w0*(1+x)

def fun(x, w0, H_i):
    f = ( 3 * (1+w(w0, x)) ) / ( 1+x )
    return quad(f,np.inf,0) + 2*np.log(H_i)

When I try this function with any value it always gives me that error. What can I do to fix this? Thank you very much and have a nice day scipy.integrate.quad 报错:给定无效可调用对象。

答案1

得分: 0

The "ValueError: invalid callable given" 错误是因为代码在处理传递给 quad 函数的参数时出现问题。这个函数期望传递一个可调用函数,该函数接受一个参数并返回一个浮点数。然而,在你的 fun 函数中,你实际上将 quad 函数本身作为第一个参数传递了进去。

要修复这个问题,你应该修改你的 fun 函数,定义一个新的函数,接受一个参数并返回积分的值。然后,你可以将这个新函数传递给 quad 函数。

以下是如何修改你的代码来实现这一点的示例:

def fun(x, w0, H_i):
    def integrand(t):
        return (3 * (1 + w(w0, t))) / (1 + t)
    
    result, _ = quad(integrand, np.inf, x)
    return result + 2 * np.log(H_i)

在这个修改后的 fun 函数中,我们定义了一个新的函数 integrand,该函数接受一个参数 t 并返回积分的被积函数。然后,我们将这个被积函数作为第一个参数传递给 quad 函数。

通过进行这些更改,你的 fun 函数应该可以正常工作,而不会抛出"invalid callable"错误。

希望这有所帮助!

英文:

The "ValueError: invalid callable given" error you're seeing means that the code is having trouble with the argument you passed to the quad function. This function expects a callable function that takes a single argument and returns a float. However, in your fun function, you're actually passing in the quad function itself as the first argument.

To fix this, you should modify your fun function to define a new function that takes a single argument and returns the value of the integrand. You can then pass this new function to the quad function.

Here's an example of how you could modify your code to achieve this:

def fun(x, w0, H_i):
    def integrand(t):
        return ( 3 * (1+w(w0, t)) ) / ( 1+t )
    
    result, _ = quad(integrand, np.inf, x)
    return result + 2*np.log(H_i)

In this modified fun function, we define a new function integrand that takes a single argument t and returns the integrand function. We then pass this integrand function to the quad function as the first argument.

By making these changes, your fun function should work correctly without throwing the "invalid callable" error.

Hope this helps!

huangapple
  • 本文由 发表于 2023年4月13日 16:59:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76003575.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定