Parabola
Investigating whether the Julia language and the Luxor library are a good combination for modelling examples drawn from physics. So on this page we take the very simple example of a ball thrown up into the air.
Code
using Luxor
# Setup data
halfg = 4.9 # We need the quantity 0.5 * g in the parabola formula
u = -10 # Initial upward velocity in m/s
c = -1.5 # Height from which the ball is thrown
v = 2 # Initial horizontal velocity in m/s
timeRange = 0: 0.2 : 10 # Drawing shows ball at times from 0s to 10s in 0.2s intervals.
pts = Point.( [(v*t, t^2 + u*t + c) for t in timeRange] ) # Positions of the ball
# Create drawing
Drawing(600, 600, "../images/Parabola.svg")
translate(100, 550) # Across and down so origin is near lower left corner
scale(20, 20) # Zoom in by factor of 20 so height reached and distance travelled fit into picture.
fontsize(1) # Font needs to be small so that zooming in doesn't make it enormous.
dotRadius = 0.25 # Radius of circles used to show ball's positions, ditto why it is small.
# x-axis, tickmarks, labels and title:
sethue("gray")
line(O, Point(22,0), :stroke)
xticks = [ (string(x), Point(x,0)) for x in 5:5:20]
(q -> label(q[1], :S, q[2], offset=0.5, leader=true)).(xticks)
text("Metres", 10, 2.2)
# y-axis, tickmarks, labels and title:
line(O, Point(0,-25), :stroke)
yticks = [ (string(-y), Point(0,y)) for y in -5:-5:-25]
(q -> label(q[1], :W, q[2], offset=0.5, leader=true)).(yticks)
text("Metres", Point(-2.2, -10), angle=-pi/2)
# Draw the parabola and output the image:
sethue("green")
circle.(pts, dotRadius, :fill)
finish()
Physics
The diagram uses gravitational acceleration, initial velocity etc. using Newton's laws. The equations are: \begin{align} \ddot{y} &= g \\ \dot{y} &= gt + u \\ y &= \frac{1}{2}gt^2 + ut + c \end{align}
I found it useful to use a spreadsheet to choose values that show up well in a diagram of this sort. e.g. It enabled me to find a suitable choice of the time interval between data points given sensible choices of u and c. Perhaps with more familiarity with Julia and Luxor I could have done the same thing without the spreadsheet.
Discussion
It is an important part of this exercise that the dots indicate the path of an object. It does this by showing that some parts of the trajectory happen more slowly than others. This is indicated by dots close together where the object is moving slowly and further apart where it is travelling more quickly. The dots are images taken at regular intervals of time.
Observations on the Code
As always in broadcasting, the parameter list of the Point constructor is given an array. This array is constructed using array comprehension and the sequence notation a:b:c.
An important observation is that there is an overload of the Point constructor which takes a tuple. This supports the use of the array of tuples created by the array comprehension. (There is another overload of Point which takes two separate numbers).
I tried using the construction of the Points inside the construction of the circles. It works but the profusion of round and square brackets looked confusing to me so I separated them out. Also, I think the separation of the physics, in the first part of the code, and the presentation in the second part is helpful in keeping these two conceptually separate things apart.
After some experimenting it seems easiest to me to keep the default orientation of the y-axis in Luxor: this has the negative part going up the screen and the positive part going down the screen. This can be reflected in the x axis so that it is the usual mathematical way up but text is then upside down.
In practice it turns out to be fairly simple to model the physics using the upside down axis. However, in this case at least, it is then entirely legitimate to label the y axis with positive numbers. The coding ensures that the correct image is created and would have been exactly the same if computed using a graphics library that had the y axis the right way up.
Some care has to be taken when using the default values of functions. For example, the offset in a label is presumably fine when we are drawing in, say, a 600 * 600 picture size, but it is much too big after we have scaled up by 20.