I am new to programming. I have recently written a code in VBA for excel, and I need to write the same code in VB 6.0. I believe my trouble is I'm not sure how to define the element of an array as variables and use them in a function.
Any help is appreciated. the variables I am having trouble with is time x and xdot as shown.
Option Explicit
Private Sub Command1_Click()
Dim s As Integer
Dim xddot As Double
Dim timestep As Integer
Dim z, h As Double
Dim row1, endrow As Integer
Dim time, x, xdot As Double
Dim time1, x1, xdot1 As Double
Dim deltatime, deltaxdot As Double
s = Range(Range("C14"), Range("C65536").End(xlUp)).Count
z = 1
x = 0
xdot = 0
xddot = 0
x1 = 0
xdot1 = 0
endrow = Range("A65536").End(xlUp).ROW
For timestep = 1 To s
time = Range("A14").Offset(timestep - 1, 0).Value
x = Range("B14").Offset(timestep - 1, 0).Value
xdot = Range("C14").Offset(timestep - 1, 0).Value
time1 = Range("A14").Offset(timestep - 2, 0).Value
x1 = Range("B14").Offset(timestep - 2, 0).Value
xdot1 = Range("C14").Offset(timestep - 2, 0).Value
deltatime = time - time1
deltaxdot = xdot - xdot1
On Error Resume Next
xddot = deltaxdot / deltatime
If Err.Number <> 0 Then
xddot = 0
End If
Range("F14").Offset(timestep - 1, 0).Value = xddot
Dim k1, k2, k3, k4 As Double
h = deltaxdot
k1 = h * f(deltaxdot, z)
k2 = h * f(deltaxdot + h / 2, z + k1 / 2)
k3 = h * f(deltaxdot + h / 2, z + k2 / 2)
k4 = h * f(deltaxdot + h, z + k3)
z = z + k1 / 6 + k2 / 3 + k3 / 3 + k4 / 6
Range("G14").Offset(timestep - 1, 0).Value = z
Dim f0 As Double
Dim ft As Double
f0 = 0
ft = fc(z, x, xdot, xddot) - f0
Range("E14").Offset(timestep - 1, 0).Value = ft
Next timestep
End Sub
Function f(deltaxdot, z) As Double
Dim A As Double
Dim n As Double
Dim gamma As Double
Dim betta As Double
A = Range("D2").Value
n = Range("E2").Value
gamma = Range("F2").Value
betta = Range("G2").Value
If z > 0 And deltaxdot > 0 Then
f = A - (z ^ n) * (gamma + betta)
ElseIf z > 0 And deltaxdot < 0 Then
f = A + (z ^ n) * (gamma - betta)
ElseIf z < 0 And deltaxdot < 0 Then
f = A - Abs(z ^ n) * (gamma + betta)
ElseIf z < 0 And deltaxdot > 0 Then
f = A + Abs(z ^ n) * (gamma - betta)
End If
End Function
Function c(xdot) As Double
Dim a1 As Double
Dim a2 As Double
Dim p As Double
a1 = Range("H2").Value
a2 = Range("I2").Value
p = Range("J2").Value
c = a1 * Exp(-(a2 * Abs(xdot)) ^ p)
End Function
Function fc(z, x, xdot, xddot) As Double
Dim alfa As Double
Dim k As Double
Dim m As Double
alfa = Range("K2").Value
k = Range("L2").Value
m = Range("M2").Value
fc = alfa * z + k * x + c(xdot) * xdot + m * xddot
End Function
Any help is appreciated. the variables I am having trouble with is time x and xdot as shown.
Option Explicit
Private Sub Command1_Click()
Dim s As Integer
Dim xddot As Double
Dim timestep As Integer
Dim z, h As Double
Dim row1, endrow As Integer
Dim time, x, xdot As Double
Dim time1, x1, xdot1 As Double
Dim deltatime, deltaxdot As Double
s = Range(Range("C14"), Range("C65536").End(xlUp)).Count
z = 1
x = 0
xdot = 0
xddot = 0
x1 = 0
xdot1 = 0
endrow = Range("A65536").End(xlUp).ROW
For timestep = 1 To s
time = Range("A14").Offset(timestep - 1, 0).Value
x = Range("B14").Offset(timestep - 1, 0).Value
xdot = Range("C14").Offset(timestep - 1, 0).Value
time1 = Range("A14").Offset(timestep - 2, 0).Value
x1 = Range("B14").Offset(timestep - 2, 0).Value
xdot1 = Range("C14").Offset(timestep - 2, 0).Value
deltatime = time - time1
deltaxdot = xdot - xdot1
On Error Resume Next
xddot = deltaxdot / deltatime
If Err.Number <> 0 Then
xddot = 0
End If
Range("F14").Offset(timestep - 1, 0).Value = xddot
Dim k1, k2, k3, k4 As Double
h = deltaxdot
k1 = h * f(deltaxdot, z)
k2 = h * f(deltaxdot + h / 2, z + k1 / 2)
k3 = h * f(deltaxdot + h / 2, z + k2 / 2)
k4 = h * f(deltaxdot + h, z + k3)
z = z + k1 / 6 + k2 / 3 + k3 / 3 + k4 / 6
Range("G14").Offset(timestep - 1, 0).Value = z
Dim f0 As Double
Dim ft As Double
f0 = 0
ft = fc(z, x, xdot, xddot) - f0
Range("E14").Offset(timestep - 1, 0).Value = ft
Next timestep
End Sub
Function f(deltaxdot, z) As Double
Dim A As Double
Dim n As Double
Dim gamma As Double
Dim betta As Double
A = Range("D2").Value
n = Range("E2").Value
gamma = Range("F2").Value
betta = Range("G2").Value
If z > 0 And deltaxdot > 0 Then
f = A - (z ^ n) * (gamma + betta)
ElseIf z > 0 And deltaxdot < 0 Then
f = A + (z ^ n) * (gamma - betta)
ElseIf z < 0 And deltaxdot < 0 Then
f = A - Abs(z ^ n) * (gamma + betta)
ElseIf z < 0 And deltaxdot > 0 Then
f = A + Abs(z ^ n) * (gamma - betta)
End If
End Function
Function c(xdot) As Double
Dim a1 As Double
Dim a2 As Double
Dim p As Double
a1 = Range("H2").Value
a2 = Range("I2").Value
p = Range("J2").Value
c = a1 * Exp(-(a2 * Abs(xdot)) ^ p)
End Function
Function fc(z, x, xdot, xddot) As Double
Dim alfa As Double
Dim k As Double
Dim m As Double
alfa = Range("K2").Value
k = Range("L2").Value
m = Range("M2").Value
fc = alfa * z + k * x + c(xdot) * xdot + m * xddot
End Function