2012-08-08 172 views
0

輪廓與Z(X,Y)

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

可以計算出該使用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

+0

我將在'ogrid'和'ravel'讀了,​​我會回來的。這看起來像一個非常複雜的解決方案 – RodericDay 2012-08-09 17:44:08

相關問題