This code simulates oscillation of mass on a spring and graphs that motion. To use this code, just start a new project, place a picture box on the form, paste the code into the form's Load event, and run the program.
Code:'set up the display------------------------------ Form1.ScaleMode = 3 With Picture1 .Appearance = 0 .BorderStyle = 0 .AutoRedraw = True .Width = 257 .Height = 257 .ScaleMode = 0 .ScaleWidth = .Width .ScaleHeight = -.Height .ScaleLeft = 0 .ScaleTop = .Height \ 2 End With 'run the simulation------------------------------ Dim f As Double 'net force acting on mass Dim a As Double 'acceleration Dim v As Double 'velocity Dim p As Double 'position Dim k As Double 'spring constant Dim d As Double 'damping factor Dim m As Double 'mass Dim i As Double 'time increment Dim t As Double 'time p = 100 k = 0.1 d = 0.15 m = 2 i = 0.01 For t = 0 To 257 Step i f = (-p * k) + (-v * d) a = f / m v = v + a * i p = p + v * i Picture1.PSet (t, p) Next t


Reply With Quote