2013-12-09 17 views
8

Sto cercando di implementare un metodo semplice solutore iterativo di Newton utilizzando Excel VB (non ho mai usato VB.)loop senza errori Do

continuo a ricevere l'errore loop without a Do e io non riesco a capire quello che ho' sto facendo male qui.

Sto cercando di trovare le origini della funzione z^3 - z^2 - (B^2 + B - A)z - A*B denominata fattore di compressibilità.

La mia fonte MSN

Function zCalculation(ByVal temp As Double, ByVal press As Double) As Double 

Dim tempCr As Double 
Dim pressCr As Double 
Dim A As Double 
Dim B As Double 

tempCr = temp/238.5 

pressCr = press/547.424092 

A = pressCr/tempCr 
A = A/(9 * (2^(1/3) - 1)) 
B = pressCr/tempCr 
B = B * (2^(1/3) - 1)/3 



Dim zNot As Double 
Dim counter As Integer 
counter = 0 
zNot = 1# 

Do 
    counter = counter + 1 

    zNot = zNot - (zNot^3 - zNot^2 - (B^2 + B - A) * zNot - A * B)/(3 * zNot^2 - 2 * zNot - (B^2 + B - A)) 
    If counter > 1000 Then 
     Exit Do 

Loop Until eval(zNot, A, B) < 0.000001 


zCalculation = zNot 


End Function 

pausa

Function eval(ByVal z As Double, ByVal A As Double, ByVal B As Double) As Double 

    eval = z^3 - z^2 - (B^2 + B - A) * z - A * B 

End Function 

risposta

12

è necessario un:

End If 

nel codice.

+0

dang ..... questo è tutto. Grazie! Non posso credere di averlo perso. –

+3

Non è colpa tua ............ Il messaggio di errore di Excel in questo caso è fuorviante. –

+1

non posso credere che in realtà ho cercato su StackOverflow prima di controllare questo – Tascalator

1

si può provare:

Function zCalculation(ByVal temp As Double, ByVal press As Double) As Double 

    Dim tempCr As Double 
    Dim pressCr As Double 
    Dim A As Double 
    Dim B As Double 

    tempCr = temp/238.5 

    pressCr = press/0.546789 

    A = pressCr/tempCr 
    A = A/(9 * (2^(1/3) - 1)) 
    B = pressCr/tempCr 
    B = B * (2^(1/3) - 1)/3 



    Dim zNot As Double 
    Dim counter As Integer 
    counter = 0 
    zNot = 1# 

    Do 
     counter = counter + 1 

     zNot = zNot - (zNot^3 + zNot^2 - (B^2 + B - A) * zNot - A * B)/(3 * zNot^2 + 2 * zNot - (B^2 + B - A)) 
     If counter > 1000 Then 
     Exit Do 
     End if ' <--- Here 

    Loop Until eval(zNot, A, B) < 0.000001 

    zCalculation = zNot 
End Function 
+0

anche se lo avevi corretto e Gary lo ha pubblicato per primo. Grazie! –

+0

Nessun problema. Sono felice di aiutarti. – Makah

-2
Sub datacalculationsandformat() 
Dim row As Integer 
row = 2 
Do While Cells(row, 2) <> "" 
Cells(row, 3).Value = Cells(row, 2).Value * 0.3 
Cells(row, 4).Value = Cells(row, 2) * 0.1 
Cells(row, 5).Value = Cells(row, 2).Value + Cells(row, 3).Value + Cells(row, 4).Value 
If Cells(row, 5).Value >= 8000 Then 
Worksheets("Sheet1").Cells(row, 5).Font.Bold = True 
row = row + 1 
Loop 
Problemi correlati