Kineatics and dynaics, a coputational approach We begin the discussion of nuerical approaches to echanics with the definition for the velocity r r ( t t) r ( t) v( t) li li or r( t t) r( t) v( t) t for sall t. This definition t 0 t t 0 t coes fro atheatics, calculus specifically, and does not contain any real physics. Where the physics coes in is through the definition of oentu. We know that p and inverting this and solving for the velocity gives v so if we can find p then we find the position using p v pt () r ( t t) r ( t) v( t) t r ( t) t This tells us how to find the position at the next increent in tie as long as we know the current position and the oentu. So the question becoes how to find the oentu. For this we will use Newton's relation (second law) between the net force on an object and the tie rate of change of the oentu. You ay be used to using F a as Newton's second law, however this is not really the best for. The law actually states that the net force produces a p tie rate of change in the oetu or atheatically: F ultiplying this expression by t t yields p F dt and recognizing that p p( t t) p( t) we can rearrange to this iportant result p( t t) p( t) F t. This expression tells us that if we know the present oentu, at tie t, and the net force on an object, than we can find the oentu at a future tie, t t. Of course, this is only exactly true in the li so the accuracy of the solution will depend on the size of the tie step. Let's use a siple exaple to show how to use these two pt () expressions, p( t t) p( t) F t and r ( t t) r ( t) t allow us to find the trajectory of an object acted on by a force. t 0 F
Start with p(0) 0 and r(0) 0 Use position and oentu update to calculate pt () future positions p( t t) p( t) F t and r ( t t) r ( t) t. Let's set up a chart for each tie step. Note t n t. n t p r 0 0 0 0 1 t F t 0 3 4 5 6 t 3 t 4 t 5 t 6 t F t 3F t 4F t 5F t 6F t F t 3F t 6F t 10F t 15F t 7 7 t 7F t 1F t 8 8 t 8F t 8F t 9 9 t 9F t 36F t 10 10 t 10F t 45F t
Of course, this is a proble that can be solved analytically, we did this in the last lecture and the pt ( ) 1 F solution was given by r( t) r( t ) t t 1 F proble. Using the initial conditions the solution is r() t t have been reoved because it is a one diensional proble. 0 where I have used the variables given in this 0 where the vectors sybols Graphically this looks like this: 50 00 150 100 analytic calculation nuerical calculation 50 0 0 4 6 8 10 1 14 16 18 0 where blue is fro the data above and green is the analytic result. You can see that there is a difference between the two sets of data. The nuerical calculation does not exactly reproduce the analytic result. Recognizing and coprehending the source and agnitude of this difference is very iportant to your understanding of the solution to the proble. Below shows a graph of the absolute error abs _ error analytic _ result coputational _ result (blue) and the fractional error ( analytic _ result coputational _ result) frac _ error (red) analytic _ result Exaining the graph shows that the absolute error increases linearly with tie, but the fractional error decreases. It is often iportant for you to understand whether you care about either the absolute or fractional error ost. Soeties you can deterine this and other you can t.
10 100 80 60 40 absolute error fractional error (%) 0 0 0 4 6 8 10 1 14 16 18 0 There is an abiguity that is worthy of a bit of discussion. In the nuerical solution the two key line are pnew pold F t p??? rnew rold t The question boils down to what oentu do you use for the second expression? This is equivalent to asking in which order do you evaluate these expressions: pnew pold Fold t p new rnew rold t or p pnew pold Fnew t old rnew rold t Do you update the oentu first and then the position using the new oentu or do you update the position first using the old oentu and then the oentu using the new position? Actually, there are a couple ore perutations that you can think of. Soeties these seeingly inor differences in the algorith really ake a significant change to the accuracy of your calculation. Topics such as these coe under the nae of nuerical ethods. In this class we will be doing very little in the way of trying to get the best answer by using the best algorith. However, occasionally this ight coe up and you should be at least aware of the issue.
One siple way of iproving the accuracy of alost any algorith is to reduce the tie increent t. Selection of the proper, or reasonable, t for a proble often involves a trade-off between the necessary accuracy and the length of tie that you can afford to run the siulation. It also involves understanding the natural tie-scales of the physical situation and what approxiations you can ake while still obtaining the inforation that you need to know. Let s exaine a real physical syste. Consider an electron in a vacuu. This is a odel for an electron oving in a vacuu tube, such as an old TV tube (in the days before flat panel screens). To provide a constant force we produce a unifor electric field in the region of space containing the electron. A siple scheatic would be: 1 10 V 0 V The force on the electron is the electric charge (q) ties the electric field, F qe ; in the region between the two plates the field is approxiately constant, therefore so is the force. Consider a siulation tie of 10-10 sec. What should the t be? Let explore this coputationally. Below is a graph of position vs tie with data fro siulations with two different tie steps, large 10-1 sec and sall 10-14 sec. F There is clearly a difference between the two data sets. Which is correct? This proble is one 1 ee of the rare situations where we know the answer analytically, x() t x t assuing the electron starts at r(0) 0,0,0 and v(0) 0,0,0. The parabolic analytic solution is
indistinguishable fro the green dots. However we can look ore precisely by plotting the fractional error, defined above. The siulation with the larger tie step (blue) has an error greater than 3% for all of the tie, whereas using the saller tie step reduces the error well below 1% for alost all of the siulation tie. Since in this proble we know the exact answer we can calculate these errors, generally this is not possible. What do you do then? The siplest thing is to try a tie step, then try a saller one and see how uch your answer changes. If it changes a lot (that eans ore than you would like your error to be) try an even saller tie step and repeat this process until you either have answers to the accuracy that you desire or you run out of coputer power and the siulations take too long. This is when you wish you had a faster coputer and have joined the legions of research scientists wanting ore powerful coputation achines. fro future iport division fro visual iport * fro visual.graph iport * # iport graphing features #Siulation of an electron acted on by a constant force. #The siulation shows two different tie steps and the relative errors. tie = 0 #start the clock at zero sall_dt = 1e-14 #sall tie increent large_dt = 1e-1 #large tie increent siulation_tie = 1e-10 #tie between scattering events c = 3e8 #speed of light /s e = 1.6e-19 #unit of charge electron_ass = 9.11e-31 #ass of electron electron_init_pos = vector(0,0,0) #call origin of syste electron_init_vel = vector(0,0,0) #initial velocity, electron_init_o = electron_ass*electron_init_vel #initial oentu E = vector(10,0,0) #Electric field in x direction, in Volts/eter Electric_Force = -e*e #constant force applied by electric field #graphics position = gdisplay(title='position',
xtitle='tie (sec)', ytitle = 'distance ()', x=0, y=0, width=1000, height=400, background= color.black) #window for position error = gdisplay(title='percent Error', xtitle='tie (sec)', ytitle = 'factional error (%)', x=0, y=400, width=1000, yax=10, height=400, background= color.black) #window for error pos_large_step = gdots(gdisplay=position, color=color.cyan) # a graphics curve for large pos_sall_step = gdots(gdisplay=position, color=color.green) # a graphics curve for sall #steps analytic = gcurve(gdisplay=position, color=color.red) # a graphics curve for exact #steps delta_sall_dot = gdots(gdisplay=error, color=color.green) # error for sall steps delta_large_dot = gdots(gdisplay=error, color=color.cyan) # error for large steps #initialize all loop variables electron_o_large = electron_init_o electron_o_sall = electron_init_o electron_pos_large = electron_init_pos electron_pos_sall = electron_init_pos tie_sall = tie tie_large = tie #ain loops (typically only one but this progra used two different tie steps while (tie_large< siulation_tie): #one nanosec of acceleration electron_o_large = electron_o_large + (Electric_Force)*large_dt #update electron oentu electron_pos_large = electron_pos_large + (electron_o_large/electron_ass)*large_dt #update electron position analytic_pos = electron_init_pos + electron_init_vel*tie + 0.5*(Electric_Force/electron_ass)*(tie_large)** pos_large_step.plot(pos=(tie_large, electron_pos_large.x )) # plot position if(tie_large > large_dt): #can't easure rel error at t = 0, delay one tie step delta_large = 100*abs((analytic_pos.x - electron_pos_large.x)/analytic_pos.x) #find error delta_large_dot.plot(pos=(tie_large, (delta_large))) #plot percent error tie_large = tie_large + large_dt #increent tie by large step while (tie_sall< siulation_tie): #one nanosec of acceleration electron_o_sall=electron_o_sall + (Electric_Force)*sall_dt #update electron oentu electron_pos_sall = electron_pos_sall + (electron_o_sall/electron_ass)*sall_dt #update electron position analytic_pos = electron_init_pos + electron_init_vel*tie + 0.5*(Electric_Force/electron_ass)*(tie_sall)** pos_sall_step.plot(pos=(tie_sall, electron_pos_sall.x )) # plot position if(tie_sall > sall_dt): #can't easure rel error at t = 0, delay one tie step delta_sall = 100*abs((analytic_pos.x - electron_pos_sall.x)/analytic_pos.x) #find error delta_sall_dot.plot(pos=(tie_sall, (delta_sall))) #plot percent error analytic.plot(pos=(tie_sall, analytic_pos.x )) #plot classical result
tie_sall = tie_sall + sall_dt #increent tie by sall step print electron_o_sall/(electron_ass*c) #check to see if velocity is uch less than c, classical liit print "average velocity", electron_pos_sall/tie_sall
fro future iport division fro visual iport * fro visual.graph iport * # iport graphing features fro rando iport rando, unifor # iport rando nuber functions #Siulation of an electron acted on by a constant force and suffering #elastic and inelastic scattering. Drude odel for electron conduction. tie = 0 elastic_tie = 0 inelastic_tie = 0 count = 0 dt = 5e-16 # start the syste clock at zero # tie since last elastic collision # tie since last inelastic collision # count nuber of cycles in loop # tie increent (sec) siulation_tie = 1e-10 #tie of siulation elastic_scattering = 1e-14 #tie between elastic scattering events inelastic_scattering = 1e-13 #tie between inelastic scattering events c = 3e8 #speed of light /s two_pi = *3.14159 e = 1.6e-19 #unit of charge electron_ass = 9.11e-31 #ass of electron electron_init_pos = vector(0,0,0) #call origin of syste electron_init_vel = vector(0,0,0) #initial velocity, electron_init_o = electron_ass*electron_init_vel #initial oentu E = vector(10,0,0) #Electric field in x direction, in Volts/eter Electric_Force = -e*e #constant force applied by electric field #graphics position = gdisplay(title = 'Position vs tie', xtitle = 'tie', ytitle = 'x position', x=0, y=0, width=1000, height=400, background= color.black) # window for position vs. tie pos_vs_tie = gdots(gdisplay=position, color=color.cyan) # a graphics curve for large steps #initialize all loop variables electron_o = electron_init_o electron_pos = electron_init_pos #ain loop while (tie < siulation_tie): #length of tie to siulate electron_o = electron_o + (Electric_Force)*dt #update electron oentu electron_pos = electron_pos + (electron_o/electron_ass)*dt #update electron position if count % 10 == 0: # only plot every n'th point pos_vs_tie.plot(pos=(tie, electron_pos.x )) # plot x position vs tie if(elastic_tie > elastic_scattering): #is it tie for an elastic collision? elastic_tie = 0 #reset elastic tie clock #randoize electron oentu electron_o = rotate(electron_o, angle = unifor(0,two_pi), axis = (0,0,1)) if(inelastic_tie > inelastic_scattering): # is it tie for an inelastic collision? inelastic_tie = 0 #resent inelastic tie clock electron_o = vector(0,0,0) #reset electron oentu to zero #print "inelastic scatter" tie = tie + dt # increent syste tie by large step
elastic_tie = elastic_tie + dt # increent tie since last elastic collision inelastic_tie = inelastic_tie + dt # increent tie since last inelastic collision count = count + 1 # total nuber of cycles #end ain loop print electron_o/(electron_ass*c) #check to see if velocity is uch less than c, print "average velocity", electron_pos/tie