简体   繁体   中英

need help in creating excel-vba function

My problem: It works fine when I call it in a SUb. But it gives a high jump in the value of "pf" I want to give a condition that pf is always less than dpdlold...I tried different ways but nothing worked..

code starts from here:

Public Function pf(p, pwf, xf, PI, tau, n, k, wg, Hf, wf, Bg, dpdlold, mug, Bg_p, mug_p)

Dim i, j As Integer

Dim error As Double

Dim pj As Double

Dim pj1 As Double

Dim pj0 As Double

Dim e As Double

j = 100

i = 0

error = 1000

pj0 = dpdlold

pj = pj0

e = 0.001


    While (error > e) And (i < j)
        qgr = FrGas(tau, n, k, wg, Hf, wf, pj, mug_p)
        pj1 = (p - pwf - 2 * qgr / PI) / xf
        error = Abs(pj1 - pj)
        pj = pj1
        i = i + 1
    Wend
        pf = pj


End Function

How about changing your loop to something like this:

While (error > e) And (i < j)
    qgr = FrGas(tau, n, k, wg, Hf, wf, pj, mug_p)
    pj1 = (p - pwf - 2 * qgr / PI) / xf
    error = Abs(pj1 - pj)
    if pj1 > dpdlold Then pj1 = dpdlold
    pj = pj1
    i = i + 1
Wend

Now, if pj1 gets too big, it will be "clipped" to the value of dpdlold . If it hits the limit twice, the "error" in successive estimates will be zero and it will stop.

With the limited information you gave it's the best I can do…

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM