dimanche 24 janvier 2021

Is it possible to create a variable and expect the if statement to be about a variable within the variable?

Sorry, my question is worded weirdly, I know. I'm working on a code for the trapezoidal rule. My function is CTR(N,a,b,f). At one point I have to find N such that the value of another function is 4. So my code looks like this

for N in range (1,2000):
    if ((CTR(2*N,0,np.sqrt(np.pi/2),func3)-CTR(N,0,np.sqrt(np.pi/2),func3)) / 
        (CTR(4*N,0,np.sqrt(np.pi/2),func3)-CTR(2*N,0,np.sqrt(np.pi/2),func3)) == 4):
        print("N = ", N)

This wasn't so bad to type out but now I have to do this for another problem which involves me writing out variations of my CTR code 16 times. Is it possible for me to do something like this:

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)

for N in range (1,2000):
    if((Th2-Th1 / Th4 - Th2) == 4):
    print("N = ", N)

I figure it didn't work when I tried because there is no N in the function. However, still wondering if there's some way to simplify my code.

Aucun commentaire:

Enregistrer un commentaire