Sage

I haven’t posted here in a while mostly because I have been preoccupied with calculus 2; between it and my other classes I don’t have a lot of time to code. However I found an intersection between calculus and coding with one of my assignments. Charged with determining if a ball could be caught by a person riding a ferris wheel, I decided to write a Sage Notebook application that animates the two trajectories with relation to time. The result is a little rough (as is my python), but it does work.

# set the global variable
a2,t,L1,L2,min_d= var('a2,t,L1,L2,min_d')

# default high minimum distance
min_d = 1000

# these fuctions return the x's and y's
def x1(t): return 10*t
def y1(t): return 10*sqrt(3)*t-4.9*(t^2)

def x2(t): return 25+7*cos((6+t)*(-pi/6))
def y2(t): return 7+7*sin((6+t)*(-pi/6))

# two lines from the functions
L1 = parametric_plot( (x1(t), y1(t) ), (t, 0, 6), color='green')

#L2 = parametric_plot( (x2(t), y2(t) ), (t, 0, 12))
L2 = circle( (25,7),7 )

# Calclulate the distance between the points
def distance(a):
d = float( sqrt( ( x2(a)-x1(a) )^2 + (y2(a)-y1(a))^2 ) )
return d

# record minimum distance
def min_distance(distance) :
global min_d
if (distance < min_d) :
min_d = distance

# find the points at 't' on the two lines
def f(a):
p1 = point( ( x1(a), y1(a) ) )
p2 = point( ( x2(a), y2(a) ) )

#calculate distance
d = distance(a)

# record minimum distance
min_distance(d)

return L1 + L2 + p1 + p2 + text("Distance: "+ str(distance(a)),(30,16)) + text("Min Distance: "+ str(min_d),(6,16))

# the animation
a2 = animate([f(i) for i in srange(2.1,6,0.1)], xmin = 0, xmax = 35, ymin = 0, ymax = 16)

# the slider and animation checkbox
@interact
def _(Time=(0,6), animate_it = checkbox(False, "Animate it")):

if (animate_it == True) :
a2.show(20,1)
else :
show(L1 + L2 + point( (x1(Time), y1(Time)) )+ point( (x2(Time), y2(Time)),xmin = 0, xmax = 35, ymin = 0, ymax = 16 )+ text("Distance: " + str(distance(Time)),(30,16)))

parametric trajectories
parametric trajectories with respect to time

 
you can see the application in action here

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s