Microsoft Small Basic

Program Listing: JDR564
' Spring 0.1
' Copyright (c) 2012 Nonki Takahashi. All rights reserved.
'
' History:
' 2012/10/13 Created.
'
GraphicsWindow.Title = "Spring 0.1"
scale = 3139 ' [dot/m]
l = 0.05 ' spring length [m]
k = 20 ' spring constant [N/m]
m = 0.01 ' mass of weight [kg]
d = 0.01 ' diameter of weight [m]
g = 9.8 ' acceleration of gravity [m/(s^2)]
xs = 0.1 ' stationary point from left [m]
ys = 0 ' stationary point from top [m]
InitWeight()
x0 = xs - d / 2
y0 = ys + l
v0 = 0
dt = 0.1 ' [s]
y = y0 + 0.05
v = v0
ShowWeight()
While "True"
dv = (m * g - k * (y - y0)) * dt
v = v + dv
dy = (2 * v + dv) * dt / 2
y = y + dy
ShowWeight()
Program.Delay(100)
EndWhile

Sub InitWeight
GraphicsWindow.PenColor = "DimGray"
spring = Shapes.AddLine(0, 0, 0, l * scale)
Shapes.Move(spring, xs * scale, ys * scale)
GraphicsWindow.PenWidth = 0
GraphicsWindow.BrushColor = "Black"
weight = Shapes.AddEllipse(d * scale, d * scale)
EndSub

Sub ShowWeight
' param y
Shapes.Move(weight, x0 * scale, y * scale)
Shapes.Move(spring, xs * scale, (ys + (y - y0) / 2) * scale)
Shapes.Zoom(spring, 1, (l + y - y0) / l)
Shapes.SetOpacity(spring, 100 * l / (l + y - y0))
EndSub