我正在编写一个代码,该代码调用.txt文件,然后将该数据用作求解方程式的变量。我需要帮助调用将变量设置为输入值。文本文件最多包含10行,每行中有7个值表示不同的值。每行都是单独的运行/模拟,应输出到单独的工作表。这是我到目前为止所拥有的
Option Explicit 'this forces you to declare all variables - a good addition to
'coding standards
Sub build3()
'This subroutine implements the Lanchester "modern" (squared law) battle
'simulation. In other words it "solves" the system of differential equations;
' dx/dt = -beta*y ; dy/dt = -alpha*x for alpha, beta, x, y > 0
'User inputs are troop starting levels (x0 and y0) and effectiveness coefficients
'(alpha and beta) for the respective sides of the battle. In this Build the
'inputs are used to generate a uniform random variable within the min and max
'specified.
'Error checking for inputs is included. Other errors go to a generic error message
'A comma delimited file with battle simulation results is written (which will look like an Excel sheet)
'The system of differential equations is "solved" using Euler's method as
'described in the Build assignment materials
'On Error GoTo GENERIC_ERROR: 'leads to generic error message - does not validate inputs
Application.ScreenUpdating = False 'makes code run faster - must turn back on later
Dim alpha, alphaMin, alphaMax, beta, betaMin, betaMax As Double 'input variables as described above
Dim xn, yn, xn1, yn1 As Double 'troop levels at current (xn) and new time(xn1)
Dim t As Double 'current time
Dim deltaT As Double 'time step
Dim iterations As Integer 'track how many times through the loop to prevent infinite
Dim dataOutput As Range 'upper left of range for output data
Dim newSheet As Worksheet 'write calculations into a new sheet
Dim FileNum As Integer
Dim dataline As Variant
Dim myFile As String
Dim fileTotal As Integer
Dim filevars As String
'const is used to make sure that code below cannot change the value
'ALL_CAPS is a common coding standard to indicate constants
Const MAX_TIME = 500 'from customer specifications
'Browse for file to be selected
FileNum = FreeFile()
With Application.FileDialog(msoFileDialogFilePicker)
.InitialFileName = ActiveWorkbook.Path
.Title = "Please Select an Input .txt file"
.Show
myFile = .SelectedItems(1)
End With
Open myFile For Input As #FileNum
While Not EOF(FileNum)
Line Input #FileNum, dataline
dataline = Trim(dataline) 'eliminate leading and trailing spaces and tabs
filevars = Split(dataline, ",")
xn = dataline(1)
yn = dataline(2)
alphaMin = dataline(3)
alphaMax = dataline(4)
betaMin = dataline(5)
betaMax = dataline(6)
deltaT = dataline(7)
Wend
Close #FileNum
'make a new sheet
Set newSheet = Worksheets.Add
newSheet.Activate 'set the newsheet to be active so that's where the results go
'newSheet.Name = "Lanchester Battle Results"
Set dataOutput = ActiveSheet.Cells(1, 1) 'set is needed because a range is an object.
'this avoid having to have a specific cell selected by the user when starting.
'initialize variables which should be set later from user inputs
alpha = 0
beta = 0
xn = 0
yn = 0
xn1 = 0
yn1 = 0
t = 0 ' battle starts at time zero
deltaT = 0.1 'default value to 1 'note that units appear to be in hours for t
iterations = 0
MsgBox ("starting your simulation run")
'start time stepping
While xn >= 1 And yn >= 1 'battle stops when at least one party gets below one troop
alpha = alphaMin + (alphaMax - alphaMin) * Rnd()
beta = betaMin + (betaMax - betaMin) * Rnd()
xn1 = xn - beta * yn * deltaT
yn1 = yn - alpha * xn * deltaT
'update for next step
t = t + deltaT 'keep track of how much time has passed
xn = xn1
yn = yn1
If (t > MAX_TIME) Then 'guard against "infinite" looping
MsgBox ("time exceeded: simulation is not complete...quitting")
Exit Sub
End If
'put data into the Excel sheet - writing at the end excludes initial values
With dataOutput 'everything after the dot refers to the dataOutput object - saves typing
.Offset(iterations, 0) = t
.Offset(iterations, 1) = xn
.Offset(iterations, 2) = yn
.Offset(iterations, 3) = alpha
.Offset(iterations, 4) = beta
End With
iterations = iterations + 1
Wend
'present results to user and save the file
MsgBox ("X ending: " & xn & vbNewLine & "Y ending: " & yn & vbNewLine & _
"end time: " & t)
ActiveWorkbook.Save
'NOte that plotting is still not required
Application.ScreenUpdating = True
MsgBox ("simulation complete")
Exit Sub 'do not envoke error handler unless an actual error occurs
GENERIC_ERROR:
Application.ScreenUpdating = True
MsgBox ("Code has some sort of error: " & Err.Number & vbNewLine & _
"x, y " & xn & " " & yn & vbNewLine & "t, iterations " & t & " " & iterations & _
vbNewLine & vbNewLine & "Error # " & Err.Number & " " & Err.Description)
Exit Sub
PLANNED_EXIT:
Application.ScreenUpdating = True
Exit Sub
End Sub
答案 0 :(得分:0)
我猜你是在追求以下内容:
Open myFile For Input As #FileNum
While Not EOF(FileNum)
Input #FileNum, xn, yn, alphaMin, alphaMax, betaMin, betaMax, deltaT
' your code to do the math with current seven variables
Wend
Close #FileNum