Search code examples
pythonpython-3.xnumerical-methods

How to continue your code after an if else statement without breaking?


I am approximating the error for a numerical mathematical method but after I wrote my if else statement the code seems to endlessly continue in that statement unless I break it. but I want to continue the simulation using the code underneath to finish the second part of the approximation. any suggestions?

while True:
        m1 = h*func1(t,v, Hs)
        k1 = h*func2(t,v, Hs)

        m2 = h*func1(t+(h/2),v+(m1/2), Hs + (k1/2))
        k2= h*func2(t+(h/2),v+(m1/2), Hs +(k1/2))

        m3 = h*func1(t+(h/2),v+(m2/2),Hs+(k2/2))
        k3= h*func2(t+(h/2),v+(m2/2),Hs+(k2/2))

        m4 = h*func1(t+h,v+m3,Hs+k3)
        k4= h*func2(t+h,v+m3,Hs+k3)

        v= v +(1/6)*(m1+(2*m2)+(2*m3)+m4)
        Hs=Hs + (1/6)*(k1+(2*k2)+(2*k3)+k4)
        t= t+h
        if Hs > 11: 
            f = f +0.0001
            Hs=Hr*d/(L*f+d)
            v = math.sqrt(2*g*Hr*d/(L*f+d))
            t = 0

        else: 
        break


while (t2 <= 10):
    m12 = z*func1(t2,v2,Hs2)
    k12 = z*func2(t2,v2,Hs2)

    m22 = z*func1(t2+(z/2),v2+(m12/2), Hs2 + (k12/2))
    k22= z*func2(t2+(z/2),v2+(m12/2), Hs2 +(k12/2))

    m32 = z*func1(t2+(z/2),v2+(m22/2),Hs2+(k22/2))
    k32= z*func2(t2+(z/2),v2+(m22/2),Hs2+(k22/2))

    m42 = z*func1(t2+z,v2+m32,Hs2+k32)
    k42= z*func2(t2+z,v2+m32,Hs2+k32)

    v2= v2 +(1/6)*(m12+(2*m22)+(2*m32)+m42)
    Hs2=Hs2 + (1/6)*(k12+(2*k22)+(2*k32)+k42)
    t2 =t2+z


    if t2 == 10:break

after Hs reaches < 11 for a certain f, the program continues until it is endlessly large. But after t has achieved a value of 75 I want the code to continue with the while loop written underneath.


Solution

  • There is a continue statement in python. For example:

    >>> for i in range(10):
    >>>     if i == 3:
    >>>         continue
    >>>     print(i)
    
    0
    1
    2
    4
    5
    6
    7
    8
    9
    

    As you can see the 3 is missing because the continue statement continued when i was 3.

    It didn't stop the loop, it just continued with the 4.

    I think this could help.