Curtis
Curtis

Reputation: 253

VBA appears to leave a for loop without cause?

I have a for loop (the last loop in the code below) which fills some arrays with values through some computations.

However, for some reason, once i=5 it jumps back up to the top of the loop (the x+h part) without going through the rest of the loop.

While x < xmax

    If x + h < xmax Then                            'If the step is going to overshoot the desired xmax
        x = x + h                                   'make h adequately smalller
    Else
        h = xmax - x
        x = xmax
    End If

                                                                            'k(Order #, equation #)
    For j = 1 To 6                                                          'First to 6th order
        'temp=riddersmethodT(temp)          'Calculate temperature of mixture
        FT = 0
        rho(0) = 0                                 'Setting FT and rho_av to 0 to be re-calculated
        For i = 1 To 7
            rho(0) = rho(0) + rho(i) * Y4(i)       'Calculate average density of mixture
            FT = FT + Y4(i)
            vol_F = vol_F + Y4(i) * MW(i) / rho(i)      'Calculating the total volumetric flowrate (m^3/s)
        Next i

        rho(0) = rho(0) / FT

        For i = 1 To 8                         'Calculating all of the k(1) values for eq 1 to 8
            k(j, i) = AllODES(x, Y4, i, j, k, h, temp, diameter, vol_F, rho(0))
        Next i
    Next j

    For i = 1 To 8
        Y4Old(i) = Y4(i)                                                    'Saving old y4 values to calc delta0
        Y4(i) = Y4(i) + h * (k(1, i) * (37 / 378) + k(3, i) * (250 / 621) + k(4, i) * (125 / 594) + k(6, i) * (512 / 1771))
        Y5(i) = Y4(i) + h * (k(1, i) * (2825 / 27648) + k(3, i) * (18575 / 48384) + k(4, i) * (13525 / 55296) + k(5, i) * (277 / 14336) + k(6, i) * (0.25))

        delta0(i) = error
        delta1(i) = Abs(Y5(i) - Y4(i))
        delRatio(i) = Abs(delta0(i) / delta1(i))                                'Ratio of errors; careful of getting zeroes!
    Next i

I don't understand how this can be possible seeing as i is not being manipulated within that loop. If you have any insight, please let me know!

Upvotes: 0

Views: 34

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522741

My guess is that your final loop over i has a divide by zero somewhere. You could handle errors in your loop using something like the following:

Sub yourSub()
    For i = 1 To 8
        On Error GoTo ErrorHandler:
        Y4Old(i) = Y4(i)                                                    
        'Saving old y4 values to calc delta0
        Y4(i) = Y4(i) + h * (k(1, i) * (37 / 378) + k(3, i) * (250 / 621) + k(4, i) * (125 / 594) + k(6, i) * (512 / 1771))
        Y5(i) = Y4(i) + h * (k(1, i) * (2825 / 27648) + k(3, i) * (18575 / 48384) + k(4, i) * (13525 / 55296) + k(5, i) * (277 / 14336) + k(6, i) * (0.25))

        delta0(i) = error
        delta1(i) = Abs(Y5(i) - Y4(i))
        delRatio(i) = Abs(delta0(i) / delta1(i)
    Next i

    Cleanup:
        ' do cleanup here
    Exit Sub

    ErrorHandler:
        ' handle error here
    Resume Cleanup
End Sub

But it would be best to fix your match which is allowing a division by zero in the first place.

Upvotes: 1

Related Questions