Fundamentals of Iterative Prediction with Varying Force: Difference between revisions
Line 19: | Line 19: | ||
===A Computational Implementation=== | ===A Computational Implementation=== | ||
Code for an implementation of varying force iterative prediciton can be found [https://colab.research.google.com/drive/1617NfftFpRj7BiZZFJZDI731MHC-J_q4 | Code for an implementation of varying force iterative prediciton can be found in [https://colab.research.google.com/drive/1617NfftFpRj7BiZZFJZDI731MHC-J_q4 this Google colaboratory jupyter notebook]. The following code snippet covers the physics calculation portions | ||
<code>def calcmotion(self): #this is the method of the class which does the actual computation | |||
#First, we want to know how many steps we will calculate, and round this to an integer | |||
#Ideally, the user will make dur%tstep = 0, but we can't be sure, so we need to sanitize the input | |||
numsteps = int(self.dur/self.tstep) #the use of self. calls these quantities which are defined elsewhere | |||
#Now we make an array to hold all of our data. For more information on numpy arrays, see | |||
#https://docs.scipy.org/doc/numpy/user/basics.types.html | |||
#It will have 5 columns and numsteps number of rows | |||
#The columns are, in order, time, position, momentum, velocity, and force | |||
#np.zeros takes a tuple for the shape of the array, and the optional argument dtype for the data type | |||
#The data type will default to float, which is what we need, so this argument is not passed | |||
self.data = np.zeros((numsteps,5),dtype=float) | |||
#Now we perform the actual work of iterating: | |||
for i in range(numsteps): | |||
#Here we compute the time stamp of each step, but considering the iteration number and the timestep width | |||
self.data[i,0] = i*self.tstep | |||
#If this is the first step, we set our initial position and momentum | |||
if i ==0: | |||
self.data[i,1] = self.xinit | |||
self.data[i,2] = self.pinit | |||
#Otherwise, we use kinematics to compute a new position, and the momentum principle to compute a new momentum | |||
else: | |||
#xfinal = xinitial + v*delta t | |||
self.data[i,1] = self.data[i-1,1] + self.data[i-1,3]*self.tstep | |||
#pfinal = pinitial + F*delta t | |||
self.data[i,2] = self.data[i-1,2] + self.data[i-1,4]*self.tstep | |||
#We compute velocity using v = p/m | |||
self.data[i,3] = self.data[i,2]/self.mass | |||
#Finally, we compute the force at our new position | |||
self.data[i,4] = self.forcefunc(self.data[i,0],self.data[i,1],self.data[i,2]) | |||
return self.data | |||
</code> | |||
==Examples== | ==Examples== |
Revision as of 13:53, 31 May 2019
It is rare to have a force which is perfectly constant, and iterative analysis of more realistic varying-force systems is substantially more complicated. A toy model demonstrates how programs may be written to analyze these systems.
Main Idea
The physics of iterative prediction with varying force is the same as for prediction with constant force, but it is necessary to generalize the mathematical expressions, which adds complexity to the code.
A Mathematical Model
To begin with, consider a one dimensional force, which may vary with both as a function of time and/or dependent variables such as position and velocity. Then we write this as [math]\displaystyle{ F(t,x,v) }[/math]. Now, using the momentum principle, we know that [math]\displaystyle{ F = \frac{\text{d}p}{\text{d}t} }[/math], which in discrete terms is [math]\displaystyle{ \Delta p = F\Delta t }[/math].
Just as with a constant force, this lets us write out for some iteration at [math]\displaystyle{ (t_0,x_0,v_0) }[/math] that
[math]\displaystyle{ p_{final} = p_{initial} + F(t_0,x_0,v_0)\Delta t }[/math]
Which we combine with kinematics to produce a new set of variables [math]\displaystyle{ (t_1,x_1,v_1) }[/math]. The difference we now have is that whereas before [math]\displaystyle{ F(t_0,x_0,v_0) = F(t_1,x_1,v_1) }[/math], we must now recalculate [math]\displaystyle{ F(t_1,x_1,v_1) }[/math] using the relevant formula. This will take the form of an extra step in each iteration. It is important to note that although we write the force as a function of all of these variables, in most cases it will only depend upon one of them. In a spring, for example, we will see that [math]\displaystyle{ F(t,x,v) = F(x) }[/math], meaning that only the position is necessary to compute the force.
A Computational Implementation
Code for an implementation of varying force iterative prediciton can be found in this Google colaboratory jupyter notebook. The following code snippet covers the physics calculation portions
def calcmotion(self): #this is the method of the class which does the actual computation
#First, we want to know how many steps we will calculate, and round this to an integer
#Ideally, the user will make dur%tstep = 0, but we can't be sure, so we need to sanitize the input
numsteps = int(self.dur/self.tstep) #the use of self. calls these quantities which are defined elsewhere
#Now we make an array to hold all of our data. For more information on numpy arrays, see
#https://docs.scipy.org/doc/numpy/user/basics.types.html
#It will have 5 columns and numsteps number of rows
#The columns are, in order, time, position, momentum, velocity, and force
#np.zeros takes a tuple for the shape of the array, and the optional argument dtype for the data type
#The data type will default to float, which is what we need, so this argument is not passed
self.data = np.zeros((numsteps,5),dtype=float)
#Now we perform the actual work of iterating:
for i in range(numsteps):
#Here we compute the time stamp of each step, but considering the iteration number and the timestep width
self.data[i,0] = i*self.tstep
#If this is the first step, we set our initial position and momentum
if i ==0:
self.data[i,1] = self.xinit
self.data[i,2] = self.pinit
#Otherwise, we use kinematics to compute a new position, and the momentum principle to compute a new momentum
else:
#xfinal = xinitial + v*delta t
self.data[i,1] = self.data[i-1,1] + self.data[i-1,3]*self.tstep
#pfinal = pinitial + F*delta t
self.data[i,2] = self.data[i-1,2] + self.data[i-1,4]*self.tstep
#We compute velocity using v = p/m
self.data[i,3] = self.data[i,2]/self.mass
#Finally, we compute the force at our new position
self.data[i,4] = self.forcefunc(self.data[i,0],self.data[i,1],self.data[i,2])
return self.data