轮廓与Z(X,Y)

时间:2012-08-08 20:18:10

标签: python numpy matplotlib

我有

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功能吗?

1 个答案:

答案 0 :(得分: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()

enter image description here