簡體   English   中英

有沒有辦法限制“尋求目標”? 如果沒有,您將如何處理?

[英]Is there a way to put bounds on Goal Seek? If not, how would you go about this?

我試圖通過改變在F1中找到的De的值來最小化殘差平方和的值。 我希望“計算的CFL”的值盡可能接近“測量的CFL”的值。 這些殘差的平方和越小,擬合度越好! 在向stackoverflow提出了一些建議之后,我決定使用Goal Seek通過改變De的值來最小化殘差平方和,以使其盡可能接近零,我想找到De的最理想值。

我使該程序完美運行,或者我想……我發現,我沒有使用=SUM(D2:D14)對每個殘差求和,而是不小心使用了=SUM(D2,D14) 所以我只是總結第一個和最后一個數字。

現在,我試圖對所有殘差平方求和,得到這些瘋狂的錯誤以及De的瘋狂值。

我知道De的值必須大於零且小於一。 我如何利用這些界限將目標尋求保持在一定范圍內? 如果有幫助,在這種情況下,De的答案約為0.012。 我不斷收到錯誤#NUM! 在所有剩余的細胞中。 這是因為溢出問題嗎?

如果您得出結論,使用目標尋求通過找到De的最理想值來最小化這些總和是行不通的,您將如何處理? 我還可以使用其他求解器嗎?

這是代碼:

Option Explicit

Dim Counter As Long
Dim DeSimpleFinal As Double
Dim simpletime As Variant
Dim Tracker As Double
Dim StepAmount As Double
Dim Volume As Double
Dim SurfArea As Double
Dim pi As Double
Dim FinalTime As Variant
Dim i As Variant

Sub SimpleDeCalculationNEW()

    'This is so you can have the data and the table I'm working with!
    Counter = 13
    Volume = 12.271846
    SurfArea = 19.634954
    pi = 4 * Atn(1)
    Range("A1") = "Time(days)"
    Range("B1") = "CFL(measured)"
    Range("A2").Value = 0.083
    Range("A3").Value = 0.292
    Range("A4").Value = 1
    Range("A5").Value = 2
    Range("A6").Value = 3
    Range("A7").Value = 4
    Range("A8").Value = 5
    Range("A9").Value = 6
    Range("A10").Value = 7
    Range("A11").Value = 8
    Range("A12").Value = 9
    Range("A13").Value = 10
    Range("A14").Value = 11
    Range("B2").Value = 0.0612
    Range("B3").Value = 0.119
    Range("B4").Value = 0.223
    Range("B5").Value = 0.306
    Range("B6").Value = 0.361
    Range("B7").Value = 0.401
    Range("B8").Value = 0.435
    Range("B9").Value = 0.459
    Range("B10").Value = 0.484
    Range("B11").Value = 0.505
    Range("B12").Value = 0.523
    Range("B13").Value = 0.539
    Range("B14").Value = 0.554

    Range("H2").Value = Volume
    Range("H1").Value = SurfArea

    Range("C1") = "CFL Calculated"
    Range("D1") = "Residual Squared"
    Range("E1") = "De value"
    Range("F1").Value = 0.1

    'Inserting Equations
    Range("C2") = "=((2 * $H$1) / $H$2) * SQRT(($F$1 * A2) / PI())"
    Range("C2").Select
    Selection.AutoFill Destination:=Range("C2:C" & Counter + 1), Type:=xlFillDefault

    Range("D2") = "=((ABS(B2-C2))^2)"
    Range("D2").Select
    Selection.AutoFill Destination:=Range("D2:D" & Counter + 1), Type:=xlFillDefault

    'Summing up the residuals squared
    Range("D" & Counter + 2) = "=Sum(D2: D" & Counter + 1 & ")"

    'Goal Seek
    Range("D" & Counter + 2).GoalSeek Goal:=0, ChangingCell:=Range("F1")

    Columns("A:Z").EntireColumn.EntireColumn.AutoFit

    DeSimpleFinal = Range("F1")    
    MsgBox ("The Final Value for DeSimple is: " & DeSimpleFinal)

End Sub

您會收到NUM個錯誤,因為在當前解決方案中F1的值將變為負數,並且您試圖在其中一個表達式中采用F1的平方根。

同樣,在這種情況下,目標搜索對您正在使用的F1的特定初始開始“猜測”非常敏感。 如果您在現在使用的0.1的任一側稍微改變F1初始值,這將很明顯。 實際上,根據F1值,目標尋求解決方案中存在很大的不穩定區域:

目標尋求不穩定

在提出問題時,如果可以對解決方案搜索的可能輸入設置約束,則更有可能獲得可用的結果。 Excel帶有一個稱為Solver的加載項,它允許這樣做,並提供幾種不同的搜索方法。 求解時自動加載當你第一次啟動Excel,但加載它是容易的,因為解釋在這里

您要求其他求解器。 有關替代方法和一些理論來幫助您了解正在發生的事情,請查看數字食譜此處是在線書籍)。 第10章討論了這一點。 如果您想嘗試使用不同於GoalSeek或Solver加載項的方法,它包括現成的代碼示例。 當然,代碼是用Fortran / C / C ++編寫的,但是它們很容易轉換為VBA(我已經做過很多次了)。

Goaleek函數使用二分法算法,可以這樣編碼:

Sub dicho(ByRef target As Range, ByRef modif As Range, ByVal targetvalue As Double, ByVal a As Double, ByVal b As Double)

Dim i As Integer
Dim imax As Integer
Dim eps As Double

eps = 0.01
imax = 10
i = 0
While Abs(target.Value - targetvalue) / Abs(targetvalue) > eps And i < imax

    modif.Value = (a + b) / 2
    If target.Value - targetvalue > 0 Then
        a = (a + b) / 2
    Else
        b = (a + b) / 2
    End If
    i = i + 1
Wend
End Sub

a和b是您的界線。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM