在Scipy中具有两个变量的函数中对一个变量的确定积分

时间:2014-08-14 18:08:51

标签: python scipy integrate quad

我试图用scipy中的一个变量来计算具有多个变量的函数的定积分。 这有点像我的代码 -

from scipy.integrate import quad
import numpy as np
def integrand(x,y):
    return x*np.exp(x/y)

quad(integrand, 1,2, args=())

它会返回此类错误:

TypeError: integrand() takes exactly 2 arguments (1 given)

但是,如果我将数字放入args中,它就可以工作。但我不想,因为我希望你保持y而不是数字。有谁知道如何做到这一点?

编辑:对不起,不要以为我很清楚。我希望最终结果是y的函数,y仍然是一个符号。

5 个答案:

答案 0 :(得分:3)

感谢mdurant,这里有什么作用:

from sympy import integrate, Symbol, exp
from sympy.abc import x
y=Symbol('y')
f=x*exp(x/y)
integrate(f, (x, 1, 2))

答案:

-(-y**2 + y)*exp(1/y) + (-y**2 + 2*y)*exp(2/y)

答案 1 :(得分:0)

您可以做的最好的事情是使用functools.partial来绑定您目前拥有的参数。但是如果你还没有指定整个域,那么从根本上说,它不能在数值上整合一个定积分;在这种情况下,结果表达式仍然必须包含符号部分,因此中间结果不是数字。

答案 2 :(得分:0)

(假设您正在讨论在给定特定固定值x的情况下计算y上的定积分。)

你可以使用lambda:

quad(lambda x:integrand(x, 10), 1, 2, args=())

functools.partial()

quad(functools.partial(integrand, y=10), 1, 2, args=())

答案 3 :(得分:0)

您可能只希望结果是y的函数对吗?:

from scipy.integrate import quad
import numpy as np
def integrand(x,y):
    return x*np.exp(x/y)

partial_int = lambda y: quad(integrand, 1,2, args=(y,))
print partial_int(5)
#(2.050684698584342, 2.2767173686148355e-14)

答案 4 :(得分:0)

// Creates a S3 Bucket in the region configured in the shared config
// or AWS_REGION environment variable.
//
// Usage:
//    go run s3_upload_object.go BUCKET_NAME FILENAME
func main() {
    if len(os.Args) != 3 {
        exitErrorf("bucket and file name required\nUsage: %s bucket_name filename",
            os.Args[0])
    }

    bucket := os.Args[1]
    filename := os.Args[2]

    file, err := os.Open(filename)
    if err != nil {
        exitErrorf("Unable to open file %q, %v", err)
    }

    defer file.Close()

    // Initialize a session in us-west-2 that the SDK will use to load
    // credentials from the shared credentials file ~/.aws/credentials.
    sess, err := session.NewSession(&aws.Config{
        Region: aws.String("us-west-2")},
    )

    // Setup the S3 Upload Manager. Also see the SDK doc for the Upload Manager
    // for more information on configuring part size, and concurrency.
    //
    // http://docs.aws.amazon.com/sdk-for-go/api/service/s3/s3manager/#NewUploader
    uploader := s3manager.NewUploader(sess)

    // Upload the file's body to S3 bucket as an object with the key being the
    // same as the filename.
    _, err = uploader.Upload(&s3manager.UploadInput{
        Bucket: aws.String(bucket),

        // Can also use the `filepath` standard library package to modify the
        // filename as need for an S3 object key. Such as turning absolute path
        // to a relative path.
        Key: aws.String(filename),

        // The file to be uploaded. io.ReadSeeker is preferred as the Uploader
        // will be able to optimize memory when uploading large content. io.Reader
        // is supported, but will require buffering of the reader's bytes for
        // each part.
        Body: file,
    })
    if err != nil {
        // Print the error and exit.
        exitErrorf("Unable to upload %q to %q, %v", filename, bucket, err)
    }

    fmt.Printf("Successfully uploaded %q to %q\n", filename, bucket)
}