简体   繁体   中英

Python: Calling functions inside functions.

Regula is a function that finds the minimum value on a set interval (Finds the root of a function).

def Regula(f,Tol,Nmax,a,b):
    N = 1
    while N < Nmax:
        c = (b*f(a)-a*f(b)) / (f(a)-f(b))
        if f(c) == 0 or abs(a-b) < Tol:
            break
        if (np.sign(f(c)) == np.sign(f(a))):
            a = c
        else:
            b =c 
        N += 1 
    return c

These are the two functions I am using. M2 calls G2.

def G2(T):
    return (48*np.pi**2)/(11*N*(np.log(lam*T+Ts))**2)

def M2(T,func):
    return ((N/6)*(func(T))*(T**2))**0.5

M2 works. I promise.

My problem is that I do not know how to generalise Regula to call M2, which calls G2.

Help please!

Min_Mass = Regula (M2(T,G2), Tol, 50, a, b)

a = 1
b = 4
T = np.linspace(a,b,100)
Tol=1e-13

Why don't you simply use:

...

def M2(T,value):
    return ((N/6)*( value )*(T**2))**0.5

Min_Mass = Regula (M2(T,G2(T)), Tol, 50, a, b)

...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM