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

For loop 输出每次迭代的值:二分法,for-loop,iteration,bisection,For Loop,Iteration,Bisection,我用二分法来求解从反应器流出的流的出口浓度。我希望我的代码给我每个迭代的“猜测”,并在sheet2中列出它和迭代编号,但我似乎无法理解它。目前它正在工作,给了我一个准确的猜测,但它只适用于迭代20,我需要它用于每个迭代1-20。 谢谢 选项显式 函数根(F,v,k,vol,a,b) ‘F是入口摩尔流量,v是反应器外的体积流量,k是反应常数,vol是反应器的体积 暗色双关 双倍暗淡的Chigh 双精度Cmid 作为整数的Dim i '指示变量是什么 Clow=a Chigh=b Cmid=(a+b

我用二分法来求解从反应器流出的流的出口浓度。我希望我的代码给我每个迭代的“猜测”,并在sheet2中列出它和迭代编号,但我似乎无法理解它。目前它正在工作,给了我一个准确的猜测,但它只适用于迭代20,我需要它用于每个迭代1-20。 谢谢

选项显式
函数根(F,v,k,vol,a,b)
‘F是入口摩尔流量,v是反应器外的体积流量,k是反应常数,vol是反应器的体积
暗色双关
双倍暗淡的Chigh
双精度Cmid
作为整数的Dim i
'指示变量是什么
Clow=a
Chigh=b
Cmid=(a+b)/2
'设置迭代次数
对于i=1到20
如果解算函数(Clow,F,v,k,vol)*解算函数(Cmid,F,v,k,vol)<0,则
Chigh=Cmid
Cmid=(Clow+Chigh)/2
其他的
Clow=Cmid
Cmid=(Clow+Chigh)/2
如果结束
接下来我
根=Cmid
端函数
函数解算函数(C、F、v、k、vol)
'这是实际进行计算的函数
解算函数=F-(v*C)-(k*(C^2)*vol)
端函数
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