Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168695
Image: ubuntu2004

Ball motion with Sage

Jose Guzman(*) and Ignacio Delgado

version 1.0.1

December 09, 2009

(*) This file is under construction. Please send any suggestions or comments to [email protected]


 

Index

  1. Introduction
  2. Ball motion equation
  3. Solving the equation with Sage
  4. Graphic representation

1. Introduction:

We will consider the equation to describe the vertical motion of a ball, and analyze and solve with Sage. Code is extensively commented through the notebook and should be self-explanatory. Comments and suggestions are greatly appreciated.

 

2. Ball motion equation:

From Newton's second law of motion we can write a mathematical formulation of a ball motion. With this equation we can calculate the ball's  vertical position (y) according to the time as follows:

y(t)=Vot12gt2y(t) = V_ot -\frac{1}{2}gt^2

where yy is the vertical distance, VoV_o is the initial velocity of the ball, gg is the acceleration of gravity, and tt is time.

# define equation var('t,v0,g') # symbolic variables y(t)=v0*t-(g*t**2)/2 # ball motion equation #result = y.subs(v0=5,g=9.81,t=1) #print result.pyobject() # we need pyobject() to get the value

3. Solving the equation with Sage:

We can look for for solutions to the equation a y=0, to know the time it takes for the ball to move upwards and returns to y again.

Vot12gt2=0;t(Vo12gt)=0V_ot -\frac{1}{2}gt^2 =0 ; t(V_o -\frac{1}{2}gt) = 0

We have two possible solutions, corresponding to

t=0t=0 or t=2Vo/gt=2V_o/g

We can easily solve this with Sage.

solve(y==0,t) # solve equation y=0 with respect to t
[t == 2*v0/g, t == 0]

We can calculate the maximal vertical distance reached by simply taking the derivative of the function and solve it against time.

dydt=Vogt{\displaystyle\frac{dy}{dt}=V_o -gt}

Once Again, Sage allows us to perform this simple calculation.

diff(y,t) # calculate dy/dt
t |--> -g*t + v0

If we equal the equation to zero.

dydt=Vogt=0:{\displaystyle\frac{dy}{dt}=V_o -gt=0 :}

 we can calculate the time at which the maximum vertical distance is reached

t=Vo/gt=V_o/g

In Sage, we simply solve the differential equation with respect to t.

solve(diff(y,t)==0,t)
[t == v0/g]

4. Graphic representation:

We can plot it now all together.

import numpy as np class Hyperbola(): def __init__(self, velocity, color): """ creates a hyperbolic curve of the ball motion at a given initial velocity. Arguments: velocity -- initial velocity color -- color to plot the curve """ # public variables #f(v) = v*t - (g*t**2)/2 v,t=var('v,t') # symbolic variables g=9.81 self._v = velocity self._color = color self.f = velocity*t - (g*t**2)/2 # this only depends on t now self._tend = 2*velocity/g self.__g=9.81 self._tmax = self._v/self.__g def maxcoor(self): """ returns x,y coordinates of the point at the maximum """ ymax = self.f(self._tmax) return (self._tmax, ymax) def plot(self): """ creates a plot object with the curve, label, maximun point and the coordinates of the max point """ curve = plot(self.f,0,self._tend,rgbcolor=self._color) circle = point(self.maxcoor(), rgbcolor='white', pointsize=30,faceted=True) location = (self._tmax,self.f(self._tmax)*1.2) label = text("V=%s m/s"%self._v, location,rgbcolor=self._color) return curve + circle + label # plot functions # create some objects myfunc = Hyperbola(velocity=6,color='orange') #print myfunc.plot() #print myfunc.maxcoor() figure = myfunc.plot() v=[2,3,4,5] # list of velocities colors = ['red','green','black','blue'] # list of colors for i,j in zip(v,colors): myfun = Hyperbola(velocity=i, color=j) figure += myfun.plot() figure.fontsize(14) figure.show(xmax=1.50)