Introduction To Discretization
Introduction To Discretization
Introduction To Discretization
Today we begin learning how to write equations in a form that will allow us to produce numeri-
cal results. In introductory physics courses, almost all the equations we deal with are continuous
and allow us to write solutions in closed form equations. However, in more advanced physics, it
becomes necessary to be able to solve equations numerically. Discretization is the name given
to the processes and protocols that we use to convert a continuous equation into a form that can
be used to calculate numerical solutions.
Let' s start with some very simple examples. Suppose I want to model the motion of an object
traveling at constant speed in one direction. For specificity, let' s say an object is traveling along
x t = x0 + 10 t
the + x direction with a speed of 10 m/s, we would write its position vector as :
where x0 is the position of the particle at time t=0. It is trivial to plot this motion, or to compute
x(t) for any time t. But let's see how we would discretize this very simple equation. Discretiza-
tion means we consider the motion to occur in discrete packets, and we seek to model a way of
describing where the position of the particle at the nth position. This procedure mimics the way
you take data in a laboratory; you can't, despite your best intentions, take data at every possible
value of time or position; rather, you take data at specific points or moments in time.
We approach the problem with this sort of reasoning : if the particle' s current position is x[n],
then in the next time frame, it will be at position x[n + 1]. These two positions are related by the
motion of the particle between the two times, so we can write :
n + 1st position = nth position + motion of particle between xn and xn + 1
We know that the particle will travel a distance of 10 Dt for a time interval of Dt, so that we can
write:
xn + 1 = xn + 10 Dt
Meaning the current position is equal to the last position plus motion since the last position
2 discrete.nb
And with this formulation, we can determine the position of the particle at any time t. Let' s
write this in Mathematica form, noticing which variables we have to define :
Clearx, n, h, v
v = 10; h = 0.01;
x0 = 0;
xn_ := xn = xn - 1 + v h
x10
1.
In the code above, what are the meaning of the variables? What do v, h, and x[10] represent?
What is the impact of setting x[0]=0?
Let' s see how we can plot data in this form; this will introduce us to two new and useful com-
mands : the Table and ListPlot commands :
ListPlotTablexn, n, 50
5
10 20 30 40 50
This plots the first 50 values of x[n] vs. n. Does n exactly represent time, or is n merely related
to time (or is n completely unrelated to the passage of time?)
Suppose now we want to analyze a situation where the velocity is not constant, for instance in
the case of an object falling in the earth' s gravitational field under the force of gravity. We
know that the velocity increases linearly with time of flight, or in equation form : v (t) = g t
(where we will assume the downward direction is positive). Let' s consider the following code :
discrete.nb 3
Cleary, h, v, g
g = 9.8; h = 0.01; y0 = 0; v0 = 0;
vn_ := vn = vn - 1 + g h
yn_ := yn = yn - 1 + vn - 1 h
ListPlotTableyn, n, 500
120
100
80
60
40
20
For the input values, what is the time interval described in the plot above? Let' s see how our
discrete model compares with the solution we expect?
1 2 g 5^2 y500
1.002
Pretty close but not exact. Our two answers are off by 0.2 %. What might be causing even this
small amount of error?
Let' s see what happens if we amend the code slightly :
4 discrete.nb
Cleary, h, v, g
g = 9.8; h = 0.01; y0 = 0; v0 = 0;
vn_ := vn = vn - 1 + g h
yn_ := yn = yn - 1 + 1 2 vn + vn - 1 h
ListPlotTableyn, n, 500
120
100
80
60
40
20
1 2 g 5^2 y500
100 200 300 400 500
1.
What did we do that was different, how else might we have reduced our error?
The Harmonic Oscillator
One of the most frequently studied systems in physics is the harmonic oscillator. In the two
systems considered above, the acceleration of the system was constant (a = 0 or a = g). In the
harmonic oscillator, the acceleration varies with the position of the particle.
d2 x -k
= x
dt2 m
or in "dot" notation :
.. k
x = - x
m
dx d2 x
x° =
..
and x =
dt dt2
Now, we know that the solutions to the harmonic oscillator problems are sin and cos. This makes sense if you
consider the differential equation in eq. (1). This equation relates the second derivative of a function to the
negative of the original function (times a constant). What functions do we know that when differentiated
twice, return the negative of the original function? And the answer, as you learned in intro calc, are the sin
and cos functions. Recall that :
d d2 sin x d
sin x = cos x; = cos x = - sinx
dx dx dx
Clearx, v, h, k, m
x0 = 0; h = 0.01; v0 = 10; k = 1111.11; m = 100;
ax_ := -k m x;
vn_ := vn = vn - 1 + axn h
xn_ := xn = xn - 1 + h vn - 1 + vn - 1 2
ListPlotTablexn, n, 566
-1
-2
-3
Let' s look at the output and see if it matches what we expect. If we have properly modeled this
system, we expect to reproduce the motion of a harmonic oscillator of mass 100 kg and spring
constant 1111.11 Nm (why the odd choice for k?). First, we expect to observe periodic motion,
and the graph above (which has the x position on the vertical axis and the value of n on the
horizontal axis. Remember, n is related to, but not exactly equal to the time elapsed. But is this
the correct representation of the motion of this system?
We can check our numerical results against the results of analytic computations. We know, for
instance, that the period of a harmonic oscillator is given by:
This means that it should take 3 x .1884 s = 5.652 s to complete three cycles. Below is a slightly
different plot of this motion. Notice how the ListPlot[Table[... command now includes two
arguments; we are plotting the quantity n h on the x axis and x[n] on the y axis. Remember that
n is the value of the time step, and h is the duration of each time step, therefore, the quantity n h
is simply the time elapsed. Notice also that we are plotting the first 566 n values (or the first 566
snapshots of the motion of the oscillator); if each time step has a value of 0.01 s, then the total
time plotted is 5.66 seconds. If you look carefully, you can see that the model predicts just
slightly more than 3 cycles will be completed in 5.66 s, in good agreement with our analytical
calculation.
1 2 3 4 5
-1
-2
-3
We can also compare the amplitude of the predicted motion with the results of calculation. Our
model assumes that x = 0 at t = 0, and that v = 10 m/s at t = 0. This means that at t = 0, all the
energy is in the form of kinetic energy, and energy conservation tells us that :
PE + KE = constant = total Energy
In this case, we can write :
1 1
k x2 + m v2 = E
2 2 (2)
When x = 0, all the energy is kinetic; when x is at its maximum distance from equilibrium, all
the energy is potential, if we call this distance xmax, we have:
1
k x2 max =
2
As you can guess, the constants were chosen to produce an integer result; this calculation pre-
dicts the amplitude of motion will be 3 m.Our graph above appears to be in good agreement, but
let' s dive deeper to do more than a visual scan.We have already determined that the period of
one cycle is 1.884 seconds, therefore the time for the particle to move from the origin to its
maximum distance should be 1/4 of a cycle, or 0.47124 seconds.Now, since we are sampling the
position of the particle every 0.01 s, we are predicting that the particle should reach this maxi-
mum amplitude between the 47 th and 48 th time step (remember each time step is 0.01 s in
duration).Thus, let' s check the values of x[n] in the vicinity of n = 47 :
These data indicate the maximum displacement occurs just after 0.47 seconds, and the amplitude
is slightly greater than 3, suggesting that our model is fairly accurate, and with a smaller choice
of step size, should approximate the analytical results even more closely.
Testing the effect of step - size :
If I redo the calculation with the step size set = 0.001, we then expect the maximum displace-
ment to reach a value of 3 between x[471] and x[472]. The results of this calculation are :
Block$RecursionLimit Infinity, x470, x471, x472, x473
Phase Diagrams
Let' s look again at equation (2) and ask the question : What should a graph of v (t) vs. x (t) look
like? Let' s construct a plot of v[n] vs. x[n] for one cycle
discrete.nb 9
10
-3 -2 -1 1 2 3
-5
-10
What a beautiful planetary orbit, I mean, ellipse. Why do you think this is the shape of the v (t)
vs. x (t) graph? Notice that v = 10 when x = 0; this is what you expect from simple energy con-
siderations. The particle has maximum velocity when it passes through the equilibrium point.
When the particle is at greatest distance from the origin (x = 3 and - 3), the velocity is zero,
consistent with your understanding of conservation of energy in harmonic oscillators.
This type of v (t) vs. x (t) diagram is called a phase diagram, and is a useful took in analyzing
the nature of dynamical systems.
Adding friction to the fray
Let ' s take our initial system and add a constant amount of friction to the system as
if our mass is sliding on a table that has a constant coefficient of friction. Let '
so that our code becomes notice the change in the definition of acceleration :
s say that friction with the table changes the nature of the acceleration,
10 discrete.nb
Clearx, v, h, c, k, m
x0 0; h 0.01; v0 10; k 1111.11; m 100; c 2;
ax_ : k m x c
vn_ : vn vn 1 axn h
xn_ : xn xn 1 h vn 1 vn 1 2
ListPlotTablexn, n, 266
3
-1
-2
-3
10
-3 -2 -1 1 2
-5
-10