Units and the Harmonic Oscillator
Purpose of this post
This post serves as an introduction to the third part of the SICM book. That part covers Hamiltionian Mechanics. Already the first examples there are hard to understand. This post builds things up from first principles. It is intended as a preamble for understanding this third part of the SICM book. It also is an enlightning remark on physical units. However, a solid background in classical Mechanics is required.
You can look at those code snippets of the book also in graphical form. For this, it is best to first read the post Competence before Comprehension.
The Compiler
We import the html file for part 3 as an IFrame (click here to see the full page). From it, we load the compiler which is used to execute the code snippets of this post here.
Time, Space and Momentum
Any timestamp is measured in seconds. We define the specific timestamp of two seconds.
(define secTWO (* 2 'sec))
Any coordiante-point is measured in meters. We define a specific coordinate-point.
(define mtrSEVEN (* 7 'meters))
Any momentum is measured in sparks. It is a made up unit, think of sparks in the fire box of a steam train. We define a specific momentum.
(define spkFIVE (* 5 'sparks))
Any state is given as an up-tuple. We define a specific state
(define upSVEN (up secTWO mtrSEVEN spkFIVE))
We can get the cooridnate out of a state
(coordinate upSVEN)
We can get the momentum out of a state
(momentum upSVEN)
The stength of some spring is measured in groans. It is a made up unit, think of the groans in a fitness center when people bend springs and such. We define a specific spring with the strength of four groans.
(define grnSPRING (* 4 'groans))
Any body's resistance to movement is measured in grams. This resistance is also called mass. We define a specific body with the resistance of nine grams.
(define grmBALL (* 9 'grams))
Energy
Any energy is measured in hams. To picture one unit of ham, think of the amount of calories a sandwich has. Any Hamiltonian is given as a function that takes some state and returns an energy. We define a specific Hamiltonian which models our body attached to our spring.
(define (ham<-spring-ball upSTATE)
(let ((q (coordinate upSTATE))
(p (momentum upSTATE)))
(+ (/ (* grnSPRING (square q)) 2)
(/ (square p) (* grmBALL 2)))))
We apply the Hamiltonian to the state
(ham<-spring-ball upSVEN)
The parts of any sum must be of the same unit. Here the unit is hams. Thus we realize that by defining our specific Hamiltonian, we also determined sparks in terms of grams and hams.
(define sparks (sqrt (* 'hams 'grams)))
(define upSEVEN (up secTWO mtrSEVEN (* 5 sparks)))
We realize that by defining our specific Hamiltonian, we also determined groans in terms of meters and hams
(define groans (/ 'hams (square 'meters)))
(define (ham<-harmonic upSTATE)
(let ((q (coordinate upSTATE))
(p (momentum upSTATE)))
(+ (/ (* (* 4 groans) (square q)) 2)
(/ (square p) (* grmBALL 2)))))
(ham<-harmonic upSEVEN)
Frequency
Any frequency is measured in cycles.
(define cycles (/ 1 'sec))
We define the specific frequency.
(define cylTHREE (* 3 cycles))
We define a coordinate-function that takes some timestamp and returns a coordinate-point
(define (mtr<-cosine secT)
(* (cos (* cylTHREE secT)) 'meters))
(mtr<-cosine secTWO)
We define a momentum-function that takes some timestamp and returns a momentum.
(define (spk<-atan secT) (* (atan (* cylTHREE secT)) sparks))
(spk<-atan secTWO)
State path and Path function
We define a higher order function called state-path. state-path takes some coordinate-function and some momentum-function. state-path returns a function that takes some timestamp and returns a state.
(define ((state-path mtr<-q spk<-p) secT)
(up secT (mtr<-q secT) (spk<-p secT)))
The function that state-path returns is called a path-function. We define a specific path-function.
(define up<-path-cosine (state-path mtr<-cosine spk<-atan))
We show that indeed, like any path-function, up<-path-cosine is a function that takes some timestamp and returns a state
(identity up<-path-cosine)
(up<-path-cosine secTWO)
Derivatives
We take the derivative of the state-path
((D up<-path-cosine) secTWO)
As shown above, we can use our path function to define a specific state.
(define upSIX (up<-path-cosine secTWO))
(coordinate upSIX)
(momentum upSIX)
We apply our Hamiltonian to this state
(ham<-harmonic upSIX)
We show how to take partial derivatives
(((partial 1) ham<-harmonic) upSEVEN)
(((partial 2) ham<-harmonic) upSEVEN)
The state-derivative is a higher order function. It takes some Hamiltonian and calculates the derivative of it. Using that, the state-derivative returns a function that takes some state and returns a path function.
(define ((state-derivative ham<-Hamiltonian) upSTATE)
(up 1
(((partial 2) ham<-Hamiltonian) upSTATE)
(- (((partial 1) ham<-Hamiltonian) upSTATE))))
(state-derivative ham<-harmonic)
((state-derivative ham<-harmonic) upSEVEN)
((state-derivative ham<-harmonic) upSIX)
Hamilton's equations
We show how to apply our Hamiltonian directly to the timestamp using path-function as an intermediary between the two. The composition of a Hamiltonian with a path-function is a simple function of time, i.e. a function that takes a number of seconds and returns a number of hams.
((compose ham<-harmonic up<-path-cosine) secTWO)
((compose (state-derivative ham<-harmonic)
up<-path-cosine)
secTWO)
Hamilton-equations is a higher order function. It takes some Hamiltonian. It returns a function that takes some coordinate-function and some momentum-function and returns a path-function.
(define ((Hamilton-equations ham<-Hamiltonian) mtr<-q spk<-p)
(let ((up<-path (state-path mtr<-q spk<-p)))
(- (D up<-path)
(compose (state-derivative ham<-Hamiltonian)
up<-path))))
(((Hamilton-equations ham<-harmonic) mtr<-cosine spk<-atan) secTWO)
For realizable coordinate- and momentum-functions, the Hamilton-equantions path-function needs to be identically zero. By looking at the previous result, we realize that by defining the Hamilton equations, we also determined hams in terms of meters, grams and seconds. This also implies new sparks and groans. With this step, we derived all our made up units.
(define hams (/ (* (square 'meters) 'grams) (square 'sec)))
(define new-sparks (sqrt (* hams 'grams)))
(define new-groans (/ hams (square 'meters)))
Textbook solution of the harmonic oscillator
We define a new Hamiltonian which does not anymore contain specific numbers. Instead, the body mass is set to any number m
of grams. In the same manner, the spring strength is quantified by the arbitrary number k
.
(define grmM (* 'm 'grams))
(define grnK (* 'k new-groans))
(define (ham<-hamiltonian upSTATE)
(let ((q (coordinate upSTATE))
(p (momentum upSTATE)))
(+ (/ (* grnK (square q)) 2)
(/ (square p) (* grmM 2)))))
We define some new constants and check their units
(define cylW (sqrt (/ grnK grmM)))
(/ cylW (/ 1 'sec))
Shown above is that cylW
indeed is of dimension one over second
, because the quotient is a dimensionless number (remember that k
and m
are just arbitrary numbers). Below the total energy of the oscillator is quantified by the dimensionless number E
.
(define calE (* 'E hams))
(define mtrQNULL (sqrt (/ (* 2 calE) (* grmM (square cylW)))))
(/ mtrQNULL 'meters)
(define spkPNULL (sqrt (* 2 grmM calE)))
(/ spkPNULL new-sparks)
We define new coordinate- and momentum-functions. They are solutions to the harmonic oscillator that can be found in any textbook on classical mechanics.
(define (mtr<-q secT) (* mtrQNULL (sin (* cylW secT))))
(define (spk<-p secT) (* spkPNULL (cos (* cylW secT))))
We show that the energy is conserved (independent of timestamp t) along the path.
(define secT (* 't 'sec))
((compose ham<-hamiltonian (state-path mtr<-q spk<-p)) secT)
We finally show that the path functions are indeed solutions of Hamilton's equations (result is zero for any timestamp t)
(((Hamilton-equations ham<-hamiltonian) mtr<-q spk<-p) secT)