lunes, 1 de diciembre de 2014


Ejercicio de las unidades 7 y 8

Método de Runge - Kutta

Dim N As Integer
Function f (ByVal t As Double, ByVal y As Double) As Double
f= t - y
End Function

Sub RungeKutta (ByVal a As Double, ByVal b As Double, ByVal N As Integer, ByVal y0 As Double)
Dim h, k1, k2, k3, k4 As Double 
Dim t, y As Double
Dim i As Integer

h = (b - a) / N
t = a 
y = y0
vt (0) = t
vy (0) = y
For i = 1 To N
       K1= h * f (t,y)
       K2 = h * f (t + h / 2, y + k1 / 2)
       K3 = h * f (t + h/ 2, y + k2 / 2)
       K4 = h * f (t+ h, y + k3)
       Y = y + (1 / 6) * (k1+ 2 * k2 +2 * k3 + k4)
       t = t + h
       vt (i) = t
       vy (i) = y
Next
End Sub

Private Sub CmdEvaluar_Click()
tmin = Val (txta.text)
tmax = Val (txtb.Text)
Yinicial = Val (txty0.Text)
N = Val (txtn.Text)
Call Rungekutta (tmin, tmax, N, Yinicial)
Call vertabla
End  sub

Private Sub Vertabla_Click()
Dim f, i As Integer
MSFlexGrid1.Clear
MSFlexGrid1.Rows = 30
MSFlexGrid1.Cols = 2
MSFlexGrid1.FixedCols = 0
MSFlexGrid1.FixedRows = 0
MSFlexGrid1.ColWidth (0) = 750
MSFlexGrid1.ColWidth  (1) = 1100
MSFlexGrid1.TextMatrix (0, 0) = "t"
MSFlexGrid1.TextMatrix (0,1) = "y(t)"
i = 0
For f = 1 To (N + 1)
      MSFlexGrid1.Rows = f + 1
      MSFlexGrid1.TextMatrix (f, 0) = Format $ ( vt (i), "0.000")
      MSFlexGrid1.TextMatrix (f, 1) = Format $ ( vy (i), "0.0000000")
      i = i + 1
Next f
End Sub

Private Sub CmdSalir_Click()
Unload Me
End Sub