Module VB0101 Public Sub Main() Console.WriteLine(3 + 5) Console.WriteLine(3 - 5) Console.WriteLine(3 * 5) Console.WriteLine(3 ^ 5) Console.WriteLine(5 / 3) Console.WriteLine(5 \ 3) Console.WriteLine(5 Mod 3) Console.Write(3 * 5 & vbNewLine) Console.WriteLine(String.Format("{0,3:D}", 3 * 5)) Console.WriteLine(String.Format("{0,23:F20}", 5 / 3)) End Sub End Module Module VB0102 Public Sub Main() Dim i As Integer = 3 * 5 Console.WriteLine("3 * 5 = " & i) Console.WriteLine(String.Format("3 * 5 = {0}", i)) End Sub End Module Module VB0103 Public Sub Main() For i As Integer = 1 To 9 Console.Write(i & ", ") Next Console.WriteLine() End Sub End Module Module VB0104 Public Sub Main() For i As Integer = 1 To 9 If i Mod 3 = 0 Then Console.Write(i & ", ") End If Next Console.WriteLine() End Sub End Module Module VB0105 Public Sub Main() Dim sum As Integer = 0 For i As Integer = 1 To 99 If i Mod 3 = 0 Then sum += i End If Next Console.WriteLine(sum) End Sub End Module Module VB0301 Public Sub Main() ' 3 の倍数の合計 Console.WriteLine( sn(3, 999) ) End Sub ' 初項:a, 公差:a で, 上限:lim の数列の総和を返す関数 Private Function sn(ByVal a As Integer, ByVal lim As Integer) As Integer Dim n As Integer = lim \ a ' 項数:n = 上限:lim / 公差:a Dim l As Integer = n * a ' 末項:l = 項数:n * 公差:a Return (a + l) * n \ 2 ' 総和:sn = (初項:a + 末項:l) * 項数:n / 2 End Function End Module Module VB0302 Sub Main() ' 10000 までの 自然数の和 ' 項目数 n = 10000 Dim n As Integer = 10000 Console.WriteLine( n * (n + 1) \ 2 ) End Sub End Module Module VB0303 Sub Main() ' 10000 までの 偶数の和 ' 項目数 n = 5000 Dim n As Integer = 10000 \ 2 Console.WriteLine( n * (n + 1) ) End Sub End Module Module VB0304 Sub Main() ' 10000 までの 奇数の和 ' 項目数 n = 5000 Dim n As Integer = 10000 \ 2 Console.WriteLine( n ^ 2 ) End Sub End Module Module VB0305 Sub Main() ' 1000 までの 自然数の2乗の和 Dim n As Integer = 1000 Console.WriteLine( n * (n + 1) * (2 * n + 1) \ 6 ) End Sub End Module Module VB0306 Sub Main() ' 100 までの 自然数の3乗の和 Dim n As Integer = 100 Console.WriteLine( (n ^ 2) * ((n + 1) ^ 2) \ 4 ) End Sub End Module Module VB0307 Sub Main() ' 初項 2, 公比 3, 項数 10 の等比数列の和 Dim n As Integer = 10 Dim a As Integer = 2 Dim r As Integer = 3 Console.WriteLine( (a * ((r ^ n) - 1)) \ (r - 1) ) End Sub End Module Option Explicit Module VB0401 Sub Main() Dim a As Integer = 5 '初項 5 Dim d As Integer = 3 '公差 3 Dim n As Integer = 10 '項数 10 Dim p As Long = 1 '積 For i As Integer = 1 To n Dim m As Integer = a + (d * (i - 1)) p *= m Next Console.WriteLine(p) End Sub End Module Option Explicit Module VB0402 Sub Main() '初項 5, 公差 3, 項数 10 の数列の積を表示する Console.WriteLine(product(5, 3, 10)) End Sub Private Function product(ByVal m As Integer, ByVal d As Integer, ByVal n As Integer) As Long If n = 0 Then Return 1 Else Return m * product(m + d, d, n - 1) End If End Function End Module Module VB0403 '階乗を求める関数 Private Function Fact(ByVal n As Integer) As Integer If n <= 1 Then Return 1 Else Return n * Fact(n - 1) End If End Function Sub Main() '10の階乗 Console.WriteLine(Fact(10)) Console.WriteLine(10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1) End Sub End Module Module VB0404 '下降階乗冪 Private Function FallingFact(ByVal x As Integer, ByVal n As Integer) As Integer If n <= 1 Then Return x Else Return x * FallingFact(x - 1, n - 1) End If End Function Sub Main() '10 から 6 までの 総乗 Console.WriteLine(FallingFact(10, 5)) Console.WriteLine(10 * 9 * 8 * 7 * 6) End Sub End Module Module VB0405 '上昇階乗冪 Private Function RisingFact(ByVal x As Integer, ByVal n As Integer) As Integer If n <= 1 Then Return x Else Return x * RisingFact(x + 1, n - 1) End If End Function Sub Main() '10 から 14 までの 総乗 Console.WriteLine(RisingFact(10, 5)) Console.WriteLine(10 * 11 * 12 * 13 * 14) End Sub End Module Module VB0406 '階乗 Private Function Fact(ByVal n As Integer) As Integer If n <= 1 Then Return 1 Else Return n * Fact(n - 1) End If End Function '下降階乗冪 Private Function FallingFact(ByVal x As Integer, ByVal n As Integer) As Integer If n <= 1 Then Return x Else Return x * FallingFact(x - 1, n - 1) End If End Function Sub Main() '順列 (異なる 10 個のものから 5 個取ってできる順列の総数) Dim n As Integer = 10 Dim r As Integer = 5 Console.WriteLine(Fact(n) / Fact(n - r)) Console.WriteLine(FallingFact(n, r)) End Sub End Module Module VB0407 Sub Main() '重複順列 (異なる 10 個のものから重複を許して 5 個取ってできる順列の総数) Dim n As Integer = 10 Dim r As Integer = 5 Console.WriteLine(n ^ r) End Sub End Module Module VB040101 '組合せ Private Function Comb(ByVal n As Integer, ByVal r As Integer) As Integer If (r = 0) OrElse (r = n) Then Return 1 ElseIf r = 1 Then Return n Else Return Comb(n - 1, r - 1) + Comb(n - 1, r) End If End Function Sub Main() '組合せ (異なる 10 個のものから 5 個取ってできる組合せの総数) Dim n As Integer = 10 Dim r As Integer = 5 Console.WriteLine(Comb(n, r)) End Sub End Module Module VB0409 '組合せ Private Function Comb(ByVal n As Integer, ByVal r As Integer) As Integer If (r = 0) OrElse (r = n) Then Return 1 ElseIf r = 1 Then Return n Else Return Comb(n - 1, r - 1) + Comb(n - 1, r) End If End Function Sub Main() '重複組合せ (異なる 10 個のものから重複を許して 5 個とる組合せの総数) Dim n As Integer = 10 Dim r As Integer = 5 Console.WriteLine(Comb(n + r - 1, r)) End Sub End Module Module VB0501 Public Sub Main() For degree As Integer = 0 To 360 Step 15 If (degree Mod 30 = 0 OrElse degree Mod 45 = 0) Then Dim radian As Double = degree * Math.PI / 180.0 '自作の正弦関数 Dim d1 As Double = mySin(radian, 1, False, radian, 1.0, radian) '標準の正弦関数 Dim d2 As Double = Math.Sin(radian) '標準関数との差異 Console.WriteLine(String.Format("{0,3:D} : {1,13:F10} - {2,13:F10} = {3,13:F10}", degree, d1, d2, d1 - d2)) End If Next End Sub '自作の正弦関数 Private Function mySin(ByVal x As Double, ByVal n As Integer, ByVal nega As Boolean, ByVal numerator As Double, ByVal denominator As Double, ByVal y As Double) As Double Dim m As Integer = 2 * n denominator = denominator * (m + 1) * m numerator = numerator * x * x Dim a As Double = numerator / denominator '十分な精度になったら処理を抜ける If (a <= 0.00000000001) Then Return y Else Return y + mySin(x, n + 1, Not nega, numerator, denominator, If(nega, a, -a)) End If End Function End Module Module VB0502 Public Sub Main() For degree As Integer = 0 To 360 Step 15 If (degree Mod 30 = 0 OrElse degree Mod 45 = 0) Then Dim radian As Double = degree * Math.PI / 180.0 '自作の余弦関数 Dim d1 As Double = myCos(radian, 1, False, 1.0, 1.0, 1.0) '標準の余弦関数 Dim d2 As Double = Math.Cos(radian) '標準関数との差異 Console.WriteLine(String.Format("{0,3:D} : {1,13:F10} - {2,13:F10} = {3,13:F10}", degree, d1, d2, d1 - d2)) End If Next End Sub '自作の余弦関数 Private Function myCos(ByVal x As Double, ByVal n As Integer, ByVal nega As Boolean, ByVal numerator As Double, ByVal denominator As Double, ByVal y As Double) As Double Dim m As Integer = 2 * n denominator = denominator * m * (m - 1) numerator = numerator * x * x Dim a As Double = numerator / denominator '十分な精度になったら処理を抜ける If (a <= 0.00000000001) Then Return y Else Return y + myCos(x, n + 1, Not nega, numerator, denominator, If(nega, a, -a)) End If End Function End Module Module VB0503 Public Sub Main() For i As Integer = 0 To 180 Step 15 If (i Mod 180 <> 0) Then Dim degree As Integer = i - 90 Dim radian As Double = degree * Math.PI / 180.0 Dim x2 As Double = radian * radian '自作の正接関数 Dim d1 As Double = myTan(radian, x2, 15, 0.0) '15:必要な精度が得られる十分大きな奇数 '標準の正接関数 Dim d2 As Double = Math.Tan(radian) '標準関数との差異 Console.WriteLine(String.Format("{0,3:D} : {1,13:F10} - {2,13:F10} = {3,13:F10}", degree, d1, d2, d1 - d2)) End If Next End Sub '自作の正接関数 Private Function myTan(ByVal x As Double, ByVal x2 As Double, ByVal n As Integer, ByVal t As Double) As Double t = x2 / (n - t) n -= 2 If (n <= 1) Then Return x / (1 - t) Else Return myTan(x, x2, n, t) End If End Function End Module Module VB0504 Public Sub Main() For i As Integer = 0 To 20 Dim x As Double = (i - 10) / 4.0 '標準の指数関数 Dim d1 As Double = Math.Exp(x) '自作の指数関数 Dim d2 As Double = myExp(x, 1, 1.0, 1.0, 1.0) '標準関数との差異 Console.WriteLine(String.Format("{0,5:F2} : {1,13:F10} - {2,13:F10} = {3,13:F10}", x, d1, d2, d1 - d2)) Next End Sub '自作の指数関数 Private Function myExp(ByVal x As Double, ByVal n As Integer, ByVal numerator As Double, ByVal denominator As Double, ByVal y As Double) As Double denominator = denominator * n numerator = numerator * x Dim a As Double = numerator / denominator '十分な精度になったら処理を抜ける If (Math.Abs(a) <= 0.00000000001) Then Return y Else Return y + myExp(x, n + 1, numerator, denominator, a) End If End Function End Module Module VB0505 Public Sub Main() For i As Integer = 0 To 20 Dim x As Double = (i - 10) / 4.0 '標準の指数関数 Dim d1 As Double = Math.Exp(x) '自作の指数関数 Dim x2 As Double = x * x Dim d2 As Double = myExp(x, x2, 30, 0.0) '30:必要な精度が得られるよう, 6から始めて4ずつ増加させる '標準関数との差異 Console.WriteLine(String.Format("{0,5:F2} : {1,13:F10} - {2,13:F10} = {3,13:F10}", x, d1, d2, d1 - d2)) Next End Sub '自作の指数関数 Private Function myExp(ByVal x As Double, ByVal x2 As Double, ByVal n As Integer, ByVal t As Double) As Double t = x2 / (n + t) n -= 4 If (n < 6) Then Return 1 + ((2 * x) / (2 - x + t)) Else Return myExp(x, x2, n, t) End If End Function End Module Module VB0506 Public Sub Main() For i As Integer = 1 To 20 Dim x As Double = i / 5.0 '標準の対数関数 Dim d1 As Double = Math.Log(x) '自作の対数関数 Dim x2 As Double = (x - 1) / (x + 1) Dim d2 As Double = 2 * myLog(x2, x2, 1.0, x2) '標準関数との差異 Console.WriteLine(String.Format("{0,5:F2} : {1,13:F10} - {2,13:F10} = {3,13:F10}", x, d1, d2, d1 - d2)) Next End Sub '自作の対数関数 Private Function myLog(ByVal x2 As Double, ByVal numerator As Double, ByVal denominator As Double, ByVal y As Double) As Double denominator = denominator + 2 numerator = numerator * x2 * x2 Dim a As Double = numerator / denominator '十分な精度になったら処理を抜ける If (Math.Abs(a) <= 0.00000000001) Then Return y Else Return y + myLog(x2, numerator, denominator, a) End If End Function End Module Module VB0507 Public Sub Main() For i As Integer = 1 To 20 Dim x As Double = i / 5.0 '標準の対数関数 Dim d1 As Double = Math.Log(x) '自作の対数関数 Dim d2 As Double = myLog(x - 1, 27, 0.0) '27:必要な精度が得られる十分大きな奇数 '標準関数との差異 Console.WriteLine(String.Format("{0,5:F2} : {1,13:F10} - {2,13:F10} = {3,13:F10}", x, d1, d2, d1 - d2)) Next End Sub '自作の対数関数 Private Function myLog(ByVal x As Double, ByVal n As Integer, ByVal t As Double) As Double Dim n2 As Integer = n Dim x2 As Double = x If (n > 3) Then If (n Mod 2 = 0) Then n2 = 2 End If x2 = x * (n \ 2) End If t = x2 / (n2 + t) If (n <= 2) Then Return x / (1 + t) Else Return myLog(x, n - 1, t) End If End Function End Module Module VB0508 Public Sub Main() For i As Integer = 0 To 20 Dim x As Integer = i - 10 '自作の双曲線正弦関数 Dim d1 As Double = mySinh(x, 1, x, 1.0, x) '標準の双曲線正弦関数 Dim d2 As Double = Math.Sinh(x) '標準関数との差異 Console.WriteLine(String.Format("{0,3:D} : {1,17:F10} - {2,17:F10} = {3,13:F10}", x, d1, d2, d1 - d2)) Next End Sub '自作の双曲線正弦関数 Private Function mySinh(ByVal x As Double, ByVal n As Integer, ByVal numerator As Double, ByVal denominator As Double, ByVal y As Double) As Double Dim m As Integer = 2 * n denominator = denominator * (m + 1) * m numerator = numerator * x * x Dim a As Double = numerator / denominator '十分な精度になったら処理を抜ける If (Math.Abs(a) <= 0.00000000001) Then Return y Else Return y + mySinh(x, n + 1, numerator, denominator, a) End If End Function End Module Module VB0509 Public Sub Main() For i As Integer = 0 To 20 Dim x As Integer = i - 10 '自作の双曲線余弦関数 Dim d1 As Double = myCosh(x, 1, 1.0, 1.0, 1.0) '標準の双曲線余弦関数 Dim d2 As Double = Math.Cosh(x) '標準関数との差異 Console.WriteLine(String.Format("{0,3:D} : {1,17:F10} - {2,17:F10} = {3,13:F10}", x, d1, d2, d1 - d2)) Next End Sub '自作の双曲線余弦関数 Private Function myCosh(ByVal x As Double, ByVal n As Integer, ByVal numerator As Double, ByVal denominator As Double, ByVal y As Double) As Double Dim m As Integer = 2 * n denominator = denominator * m * (m - 1) numerator = numerator * x * x Dim a As Double = numerator / denominator '十分な精度になったら処理を抜ける If (Math.Abs(a) <= 0.00000000001) Then Return y Else Return y + myCosh(x, n + 1, numerator, denominator, a) End If End Function End Module Module VB0601 Public Sub Main() Const a As Double = 0 Const b As Double = 1 '台形則で積分 Dim n As Integer = 2 For j As Integer = 1 To 10 Dim h As Double = (b - a) / n Dim s As Double = 0 Dim x As Double = a For i As Integer = 1 To n - 1 x += h s += f(x) Next s = h * ((f(a) + f(b)) / 2 + s) n *= 2 '結果を π と比較 Console.WriteLine(String.Format("{0,2:D} : {1,13:F10}, {2,13:F10}", j, s, s - Math.PI)) Next End Sub Private Function f(ByVal x As Double) As Double Return 4 / (1 + x * x) End Function End Module Module VB0602 Public Sub Main() Const a As Double = 0 Const b As Double = 1 '中点則で積分 Dim n As Integer = 2 For j As Integer = 1 To 10 Dim h As Double = (b - a) / n Dim s As Double = 0 Dim x As Double = a + (h / 2) For i As Integer = 1 To n s += f(x) x += h Next s = h * s n *= 2 '結果を π と比較 Console.WriteLine(String.Format("{0,2:D} : {1,13:F10}, {2,13:F10}", j, s, s - Math.PI)) Next End Sub Private Function f(ByVal x As Double) As Double Return 4 / (1 + x * x) End Function End Module Module VB0603 Public Sub Main() Const a As Double = 0 Const b As Double = 1 'Simpson則で積分 Dim n As Integer = 2 For j As Integer = 1 To 5 Dim h As Double = (b - a) / n Dim s2 As Double = 0 Dim s4 As Double = 0 Dim x As Double = a + h For i As Integer = 1 To n \ 2 s4 += f(x) x += h s2 += f(x) x += h Next s2 = (s2 - f(b)) * 2 + f(a) + f(b) s4 *= 4 Dim s As Double = (s2 + s4) * h / 3 n *= 2 '結果を π と比較 Console.WriteLine(String.Format("{0,2:D} : {1,13:F10}, {2,13:F10}", j, s, s - Math.PI)) Next End Sub Private Function f(ByVal x As Double) As Double Return 4 / (1 + x * x) End Function End Module Module VB0604 Public Sub Main() Const a As Double = 0 Const b As Double = 1 Dim t(6, 6) As Double '台形則で積分 Dim n As Integer = 2 For i As Integer = 1 To 6 Dim h As Double = (b - a) / n Dim s As Double = 0 Dim x As Double = a For j As Integer = 1 To n - 1 x += h s += f(x) Next '結果を保存 t(i,1) = h * ((f(a) + f(b)) / 2 + s) n *= 2 Next 'Richardsonの補外法 n = 4 For j As Integer = 2 To 6 For i As Integer = j To 6 t(i,j) = t(i, j - 1) + (t(i, j - 1) - t(i - 1, j - 1)) / (n - 1) If i = j Then '結果を π と比較 Console.WriteLine(String.Format("{0,2:D} : {1,13:F10}, {2,13:F10}", j, t(i,j), t(i,j) - Math.PI)) End If Next n *= 4 Next End Sub Private Function f(ByVal x As Double) As Double Return 4 / (1 + x * x) End Function End Module Module VB0701 'データ点の数 - 1 Private Const N As Integer = 6 Public Sub Main() Dim x(N) As Double Dim y(N) As Double '1.5刻みで -4.5〜4.5 まで, 7点だけ値をセット For i As Integer = 0 To N Dim d As Double = i * 1.5 - 4.5 x(i) = d y(i) = f(d) Next '0.5刻みで 与えられていない値を補間 For i As Integer = 0 To 18 Dim d As Double = i * 0.5 - 4.5 Dim d1 As Double = f(d) Dim d2 As Double = lagrange(d, x, y) '元の関数と比較 Console.WriteLine(string.Format("{0,5:F2}{4}{1,8:F5}{4}{2,8:F5}{4}{3,8:F5}", d, d1, d2, d1 - d2, vbTab)) Next End Sub '元の関数 Private Function f(ByVal x As Double) As Double Return x - (x ^ 3) / (3 * 2) + (x ^ 5) / (5 * 4 * 3 * 2) End Function 'Lagrange (ラグランジュ) 補間 Private Function lagrange(ByVal d As Double, ByVal x() As Double, ByVal y() As Double) As Double Dim sum As Double = 0 For i As Integer = 0 To N Dim prod As Double = y(i) For j As Integer = 0 To N If j <> i Then prod *= (d - x(j)) / (x(i) - x(j)) End If Next sum += prod Next Return sum End Function End Module Module VB0702 'データ点の数 - 1 Private Const N As Integer = 6 Public Sub Main() Dim x(N) As Double Dim y(N) As Double '1.5刻みで -4.5〜4.5 まで, 7点だけ値をセット For i As Integer = 0 To N Dim d As Double = i * 1.5 - 4.5 x(i) = d y(i) = f(d) Next '0.5刻みで 与えられていない値を補間 For i As Integer = 0 To 18 Dim d As Double = i * 0.5 - 4.5 Dim d1 As Double = f(d) Dim d2 As Double = neville(d, x, y) '元の関数と比較 Console.WriteLine(string.Format("{0,5:F2}{4}{1,8:F5}{4}{2,8:F5}{4}{3,8:F5}", d, d1, d2, d1 - d2, vbTab)) Next End Sub '元の関数 Private Function f(ByVal x As Double) As Double Return x - (x ^ 3) / (3 * 2) + (x ^ 5) / (5 * 4 * 3 * 2) End Function 'Neville (ネヴィル) 補間 Private Function neville(ByVal d As Double, ByVal x() As Double, ByVal y() As Double) As Double Dim w(N, N) As Double For i As Integer = 0 To N w(0,i) = y(i) Next For j As Integer = 1 To N For i As Integer = 0 To N - j w(j,i) = w(j-1,i+1) + (w(j-1,i+1) - w(j-1,i)) * (d - x(i+j)) / (x(i+j) - x(i)) Next Next Return w(N,0) End Function End Module Module VB0703 'データ点の数 - 1 Private Const N As Integer = 6 Public Sub Main() Dim x(N) As Double Dim y(N) As Double '1.5刻みで -4.5〜4.5 まで, 7点だけ値をセット For i As Integer = 0 To N Dim d1 As Double = i * 1.5 - 4.5 x(i) = d1 y(i) = f(d1) Next '差分商の表を作る Dim d(N, N) As Double For j As Integer = 0 To N d(0,j) = y(j) Next For i As Integer = 1 To N For j As Integer = 0 To (N - i) d(i,j) = (d(i-1,j+1) - d(i-1,j)) / (x(j+i) - x(j)) Next Next 'n階差分商 Dim a(N) As Double For j As Integer = 0 To N a(j) = d(j,0) Next '0.5刻みで 与えられていない値を補間 For i As Integer = 0 To 18 Dim d1 As Double = i * 0.5 - 4.5 Dim d2 As Double = f(d1) Dim d3 As Double = newton(d1, x, a) '元の関数と比較 Console.WriteLine(string.Format("{0,5:F2}{4}{1,8:F5}{4}{2,8:F5}{4}{3,8:F5}", d1, d2, d3, d2 - d3, vbTab)) Next End Sub '元の関数 Private Function f(ByVal x As Double) As Double Return x - (x ^ 3) / (3 * 2) + (x ^ 5) / (5 * 4 * 3 * 2) End Function 'Newton (ニュートン) 補間 Private Function newton(ByVal d As Double, ByVal x() As Double, ByVal a() As Double) As Double Dim sum As Double = a(0) For i As Integer = 1 To N Dim prod As Double = a(i) For j As Integer = 0 To (i - 1) If j <> i Then prod *= (d - x(j)) End If Next sum += prod Next Return sum End Function End Module Module VB0704 'データ点の数 - 1 Private Const N As Integer = 6 Private Const Nx2 As Integer = 13 Public Sub Main() Dim x(N) As Double Dim y(N) As Double Dim yd(N) As Double '1.5刻みで -4.5〜4.5 まで, 7点だけ値をセット For i As Integer = 0 To N Dim d1 As Double = i * 1.5 - 4.5 x(i) = d1 y(i) = f(d1) yd(i) = fd(d1) Next '差分商の表を作る Dim z(Nx2) As Double Dim d(Nx2, Nx2) As Double For i As Integer = 0 To Nx2 Dim j As Integer = i \ 2 z(i) = x(j) d(0,i) = y(j) Next For i As Integer = 1 To Nx2 For j As Integer = 0 To (Nx2 - i) If i = 1 AndAlso j mod 2 = 0 Then d(i,j) = yd(j \ 2) Else d(i,j) = (d(i-1,j+1) - d(i-1,j)) / (z(j+i) - z(j)) End If Next Next 'n階差分商 Dim a(Nx2) As Double For j As Integer = 0 To Nx2 a(j) = d(j,0) Next '0.5刻みで 与えられていない値を補間 For i As Integer = 0 To 18 Dim d1 As Double = i * 0.5 - 4.5 Dim d2 As Double = f(d1) Dim d3 As Double = hermite(d1, z, a) '元の関数と比較 Console.WriteLine(string.Format("{0,5:F2}{4}{1,8:F5}{4}{2,8:F5}{4}{3,8:F5}", d1, d2, d3, d2 - d3, vbTab)) Next End Sub '元の関数 Private Function f(ByVal x As Double) As Double Return x - (x ^ 3) / (3 * 2) + (x ^ 5) / (5 * 4 * 3 * 2) End Function '導関数 Private Function fd(ByVal x As Double) As Double Return 1 - (x ^ 2) / 2 + (x ^ 4) / (4 * 3 * 2) End Function 'Hermite (エルミート) 補間 Private Function hermite(ByVal d As Double, ByVal z() As Double, ByVal a() As Double) As Double Dim sum As Double = a(0) For i As Integer = 1 To Nx2 Dim prod As Double = a(i) For j As Integer = 0 To (i - 1) prod *= (d - z(j)) Next sum += prod Next Return sum End Function End Module Module VB0705 'データ点の数 - 1 Private Const N As Integer = 6 Public Sub Main() Dim x(N) As Double Dim y(N) As Double '1.5刻みで -4.5〜4.5 まで, 7点だけ値をセット For i As Integer = 0 To N Dim d1 As Double = i * 1.5 - 4.5 x(i) = d1 y(i) = f(d1) Next '3項方程式の係数の表を作る Dim a(N) As Double Dim b(N) As Double Dim c(N) As Double Dim d(N) As Double For i As Integer = 1 To N - 1 a(i) = x(i) - x(i-1) b(i) = 2.0 * (x(i+1) - x(i-1)) c(i) = x(i+1) - x(i) d(i) = 6.0 * ((y(i+1) - y(i)) / (x(i+1) - x(i)) - (y(i) - y(i-1)) / (x(i) - x(i-1))) Next '3項方程式を解く (ト−マス法) Dim g(N) As Double Dim s(N) As Double g(1) = b(1) s(1) = d(1) For i As Integer = 2 To N - 1 g(i) = b(i) - a(i) * c(i-1) / g(i-1) s(i) = d(i) - a(i) * s(i-1) / g(i-1) Next Dim z(N) As Double z(0) = 0 z(N) = 0 z(N-1) = s(N-1) / g(N-1) For i As Integer = N - 2 To 1 Step -1 z(i) = (s(i) - c(i) * z(i+1)) / g(i) Next '0.5刻みで 与えられていない値を補間 For i As Integer = 0 To 18 Dim d1 As Double = i * 0.5 - 4.5 Dim d2 As Double = f(d1) Dim d3 As Double = spline(d1, x, y, z) '元の関数と比較 Console.WriteLine(string.Format("{0,5:F2}{4}{1,8:F5}{4}{2,8:F5}{4}{3,8:F5}", d1, d2, d3, d2 - d3, vbTab)) Next End Sub '元の関数 Private Function f(ByVal x As Double) As Double Return x - (x ^ 3) / (3 * 2) + (x ^ 5) / (5 * 4 * 3 * 2) End Function 'Spline (スプライン) 補間 Private Function spline(ByVal d As Double, ByVal x() As Double, ByVal y() As Double, ByVal z() As Double) As Double '補間関数値がどの区間にあるか Dim k As Integer = -1 For i As Integer = 1 To N if d <= x(i) Then k = i - 1 Exit For End If Next If (k < 0) Then k = N Dim d1 As Double = x(k+1) - d Dim d2 As Double = d - x(k) Dim d3 As Double = x(k+1) - x(k) return (z(k) * (d1 ^ 3) + z(k+1) * (d2 ^ 3)) / (6.0 * d3) + (y(k) / d3 - z(k) * d3 / 6.0) * d1 + (y(k+1) / d3 - z(k+1) * d3 / 6.0) * d2 End Function End Module Option Explicit Module VB0801 '重力加速度 Private Const g As Double = -9.8 '空気抵抗係数 Private Const k As Double = -0.01 '時間間隔(秒) Private Const h As Double = 0.01 Public Sub Main() '角度 Const degree As Double = 45 Dim radian As Double = degree * Math.PI / 180.0 '初速 250 km/h -> 秒速に変換 Dim v As Double = 250 * 1000 \ 3600 '水平方向の速度 Dim vx(1) As Double vx(0) = v * Math.Cos(radian) '鉛直方向の速度 Dim vy(1) As Double vy(0) = v * Math.Sin(radian) '経過秒数 Dim t As Double = 0.0 '位置 Dim x As Double = 0.0 Dim y As Double = 0.0 'Euler法 Dim i As Integer = 1 Do While (y >= 0.0) '経過秒数 t = i * h '位置 x += h * vx(0) y += h * vy(0) Console.WriteLine(string.Format("{0,4:F2}{5}{1,8:F5}{5}{2,9:F5}{5}{3,9:F5}{5}{4,8:F5}", t, vx(0), vy(0), x, y, vbTab)) '速度 vx(1) = vx(0) + h * fx(vx(0), vy(0)) vy(1) = vy(0) + h * fy(vx(0), vy(0)) vx(0) = vx(1) vy(0) = vy(1) i += 1 Loop End Sub '空気抵抗による水平方向の減速分 Private Function fx(ByVal vx As Double, ByVal vy As Double) As Double Return k * Math.Sqrt(vx * vx + vy * vy) * vx End Function '重力と空気抵抗による鉛直方向の減速分 Private Function fy(ByVal vx As Double, ByVal vy As Double) As Double Return g + (k * Math.Sqrt(vx * vx + vy * vy) * vy) End Function End Module Option Explicit Module VB0802 '重力加速度 Private Const g As Double = -9.8 '空気抵抗係数 Private Const k As Double = -0.01 '時間間隔(秒) Private Const h As Double = 0.01 Public Sub Main() '角度 Const degree As Double = 45 Dim radian As Double = degree * Math.PI / 180.0 '初速 250 km/h -> 秒速に変換 Dim v As Double = 250 * 1000 \ 3600 '水平方向の速度 Dim vx(2) As Double vx(0) = v * Math.Cos(radian) '鉛直方向の速度 Dim vy(2) As Double vy(0) = v * Math.Sin(radian) '経過秒数 Dim t As Double = 0.0 '位置 Dim x(2) As Double x(0) = 0.0 Dim y(2) As Double y(0) = 0.0 'Heun法 Dim i As Integer = 1 Do While (y(0) >= 0.0) '経過秒数 t = i * h '位置・速度 x(1) = x(0) + h * vx(0) y(1) = y(0) + h * vy(0) vx(1) = vx(0) + h * fx(vx(0), vy(0)) vy(1) = vy(0) + h * fy(vx(0), vy(0)) x(2) = x(0) + h * ( vx(0) + vx(1) ) / 2 y(2) = y(0) + h * ( vy(0) + vy(1) ) / 2 vx(2) = vx(0) + h * (fx(vx(0), vy(0)) + fx(vx(1), vy(1))) / 2 vy(2) = vy(0) + h * (fy(vx(0), vy(0)) + fy(vx(1), vy(1))) / 2 x(0) = x(2) y(0) = y(2) vx(0) = vx(2) vy(0) = vy(2) Console.WriteLine(string.Format("{0,4:F2}{5}{1,8:F5}{5}{2,9:F5}{5}{3,9:F5}{5}{4,8:F5}", t, vx(0), vy(0), x(0), y(0), vbTab)) i += 1 Loop End Sub '空気抵抗による水平方向の減速分 Private Function fx(ByVal vx As Double, ByVal vy As Double) As Double Return k * Math.Sqrt(vx * vx + vy * vy) * vx End Function '重力と空気抵抗による鉛直方向の減速分 Private Function fy(ByVal vx As Double, ByVal vy As Double) As Double Return g + (k * Math.Sqrt(vx * vx + vy * vy) * vy) End Function End Module Option Explicit Module VB0803 '重力加速度 Private Const g As Double = -9.8 '空気抵抗係数 Private Const k As Double = -0.01 '時間間隔(秒) Private Const h As Double = 0.01 Public Sub Main() '角度 Const degree As Double = 45 Dim radian As Double = degree * Math.PI / 180.0 '初速 250 km/h -> 秒速に変換 Dim v As Double = 250 * 1000 \ 3600 '水平方向の速度 Dim vx(1) As Double vx(0) = v * Math.Cos(radian) '鉛直方向の速度 Dim vy(1) As Double vy(0) = v * Math.Sin(radian) '経過秒数 Dim t As Double = 0.0 '位置 Dim x(1) As Double x(0) = 0.0 Dim y(1) As Double y(0) = 0.0 '中点法 Dim i As Integer = 1 Do While (y(0) >= 0.0) '経過秒数 t = i * h '位置・速度 vx(1) = h * fx(vx(0), vy(0)) vy(1) = h * fy(vx(0), vy(0)) Dim wx As Double = vx(0) + vx(1) / 2.0 Dim wy As Double = vy(0) + vy(1) / 2.0 vx(0) = vx(0) + h * fx(wx, wy) vy(0) = vy(0) + h * fy(wx, wy) x(0) = x(0) + h * wx y(0) = y(0) + h * wy Console.WriteLine(string.Format("{0,4:F2}{5}{1,8:F5}{5}{2,9:F5}{5}{3,9:F5}{5}{4,9:F5}", t, vx(0), vy(0), x(0), y(0), vbTab)) i += 1 Loop End Sub '空気抵抗による水平方向の減速分 Private Function fx(ByVal vx As Double, ByVal vy As Double) As Double Return k * Math.Sqrt(vx * vx + vy * vy) * vx End Function '重力と空気抵抗による鉛直方向の減速分 Private Function fy(ByVal vx As Double, ByVal vy As Double) As Double Return g + (k * Math.Sqrt(vx * vx + vy * vy) * vy) End Function End Module Option Explicit Module VB0804 '重力加速度 Private Const g As Double = -9.8 '空気抵抗係数 Private Const k As Double = -0.01 '時間間隔(秒) Private Const h As Double = 0.01 Public Sub Main() '角度 Const degree As Double = 45 Dim radian As Double = degree * Math.PI / 180.0 '初速 250 km/h -> 秒速に変換 Dim v As Double = 250 * 1000 \ 3600 '水平方向の速度 Dim vx(5) As Double vx(0) = v * Math.Cos(radian) '鉛直方向の速度 Dim vy(5) As Double vy(0) = v * Math.Sin(radian) '経過秒数 Dim t As Double = 0.0 '位置 Dim x(5) As Double x(0) = 0.0 Dim y(5) As Double y(0) = 0.0 'Runge-Kutta法 Dim i As Integer = 1 Do While (y(0) >= 0.0) '経過秒数 t = i * h '位置・速度 x(1) = h * vx(0) y(1) = h * vy(0) vx(1) = h * fx(vx(0), vy(0)) vy(1) = h * fy(vx(0), vy(0)) Dim wx As Double = vx(0) + vx(1) / 2.0 Dim wy As Double = vy(0) + vy(1) / 2.0 x(2) = h * wx y(2) = h * wy vx(2) = h * fx(wx, wy) vy(2) = h * fy(wx, wy) wx = vx(0) + vx(2) / 2 wy = vy(0) + vy(2) / 2 x(3) = h * wx y(3) = h * wy vx(3) = h * fx(wx, wy) vy(3) = h * fy(wx, wy) wx = vx(0) + vx(3) wy = vy(0) + vy(3) x(4) = h * wx y(4) = h * wy vx(4) = h * fx(wx, wy) vy(4) = h * fy(wx, wy) x(0) += ( x(1) + x(2) * 2 + x(3) * 2 + x(4)) / 6 y(0) += ( y(1) + y(2) * 2 + y(3) * 2 + y(4)) / 6 vx(0) += (vx(1) + vx(2) * 2 + vx(3) * 2 + vx(4)) / 6 vy(0) += (vy(1) + vy(2) * 2 + vy(3) * 2 + vy(4)) / 6 Console.WriteLine(string.Format("{0,4:F2}{5}{1,8:F5}{5}{2,9:F5}{5}{3,9:F5}{5}{4,9:F5}", t, vx(0), vy(0), x(0), y(0), vbTab)) i += 1 Loop End Sub '空気抵抗による水平方向の減速分 Private Function fx(ByVal vx As Double, ByVal vy As Double) As Double Return k * Math.Sqrt(vx * vx + vy * vy) * vx End Function '重力と空気抵抗による鉛直方向の減速分 Private Function fy(ByVal vx As Double, ByVal vy As Double) As Double Return g + (k * Math.Sqrt(vx * vx + vy * vy) * vy) End Function End Module Option Explicit Module VB0805 '重力加速度 Private Const g As Double = -9.8 '空気抵抗係数 Private Const k As Double = -0.01 '時間間隔(秒) Private Const h As Double = 0.01 Public Sub Main() '角度 Const degree As Double = 45 Dim radian As Double = degree * Math.PI / 180.0 '初速 250 km/h -> 秒速に変換 Dim v As Double = 250 * 1000 \ 3600 '水平方向の速度 Dim vx(5) As Double vx(0) = v * Math.Cos(radian) '鉛直方向の速度 Dim vy(5) As Double vy(0) = v * Math.Sin(radian) '経過秒数 Dim t As Double = 0.0 '位置 Dim x(5) As Double x(0) = 0.0 Dim y(5) As Double y(0) = 0.0 'Runge-Kutta-Gill法 Dim i As Integer = 1 Do While (y(0) >= 0.0) '経過秒数 t = i * h '位置・速度 x(1) = h * vx(0) y(1) = h * vy(0) vx(1) = h * fx(vx(0), vy(0)) vy(1) = h * fy(vx(0), vy(0)) Dim wx As Double = vx(0) + vx(1) / 2.0 Dim wy As Double = vy(0) + vy(1) / 2.0 x(2) = h * wx y(2) = h * wy vx(2) = h * fx(wx, wy) vy(2) = h * fy(wx, wy) wx = vx(0) + vx(1) * ((Math.Sqrt(2.0) - 1) / 2) + vx(2) * (1 - 1 / Math.Sqrt(2.0)) wy = vy(0) + vy(1) * ((Math.Sqrt(2.0) - 1) / 2) + vy(2) * (1 - 1 / Math.Sqrt(2.0)) x(3) = h * wx y(3) = h * wy vx(3) = h * fx(wx, wy) vy(3) = h * fy(wx, wy) wx = vx(0) - vx(2) / Math.Sqrt(2.0) + vx(3) * (1 + 1 / Math.Sqrt(2.0)) wy = vy(0) - vy(2) / Math.Sqrt(2.0) + vy(3) * (1 + 1 / Math.Sqrt(2.0)) x(4) = h * wx y(4) = h * wy vx(4) = h * fx(wx, wy) vy(4) = h * fy(wx, wy) x(0) += ( x(1) + x(2) * (2 - Math.Sqrt(2.0)) + x(3) * (2 + Math.Sqrt(2.0)) + x(4)) / 6 y(0) += ( y(1) + y(2) * (2 - Math.Sqrt(2.0)) + y(3) * (2 + Math.Sqrt(2.0)) + y(4)) / 6 vx(0) += (vx(1) + vx(2) * (2 - Math.Sqrt(2.0)) + vx(3) * (2 + Math.Sqrt(2.0)) + vx(4)) / 6 vy(0) += (vy(1) + vy(2) * (2 - Math.Sqrt(2.0)) + vy(3) * (2 + Math.Sqrt(2.0)) + vy(4)) / 6 Console.WriteLine(string.Format("{0,4:F2}{5}{1,8:F5}{5}{2,9:F5}{5}{3,9:F5}{5}{4,9:F5}", t, vx(0), vy(0), x(0), y(0), vbTab)) i += 1 Loop End Sub '空気抵抗による水平方向の減速分 Private Function fx(ByVal vx As Double, ByVal vy As Double) As Double Return k * Math.Sqrt(vx * vx + vy * vy) * vx End Function '重力と空気抵抗による鉛直方向の減速分 Private Function fy(ByVal vx As Double, ByVal vy As Double) As Double Return g + (k * Math.Sqrt(vx * vx + vy * vy) * vy) End Function End Module Option Explicit Module VB0901 Public Sub Main() Dim a As Double = 1.0 Dim b As Double = 2.0 Console.WriteLine(string.Format("{0,12:F10}", bisection(a, b))) End Sub Private Function bisection(ByVal a As Double, ByVal b As Double) Dim c As Double Do While(True) '区間 (a, b) の中点 c = (a + b) / 2 c = (a + b) / 2 Console.WriteLine(String.Format("{0,12:F10}{2}{1,13:F10}", c, c - Math.Sqrt(2), vbTab)) Dim fc As Double = f(c) If Math.Abs(fc) < 0.0000000001 Then Exit Do If fc < 0 Then 'f(c) < 0 であれば, 解は区間 (c, b) の中に存在 a = c Else 'f(c) > 0 であれば, 解は区間 (a, c) の中に存在 b = c End If Loop Return c End Function Private Function f(ByVal x As Double) As Double Return x * x - 2.0 End Function End Module Option Explicit Module VB0902 Public Sub Main() Dim a As Double = 1.0 Dim b As Double = 2.0 Console.WriteLine(string.Format("{0,12:F10}", falseposition(a, b))) End Sub Private Function falseposition(ByVal a As Double, ByVal b As Double) Dim c As Double Do While(True) '点 (a,f(a)) と 点 (b,f(b)) を結ぶ直線と x軸の交点 c = (a * f(b) - b * f(a)) / (f(b) - f(a)) Console.WriteLine(String.Format("{0,12:F10}{2}{1,13:F10}", c, c - Math.Sqrt(2), vbTab)) Dim fc As Double = f(c) If Math.Abs(fc) < 0.0000000001 Then Exit Do If fc < 0 Then 'f(c) < 0 であれば, 解は区間 (c, b) の中に存在 a = c Else 'f(c) > 0 であれば, 解は区間 (a, c) の中に存在 b = c End If Loop Return c End Function Private Function f(ByVal x As Double) As Double Return x * x - 2.0 End Function End Module Option Explicit Module VB0903 Public Sub Main() Dim x As Double = 1.0 Console.WriteLine(string.Format("{0,12:F10}", iterative(x))) End Sub Private Function iterative(ByVal x0 As Double) Dim x1 As Double Do While(True) x1 = g(x0) Console.WriteLine(String.Format("{0,12:F10}{2}{1,13:F10}", x1, x1 - Math.Sqrt(2), vbTab)) If Math.Abs(x1 - x0) < 0.0000000001 Then Exit Do x0 = x1 Loop Return x1 End Function Private Function g(ByVal x As Double) As Double Return (x / 2) + (1 / x) End Function End Module Option Explicit Module VB0904 Public Sub Main() Dim x As Double = 2.0 Console.WriteLine(string.Format("{0,12:F10}", newton(x))) End Sub Private Function newton(ByVal x0 As Double) Dim x1 As Double Do While(True) x1 = x0 - (f0(x0) / f1(x0)) Console.WriteLine(String.Format("{0,12:F10}{2}{1,13:F10}", x1, x1 - Math.Sqrt(2), vbTab)) If Math.Abs(x1 - x0) < 0.0000000001 Then Exit Do x0 = x1 Loop Return x1 End Function Private Function f0(ByVal x As Double) As Double Return x * x - 2 End Function Private Function f1(ByVal x As Double) As Double Return 2 * x End Function End Module Option Explicit Module VB0905 Public Sub Main() Dim x As Double = 2.0 Console.WriteLine(string.Format("{0,12:F10}", bailey(x))) End Sub Private Function bailey(ByVal x0 As Double) Dim x1 As Double Do While(True) x1 = x0 - (f0(x0) / (f1(x0) - (f0(x0) * f2(x0) / (2 * f1(x0))))) Console.WriteLine(String.Format("{0,12:F10}{2}{1,13:F10}", x1, x1 - Math.Sqrt(2), vbTab)) If Math.Abs(x1 - x0) < 0.0000000001 Then Exit Do x0 = x1 Loop Return x1 End Function Private Function f0(ByVal x As Double) As Double Return x * x - 2 End Function Private Function f1(ByVal x As Double) As Double Return 2 * x End Function Private Function f2(ByVal x As Double) As Double Return 2 End Function End Module Option Explicit Module VB0906 Public Sub Main() Dim x0 As Double = 1.0 Dim x1 As Double = 2.0 Console.WriteLine(string.Format("{0,12:F10}", secant(x0, x1))) End Sub Private Function secant(ByVal x0 As Double, ByVal x1 As Double) Dim x2 As Double Do While(True) x2 = x1 - f(x1) * (x1 - x0) / (f(x1) - f(x0)) Console.WriteLine(String.Format("{0,12:F10}{2}{1,13:F10}", x2, x2 - Math.Sqrt(2), vbTab)) If Math.Abs(x2 - x1) < 0.0000000001 Then Exit Do x0 = x1 x1 = x2 Loop Return x2 End Function Private Function f(ByVal x As Double) As Double Return x * x - 2 End Function End Module Option Explicit Module VB1001 Private Const N As Integer = 3 Public Sub Main() Dim a(,) As Double = {{9,2,1,1},{2,8,-2,1},{-1,-2,7,-2},{1,-1,-2,6}} Dim b() As Double = {20,16,8,17} Dim c() As Double = {0,0,0,0} 'ヤコビの反復法 jacobi(a,b,c) Console.WriteLine("解") For i As Integer = 0 To N Console.Write(string.Format("{0,14:F10}{1}", c(i), vbTab)) Next Console.WriteLine() End Sub Private Sub jacobi(a(,) As Double, b() As Double, x0() As Double) Do While(True) Dim x1() As Double = {0,0,0,0} Dim finish As Boolean = True For i As Integer = 0 To N x1(i) = 0 For j As Integer = 0 To N If j <> i Then x1(i) += a(i,j) * x0(j) End If Next x1(i) = (b(i) - x1(i)) / a(i,i) If (Math.Abs(x1(i) - x0(i)) > 0.0000000001) Then finish = False Next For i As Integer = 0 To N x0(i) = x1(i) Console.Write(string.Format("{0,14:F10}{1}", x0(i), vbTab)) Next Console.WriteLine() If finish Then Return Loop End Sub End Module |