This is a class about learning simulation's assignment.
Using a composite function as a probability density function.
In other words, to generate variable of composite function by uniform random number.
4. 3
――――― 以下 ソースコード ―――――
Const beta1 As Double = 3 / 5, _
beta2 As Double = 9 / 10, _
beta3 As Double = 1 / 2
Function Rei1() As Double
Dim xi As Double
Randomize
xi = Rnd()
Rei1 = Rnd()
If (xi < beta1) Then
Else
Rei1 = max(Rei1, Rnd())
End If
If (xi < beta2) Then
Else
Rei1 = max(Rei1, Rnd())
End If
End Function
5. 4
Function Rei2() As Double
Randomize
Rei2 = (Rnd()) ^ 2
If (Rnd() > beta3) Then
Rei2 = 1 - Rei2
End If
End Function
Function max(a As Double, b As Double) As Double
If (a > b) Then
max = a
Else
max = b
End If
End Function
Function F(ex As Integer, x1 As Double, x2 As Double)
If ex = 1 Then
F = 3 / 5 * ((x2 + 1 / 2 * x2 * x2 + 1 / 6 * x2 * x2 * x2) _
- (x1 + 1 / 2 * x1 * x1 + 1 / 6 * x1 * x1 * x1))
ElseIf ex = 2 Then
F = -1 / 2 * ((Sqr(1 - x2) - Sqr(x2)) - (Sqr(1 - x1) - Sqr(x1)))
End If
6. 5
End Function
Function histogramming()
Dim N As Long, Nbin As Integer, Nbin1 As Integer, _
NRnd As Long, i As Integer, ex As Integer
Dim a As Double, b As Double, dx As Double, _
x As Double, x2 As Double, y As Double
With Sheet1
.Cells(4, 11) = InputBox("例題番号を入力してください(1 or 2)", "例題番号の決定")
.Cells(4, 12) = InputBox("Nbin 数を入力してください。(30-50)", "Nbin の決定")
.Cells(4, 13) = InputBox("発生する乱数の個数を入力してください。型:Long", "乱数の個数の決定")
ex = .Cells(4, 11)
Nbin = .Cells(4, 12)
Nbin1 = Nbin + 1
NRnd = .Cells(4, 13)
a = 0#
b = 1#
Dim H(0 To 51) As Long
For i = 0 To Nbin1
H(i) = 0
Next i
dx = (b - a) / Nbin
For N = 1 To NRnd
If ex = 1 Then
x1 = Rei1()
ElseIf ex = 2 Then
x1 = Rei2()
End If
7. 6
x2 = x1 - a '3 つの乱数の最大値 0~1
i = Int(x2 / dx) + 1 '(x * Nbin) + 1 ->添字
If (i < 0) Then
i = 0
End If
If (i > Nbin) Then
i = Nbin1
End If
H(i) = H(i) + 1
Next N
For i = 1 To Nbin
x = a + (i - 0.5) * dx ' / Nbin
y = H(i) / (NRnd * dx)
.Cells(i + 3, 2) = x
.Cells(i + 3, 3) = y
Next i
End With
End Function
Sub Analytic()
Dim N As Long, Nbin As Integer, Nbin1 As Integer, _
i As Integer, a As Integer, b As Integer, ex As Integer
Dim dx As Double, H As Double, _
x As Double, x1 As Double, x2 As Double
8. 7
With Sheet1
.Cells(4, 11) = InputBox("例題番号を入力してください(1 or 2)", "例題番号の決定")
.Cells(4, 12) = InputBox("Nbin 数を入力してください。(30-50)", "Nbin の決定")
.Cells(4, 13) = InputBox("発生する乱数の個数を入力してください。型:Long", "乱数の個数の決定")
ex = .Cells(4, 11)
Nbin = .Cells(4, 12)
Nbin1 = Nbin + 1
NRnd = .Cells(4, 13)
a = 0
b = 1
dx = (b - a) / Nbin
For i = 1 To Nbin
x1 = a + (i - 1) * dx
x2 = x1 + dx
x = (x1 + x2) / 2
H = F(ex, x1, x2) / dx
.Cells(i + 3, 4) = x
.Cells(i + 3, 5) = H
Next i
End With
End Sub
Sub clear()
With Sheet1
Range(.Cells(4, 2), Cells(100, 5)).clear
End With
End Sub