0

I am trying to integrate my function over u and xx and then store the values in a matrix so I can plot them with imshow or pcolormesh. The bounds on the integration are 0 < u < inf and -inf < xx < inf. At the moment, I am only taking the bounds to be 10 until I can figure this out.

import numpy as np
import pylab as pl
from scipy.integrate import dblquad

b = 50.0

x = np.linspace(-10, 10, 1000)
y = np.linspace(0, 10, 1000)

T = pl.zeros([len(x), len(y)])


for xi in enumerate(x):
    for yi in enumerate(y):
        def f(xi, yi, u, xx):
            return ((np.exp(u * (b - yi)) - np.exp(-u * (b - yi))) /
                    (np.exp(u * b) - np.exp(-u * b)) * np.cos(u * (xx - xi)))


def fint(u, xx):
    return T + dblquad(f, -10, 10, 0.1, 10, args = (u, xx))[0]

This is the code I have so far but I know it isn't working properly; unfortunately, I don't know what the problem is. Maybe I can't have the two for loops in my definition of f or my my fint is wrong.

4

2 回答 2

1

您的问题并不完全清楚您要做什么。但我是这样解释的:你有一个对两个变量u和的双积分xx,它也有两个参数xiyi。您想在xxu的许多不同值上计算积分,xi并将yj这些值存储在 中T。假设这是您想要做的(如果我错了,请纠正我),这就是我的做法。

import numpy as np
from scipy.integrate import dblquad

b = 50.0

x = np.linspace(-10, 10, 1000)
y = np.linspace(0, 10, 1000)

def f(xx, u, xi, yj):
    return ((np.exp(u * (b - yj)) - np.exp(-u * (b - yj))) /
            (np.exp(u * b) - np.exp(-u * b)) * np.cos(u * (xx - xi)))

T = np.zeros([len(x), len(y)])
for i, xi in enumerate(x):
    for j, yj in enumerate(y):
        T[i, j] += dblquad(
            f, -10, 10, lambda x: 0.1, lambda x: 10, args=(xi, yj))[0]
于 2013-10-17T18:23:08.337 回答
0

fint 是唯一调用 f 的东西。你没有调用 fint,这意味着 f 根本没有被使用,只是定义了大约一百万次。我会考虑只定义一次函数并调用它一百万次。

于 2013-10-17T18:21:58.980 回答