迭代循环内的集成内部类型不匹配错误和二分法

时间:2017-07-22 14:09:50

标签: excel-vba vba excel

  

我正在尝试将Mathcad计算转换为Excel电子表格。我试图找到一个名为kw的变量。我在行中遇到类型不匹配错误:

        INTEG = INTEG + ...

有人知道我为什么会收到这个错误吗?我已经花了很多时间在上面,但无法找到原因。由于这项工作对我来说非常重要,我想提前感谢任何帮助/建议。#

Option Explicit
Dim wp As Double, alpha As Double, w As Double, gama As Double, d As Double 
Dim kw As Double, n As Integer, sExp As String, g As Double

'Calculating kw
Sub CalculateKw()
Dim l As Integer
alpha = ThisWorkbook.Sheets("Current-Wave Calculation").Range("H49").Value
gama = ThisWorkbook.Sheets("Current-Wave Calculation").Range("H34").Value
d = ThisWorkbook.Sheets("Environmental Data Input").Range("H4").Value
wp = ThisWorkbook.Sheets("Current-Wave Calculation").Range("H47").Value
kw = 100
For l = 1 To 99
    If Err.Number = 0 Then
        kw = INTEG(0, kw, 5000)
    Else
        kw = kw - 1
    End If
Next l
ThisWorkbook.Sheets("Sheet1").Range("B1").Value = kw
End Sub

'DEFINITE INTEGRAL from 0 to kw*wp to solve JONSWAP Equation
Function INTEG(n, kw, lBit As Long)
Dim SpectralWidthParameter As Double, dMin As Double, dMax As Double
Dim dW As Double, lW As Long, AAA As String
g = 9.80665
alpha = ThisWorkbook.Sheets("Current-Wave Calculation").Range("H49").Value
gama = ThisWorkbook.Sheets("Current-Wave Calculation").Range("H34").Value
d = ThisWorkbook.Sheets("Environmental Data Input").Range("H4").Value
wp = ThisWorkbook.Sheets("Current-Wave Calculation").Range("H47").Value
dMin = 0
dMax = kw * wp
If w <= wp Then
    SpectralWidthParameter = 0.07
Else
    SpectralWidthParameter = 0.09
End If
sExp = "(w ^ n) * (((w / _ 
(Application.WorksheetFunction.Sinh(WaveNumber(0,20,w,d) * d))) ^ 2)*  
(alpha * (g ^ 2) * (w ^ (-5)) * (EXP((-5 / 4) * ((w / wp) ^ (-4)))) * 
(gama  ^ (EXP(-0.5 * (((w - wp) / (SpectralWidthParameter * wp)) ^ 2))))))"
sExp = Replace(sExp, "EXP", "AAA")
dW = (dMax - dMin) / lBit
For lW = 1 To lBit
    *INTEG = INTEG + Evaluate(Replace(Replace(sExp, "w", dMin), "AAA", _
    "EXP")) * dW + 0.5 * dW * Abs(Evaluate(Replace(Replace(sExp, "w", dMin _ 
    +dW), "AAA", "EXP")) - Evaluate(Replace(Replace(sExp, "w", dMin), _ 
    "AAA", "EXP")))*
    dMin = dMin + dW
    Next lW
End Function

'BISECTION METHOD TO CALCULATE Wave number k
Function WaveNumber(a, b, w, d)
Dim klow As Double, khigh As Double, kmid As Double, i As Integer
Dim a As Integer, b As Integer
klow = a
khigh = b
kmid = (klow + khigh) / 2
For i = 1 To 100
    If SolveFunction(klow, w, d) * SolveFunction(kmid, w, d) < 0 Then
        khigh = kmid
        kmid = (klow + khigh) / 2
    Else
        klow = kmid
        kmid = (klow + khigh) / 2
    End If
Next i
WaveNumber = kmid
End Function

'HELPER FUNCTION(Wave Dispersion Equation)FOR BISECTION METHOD
Function SolveFunction(k, w, d)
SolveFunction = k * Application.WorksheetFunction.Tanh(k * d) - (w ^ 2) _
/ 9.80665
End Function

1 个答案:

答案 0 :(得分:1)

INTEG = INTEG + Evaluate(....)

&#34;输入不匹配&#34; ,因为您的Evaluate表达式返回了错误。向错误变体添加数字会生成运行时错误。

基本上你不应该在评估的表达式中插入Application.WorksheetFunction.Sinh(...),而只是Sinh(...)

此外,我认为您可以在不使用Evaluate的情况下重新编写代码,这样可以更轻松地调试代码。