0

我有

from pylab import *

###FUNCTIONS##
def LOSS(y0,y1):
    return sum(abs(y0-y1))

def genuchten(t,C,k):
    return 1/(1+(C/t)**k)**(1-1/k)

###MAIN#######
if __name__ == '__main__':
    t0 = linspace(0,10,100)
    f0 = genuchten(t0,3,3)

    x = linspace(1,10,10)
    y = linspace(1,10,10)
    X,Y = meshgrid(x,y)

    Z = zeros(shape(X))
    for i in range(len(x)):
        for j in range(len(y)):
            f = genuchten(t0,X[i][j],Y[i][j])
            Z[i][j] = LOSS(f0,f)

    contourf(X,Y,Z)
    show()

这行得通,但我觉得必须有更直接的方法。似乎 MATLAB 有一个 ARRAYFUN 函数可以自动执行此操作?

4

1 回答 1

1

您可以通过使用 ndarray 的广播来快速计算:

from pylab import *

def genuchten(t,C,k):
    return 1/(1+(C/t)**k)**(1-1/k)

###MAIN#######
t0, Y, X = ogrid[0:10:100j, 1:10:10j, 1:10:10j]
f0 = genuchten(t0, 3, 3)
f = genuchten(t0, X, Y)
Z = sum(abs(f0-f), axis=0)
contourf(X.ravel(), Y.ravel(), Z)
show()

在此处输入图像描述

于 2012-08-09T00:56:37.210 回答