Issue
When defining Th1
, Th2
, and Th3
, their expressions are evaluated and their value is known. Changing N
won't anything about it, since they've already been evaluated with a value of N
(in your second code you were indeed supposed to define N
before all four operators are defined).
Possible solution
What you can do though is wrap those expressions in a function, which would receive N
as input. I'm assuming of course that func3
as already been defined in the current scope.
def Ths(N):
Th1 = CTR(2*N, 0, np.sqrt(np.pi/2), func3)
Th2 = CTR(N, 0, np.sqrt(np.pi/2), func3)
Th3 = CTR(4*N, 0, np.sqrt(np.pi/2), func3)
return Th1, Th2, Th3
This way you can call Ths
with a value of N
to compute the associated triplet Th1(N)
, Th2(N)
, and Th3(N)
.
for N in range (1,2000):
if ((Th1 - Th2) / (Th3 - Th1)) == 4:
return N
Functional approach
It seems you are working with more than just Th1
, Th2
and Th3
. Also with more than one function. In your post you showed (Th2-Th1) / (Th4 - Th2)
. You would take a functional approach to solving this.
First define all your Thi
as lambdas:
Th1 = lambda N: CTR(2*N, 0, np.sqrt(np.pi/2), func3)
Th2 = lambda N: CTR(N, 0, np.sqrt(np.pi/2), func3)
Th3 = lambda N: CTR(4*N, 0, np.sqrt(np.pi/2), func3)
Th4 = lambda N: CTR(2*N, 0, np.sqrt(np.pi/4), func3) # some other Thi
Then combine those operators:
Fc1 = lambda N: (Th1(N) - Th2(N)) / (Th3(N) - Th1(N))
Fc2 = lambda N: (Th2(N) - Th1(N)) / (Th4(N) - Th2(N))
If your functions all have the same shape, you could even create a helper to create these:
def make_func(a, b, c, d):
return lambda N: (a(N) - b(N)) / (c(N) - d(N))
This way you can define Fc1
and Fc2
very easily with make_fc(Th1, Th2, Th3, Th1)
and make_fc(Th2, Th1, Th4, Th2)
. Notice how I'm not calling Th1
, Th2
, ... but passing Th1
, Th2
, ... to make_func
.
Finally create a wrapper to test your functions:
def test(fc, N_max=2000, value=4):
for N in range(1, N_max):
if fc(N) == 4:
return N