Grokking Glileo's Physics III --Python Code

©Fernando Caracena 31 August 2012

Python genrated figures

The figures used in the previous session were generated using an interactive version of python (ipython). For those familiar with, or learning the language, the following snippets of code are provided for generating each figure.

Acceleration versus Time

ipython --pylab
from pylab import *
from matplotlib.font_manager import FontProperties
from matplotlib.backends.backend_agg import RendererAgg

figure()
#--------------------------------------------
a=0.5
x=arange(0,0.5, 0.01)
y=0.*x+a
x=concatenate((x,x[::-1]))
y=concatenate((0*y,y[::-1]))
p=fill(x, y, facecolor='g')
x1=[0,1]
y1=[1,1]
y2=[0.0,0.0]
plot(x1,y1, x1, y2, color='k')
text(0.501,0.5,r'a')
xlabel('Time', color='k')
ylabel('Acceleration', color='k')
title("Acceleration vs. time", color='k')

#---------------------------------------------------------------------------

# N.B. that the lead character comments out the whole line.

Velocity versus timefigure()

t=arange(0,0.51, 0.01)
v=a*t
t=concatenate((t,t[::-1]))
v=concatenate((0*v,v[::-1]))
p=fill(t, v, facecolor='g')
xlabel('Time', color='k')
ylabel('Velocity', color='k')
title("Velocity vs. time", color='k')

#-----------------------------------------------------------------------------------------------

figure() #Generates a new active window, in which is plotted a new figure.

Distance versus time

t=arange(0,0.51, 0.01)
y=0.5*a*t*t
t=concatenate((t,t[::-1]))
y=concatenate((0*y,y[::-1]))
p=fill(t, y, facecolor='g')
t1=[0,1]
y1=[0.06,0.06]
y2=[0.0,0.0]
# plot(x1,y1, x1, y2, color='w')
xlabel('Time', color='k')
ylabel('Distance', color='k')
title("Distance vs. time", color='k')

#--------------END--------------------------------------------------------------------------------------

About python

Python is a good scripting language, sometimes used by web developers, which through its various add-on graphics libraries becomes a powerful graphics tool. Although it is interpreted, rather than compiled, it runs fast on the modern desktop and laptop.

This entry was posted in frame of reference, mathematics, physics, programming, python and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *