输出每次迭代的值:二分法

时间:2017-03-22 14:33:12

标签: for-loop iteration bisection

我正在使用二分法来求解从反应器流出的物流的出口浓度。我想让我的代码给我#猜测"对于每次迭代并将它列在sheet2中并与迭代次数一起列出,我似乎无法弄明白。目前它正在工作并给我一个准确的猜测,但它只适用于迭代20,我需要每次迭代1-20。 谢谢!

Option Explicit


Function Roots(F, v, k, vol, a, b)
'F is inlet molar flow rate, v is the volumetric flow rate out of the reactor, k is the reaction constant, and vol is the volume of the reactor

    Dim Clow As Double
    Dim Chigh As Double
    Dim Cmid As Double
    Dim i As Integer
'indicating what the variables are
    Clow = a
    Chigh = b
    Cmid = (a + b) / 2
'Setting the number of iterations
    For i = 1 To 20
        If solvefunction(Clow, F, v, k, vol) * solvefunction(Cmid, F, v, k,     vol) < 0 Then
        Chigh = Cmid
        Cmid = (Clow + Chigh) / 2
    Else
        Clow = Cmid
        Cmid = (Clow + Chigh) / 2
    End If
    Next i
    Roots = Cmid

End Function

Function solvefunction(C, F, v, k, vol)
'This is the function that is going to actually do the calculation

    solvefunction = F - (v * C) - (k * (C ^ 2) * vol)
End Function

0 个答案:

没有答案