| クラスモジュールの便利な使い方T |
|
|
まず、例を利用して、まず、順次クラスモジュールの便利さを説明して 行こうと思います。 例として、シンプソンの数値積分を利用しようと思います。 まず、標準モジュールを利用して、以下のようなコードを作ってみました。 数値積分する式は、固体の比熱の式を積分しています。 あくまでも、数値計算の勉強や、化学の勉強ではなく、エクセルの使い方を 本ホームページは紹介しているので、数値計算等の説明は、割愛します。。 |
|
|
| Function sinpson(sa As Double, sb As Double, sc As Double, x1 As Double, x2 As Double) As Double Dim dd As Integer Dim i As Integer Dim dd2 As Integer Dim h As Double Dim an As Double Dim an1 As Double Dim y As Double Dim j As Integer Dim x As Double sinpson = 0 If x1 = x2 Then Exit Function End If i = 2 dd2 = 2 h = (x2 - x1) / 2 an = f(sa, sb, sc, x1) + 4 * f(sa, sb, sc, x1 + h) + f(sa, sb, sc, x2) * h / 2 an1 = an * 0.9 Do While (Abs(an1 - an) / an * 100 > 0.0000001) an = an1 dd = dd2 * i h = (x2 - x1) / dd y = f(sa, sb, sc, x1) x = x1 For j = 1 To i x = x + h y = y + 4 * f(sa, sb, sc, x) x = x + h y = y + 2 * f(sa, sb, sc, x) Next j y = y - f(sa, sb, sc, x2) an1 = y * h / 3 i = i + 1 Loop sinpson = an1 End Function |
|
|
| Public Function f(a As Double, b As Double, c As Double, x As Double) As Double f = a + b * 10 ^ -3 * x + (c * 10 ^ 5 / x ^ 2) End Function |
|
|
sa,sb,scは、物質によって決まる。定数。X1、X2は、温度範囲を示しています。 単に、コレを、クラスモジュールにしたい場合は、このコードを、クラスモジュールを作って、コピーアンドペーストして、 標準モジュールで、以下のコードを書けば、同様な動作をします。 |
|
|
| Public Function sinp(sa As Double, sb As Double, sc As Double, x1 As Double, x2 As Double) As Double Dim a As New sinpson1 sinp = a.sinpson(sa, sb, sc, x1, x2) End Function |
|
|
シンプソンのコードは、sinpson1のクラスモジュールにペーストしています。 ここで、気が付くと思うのですが、クラスモジュールを、作らなくても 別に、良いのではないか。標準モジュールで、再度定義しないといけないので、 かえって不便なのでは、無いか。 プログラミングの経験が少ない人は、誰でもそう思うかと思います。 では、まず、便利さ改良の第一弾を使い方Uで、説明します。 なお計算結果の例として、以下に計算結果画面を入れます。 |
![]() |
| sa,sb,scは、csa,csb,cscになり、x1、x2は、T1、T2になり、積分結果は、固体の熱容量の(J/mol)の列になります。 |