|
大変読みにくいコードで申し訳ありません。VBAの本を片手に書き上げたコードなのですが、計算時間が遅くお手上げ状態です。下記のコードで、速くするよい方法はないでしょうか?コードは選択&データ読込を、Excelに貼り付けた数値を読込んで計算し、最大値を求める計算をします。
Dt = Range("a2").Value ^ -1
For s = 1 To 50 '周期選択'
t(s) = Cells(s + 1, 8).Value
For g = 1 To 5 '減衰選択'
h = Cells(g, 6).Value
w = 2 * Application.WorksheetFunction.Pi() / t(s)
w2 = w ^ 2
hw = h * w
wd = w * Sqr(1 - h ^ 2)
wdt = wd * Dt
e = Exp(-hw * Dt)
cwdt = Cos(wdt)
swdt = Sin(wdt)
a11 = e * (cwdt + hw * swdt / wd)
a12 = e * swdt / wd
a21 = -e * w2 * swdt / wd
a22 = e * (cwdt - hw * swdt / wd)
ss = -hw * swdt - wd * cwdt
cc = -hw * cwdt + wd * swdt
s1 = (e * ss + wd) / w2
c1 = (e * cc + hw) / w2
s2 = (e * Dt * ss + hw * s1 + wd * c1) / w2
c2 = (e * Dt * cc + hw * c1 - wd * s1) / w2
s3 = Dt * s1 - s2
c3 = Dt * c1 - c2
b11 = -s2 / wdt
b12 = -s3 / wdt
b21 = (hw * s2 - wd * c2) / wdt
b22 = (hw * s3 - wd * c3) / wdt
For a = 1 To 3 '成分選択'
For n = 0 To 50000 'データ読込'
If n = 0 Then '初期条件'
x(n, g, a) = 0
y(n, g, a) = -Cells(5, a).Value * Dt
xy(n, g, a) = 2 * Cells(5, a).Value * w * h * Dt
ElseIf n > 0 Then
x(n, g, a) = (a11 * x(n - 1, g, a) + a12 * y(n - 1, g, a) + b11 * Cells(n + 4,a).Value + b12 * Cells(n + 5, a).Value)
y(n, g, a) = (a21 * x(n - 1, g, a) + a22 * y(n - 1, g, a) + b21 * Cells(n + 4, a).Value + b22 * Cells(n + 5, a).Value)
xy(n, g, a) = -2 * h * w * y(n, g, a) - w2 * x(n, g, a)
End If
Next n
xmax(s, g, a) = Abs(x(0, g, a))
ymax(s, g, a) = Abs(y(0, g, a))
xymax(s, g, a) = Abs(xy(0, g, a))
For n = 0 To 50000 '最大値を求める'
If Abs(x(n, g, a)) >= xmax(s, g, a) Then xmax(s, g, a) = Abs(x(n, g, a))
If Abs(y(n, g, a)) >= ymax(s, g, a) Then ymax(s, g, a) = Abs(y(n, g, a))
If Abs(xy(n, g, a)) >= xymax(s, g, a) Then xymax(s, g, a) = Abs(xy(n, g, a))
Next n
Next a
Next g
Next s
以上です。
|
|