Let
The damped harmonic oscillator can be modeled by the following Differential Equation (DE) which we present as an Initial Value Problem (IVP) since we are including Initial Conditions (IC):
We divide the DE by
which is easier to work with. If we want, we can think of
The letters
are called parameters. Sometimes the values of parameters appearing in a DE can be measured directly. For example, if we go back to our first DE for the damped harmonic oscillator:
the parameter
For example, with the damped harmonic oscillator we might have measured the position of the mass at several different times. This would result in our having a set of data points. One data point
This project. In this project we will estimate the parameters
from data points of the form
Note. The Python program embedded below will generate simulated data for us.
Method of Parameter Estimation.
We will think of the parameters that we want to estimate, the
At each point
We write the solution as
We want to find the
We can think of the DE’s solution,
We calculate
The data point
For each
and the squared error is
Finally, to get the SSE, we sum up the squared errors
Note. The SSE is a a function of
Keep in mind, each time the computer calculates the
Note about SSE. For some general information on the SSE in the context of regression see:
https://mccarthymat150.commons.gc.cuny.edu/units-16/17-correlation-and-regression/
Next, we need an efficient algorithm to find the
Note about GDA. For some general information about the GDA see:
https://mccarthymat501.commons.gc.cuny.edu/gradient-descent-algorithm-gda/
The following Python 3 program uses entirely numerical methods to estimate the parameters
Outline of how the program works. You can accept the defaults, or you can chose values for
its default value is 200, meaning the GDA should iterate itself 200 times.
The program then uses the Euler method to numerically solve the differential equation for the chosen values of
The program applies the GDA to the
The program outputs the GDA estimate for
Then, if the option
Note. The most time consuming part of the program is doing the calculations to produce these graphs. As these calculations are done, Python outputs what percent of the calculations are left to do.
The first is the SSE(c,k) surface with the GDA path superimposed on it. The surface height and GDA path height are scaled by log 10. This is to prevent the huge values of the SSE at some locations from making it impossible to see, in the graph, the details at locations were the SEE is near its minimum. See image below:
Note. All images were produced by the source code embedded at the bottom of this page.
Note that in Python, the above graph can be rotated interactively:
The second graph is the SSE(c,k) surface, again scaled by log 10, shown as a two dimensional contour map. The contours being the level curves of the surface. The GDA path is superimposed on it. See image below:
Note that in Python, sections of the graph can be magnified using the magnifying glass tool. That tool is at the top of the graph window. See video and image below:
Video of Contour Map showing SSE(c,k) and the GDA path in (c, k) space.
In the above image, which is a zoomed in version of the previous image, we see the last points produced by the GDA. Notice how the GDA nicely gets us close to the minimum SSE (the center of that blue polygonal shape) but then starts to zigzag a little. That zigzagging behavior is a common artifact of GDA. To counter that, I have our GDA output the
After the optional 3D and contour graphs, the program will produce a graph showing the data points
The program will finally produce a video of the above graph, showing the evolution the solution x(c,k, t) as the GDA updates its estimates of (c, k). See video below.
Note. Unlike the static graph above, that the video does not graph
In the video below, the up and down motion of the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | # Written by Chris McCarthy March 2022 # numerically finds the parameters c,k that best fit # the harmonic oscillator mx'' +cx' + k = 0 with x(0) = x0 and x'(0) = v0 # to data generated by Python # m = 1 ----- m should be 1 #=========================================== Graphics Note # update anaconda then in Spyder Pref iPython Graphics Backend InLine #=========================================== Online Compiling Note # https://trinket.io/features/python3 python online #=========================================== usual Python packages import numpy as np #from mpl_toolkits import mplot3d from matplotlib import pyplot as plt plt.rcParams.update({ 'font.size' : 22 }) #=================== (global variables) ODE IC x0 = - 1.25 v0 = - 20 #=================== (global variables) ODE params m = 1 # do not change m c = 2 # you can change c and or k k = 16 #======================== (global variables) for GDA starting point (m, c, k) m0 = m / m # DO NOT CHANGE m0 c0 = 1 # you can change c0 and or k0 k0 = 1 GDAiterations = 200 # How many iterations of GDA to do #=================== (global variables) time t start and stop t0 = 0 #time start tf = 6 #time stop #=================== (global variables) Euler time step dt = 0.01 # for Euler # ================= (global variables) GDA learning rate and numerical gradient eta = . 1 # for GDA learning rate h = 0.0001 # for numerical gradient #=================== (global variables) for Euler Method En = np.ceil((tf - t0) / dt).astype( int ) + 1 # how many Euler points to do ET = np.linspace(t0, tf, En) # Euler tn time values # ======================= what to plot Do3DandContourPlot = 1 # 1 = do the 3D plot and Contour Plots #================================== (global variables) for contour map ckGrid = 50 # SSE will be calculated at (c, k) at contourGrid x contourGrid contourLevels = 100 # how many levels to show in the contour map legendLocationContour = 4 # 1 upperRight 2 upperLeft 3 lowerLeft 4 lowerRight #================================== (global variables) Python generated data points NumberOfDataPoints = 200 # number of data points to generate SpreadOfDataPoints = 0.1 # was 0.02 #================================== (global variables) Python generated data points np.random.seed( 5 ) # will create the same random sequence every time # you can change 200 to any pos integer # used to generate random data points # ============================ Functions def ODE(x,v,m,c,k): #mx'' + cx' + kx = 0 ODE as Vector Field return [v, - (c / m) * v - (k / m) * x] # Outputs x value of Euler points def EulerX(fn, x0, v0, m, c, k): n = np.ceil((tf - t0) / dt).astype( int ) + 1 EPX = np.empty(n, dtype = float ) EPV = np.empty(n, dtype = float ) EPX[ 0 ] = x0 EPV[ 0 ] = v0 for i in range ( 0 ,n - 1 ): EPX[i + 1 ] = EPX[i] + fn(EPX[i],EPV[i],m,c,k)[ 0 ] * dt EPV[i + 1 ] = EPV[i] + fn(EPX[i],EPV[i],m,c,k)[ 1 ] * dt return EPX # converts Euler tn and Euler xn to function using linear interpolation def LinearInterpolation(npInArray, npOutArray, InValue): a = np.where(npInArray< = InValue)[ 0 ][ - 1 ] b = np.where(npInArray> = InValue)[ 0 ][ 0 ] if npInArray[a] = = InValue: returnValue = npOutArray[a] else : slope = (npOutArray[b] - npOutArray[a]) / (npInArray[b] - npInArray[a]) returnValue = npOutArray[a] + slope * (InValue - npInArray[a]) return (returnValue) # =============================== Following Code Generates noisy data # correspoding to mx'' + cx' + ks = 0 #============================================================= UseRandomData = 1 if UseRandomData = = 1 : EXX = np.array(EulerX(ODE, x0, v0, m,c,k )) # Euler from model's mck #np.random.seed(1) DataTT = np.random.uniform(t0, tf, NumberOfDataPoints) DataXX = np.empty(NumberOfDataPoints, dtype = float ) for i in range ( 0 , NumberOfDataPoints): DataXX[i] = LinearInterpolation( ET, EXX, DataTT[i] ) + SpreadOfDataPoints * np.random.randn() dataT = DataTT dataX = DataXX print ( 'Number of Data Points generated.' ) print (NumberOfDataPoints) n = len (dataT) # ============================ More Functions # Outputs Sum of the Squared Errors (distance from Euler to Data) def SSE(x0,v0,m,c,k): EPX = np.array(EulerX(ODE, x0, v0, m,c,k )) EPXatTi = np.empty(n, dtype = float ) for i in range ( 0 , n): EPXatTi[i] = LinearInterpolation(ET, EPX, dataT[i] ) return (np. sum ((EPXatTi - dataX) * * 2 )) # for use with 3D plotting. Input mesh c and k, output mesh SSE def SSE_plot(c,k): ArrayShape = np.shape(c) ArrayOut = np.empty(ArrayShape, dtype = float ) for cols in range ( 0 , ArrayShape[ 1 ]): percentLeft = np. round ( 100 * ( 1 - cols / ArrayShape[ 1 ])) print (f '{percentLeft} percent of calculations remaining' ) for rows in range ( 0 , ArrayShape[ 0 ]): EPX = np.array(EulerX(ODE, x0, v0, m,c[rows][cols],k[rows][cols] )) EPXatTi = np.empty(n, dtype = float ) for i in range ( 0 , n): EPXatTi[i] = LinearInterpolation(ET, EPX, dataT[i] ) ArrayOut[rows][cols] = np. sum ((EPXatTi - dataX) * * 2 ) return (ArrayOut) # for use with 2d contour plotting. Input vector c and k, output vector SSE def SSE_plot_pts(c,k): ArrayShape = len (c) ArrayOut = np.empty(ArrayShape, dtype = float ) for j in range ( 0 , ArrayShape): EPX = np.array(EulerX(ODE, x0, v0, m,c[j],k[j])) EPXatTi = np.empty(n, dtype = float ) for i in range ( 0 , n): EPXatTi[i] = LinearInterpolation(ET, EPX, dataT[i] ) ArrayOut[j] = np. sum ((EPXatTi - dataX) * * 2 ) return (ArrayOut) # Calculates numerical gradient in c,k direction def GRADmck2(x,v,m,c,k): ssec = (SSE(x,v,m,c + h,k) - SSE(x,v,m,c - h,k)) / ( 2 * h) ssek = (SSE(x,v,m,c,k + h) - SSE(x,v,m,c,k - h)) / ( 2 * h) return np.array( [ 0 , 0 , 0 , ssec, ssek]) # Does GDA algorithm def GDAmck2(x,v,m,c,k, its): gda_p = np.empty((its, 5 ), dtype = float ) p = np.array([x,v,m,c,k]) gda_p[ 0 ,:] = p for i in range ( 1 , its): g = GRADmck2(p[ 0 ], p[ 1 ],p[ 2 ],p[ 3 ],p[ 4 ]) ng = np.sqrt(g[ 0 ] * * 2 + g[ 1 ] * * 2 + g[ 2 ] * * 2 + g[ 3 ] * * 2 + g[ 4 ] * * 2 ) if ng > 1 : p = p - (eta / ng) * g #new else : p = p - eta * g #original gda_p[i, :] = p #new return gda_p # ============================================================================= # Print how many iterations in the GDA is being used print ( 'GDAiterations' ) print (GDAiterations) #======================== Call GDA and print results b2 = GDAmck2(x0, v0, m0, c0, k0, GDAiterations) # to get the final (c,k) of GDA average the last two GDA points b = . 5 * (b2[GDAiterations - 1 ] + b2[GDAiterations - 2 ]) print ( 'GDA output [x0 v0 m c k] as b[i] i = 0 1 2 3 4' ) print (b) #print(b2) print (f 'k/m should be {k/m}' ) print (b[ 4 ] / b[ 2 ]) print (f 'c/m should be {c/m}' ) print (b[ 3 ] / b[ 2 ]) print ( "SSE for (b[0], b[1], b[2], b[3], b[4]) from GDA" ) SSE_from_GDA = SSE(b[ 0 ], b[ 1 ], b[ 2 ], b[ 3 ], b[ 4 ]) print (SSE_from_GDA) print ( 'SSE from models value for m c k' ) SSE_from_model = SSE(x0,v0, m, c, k) print (SSE_from_model) # ================================================================= # graphing part of program TitleString = f 'model: mx\'\' + cx\' + kx = 0 with m = {m}, c = {c}, k = {k},\ and x({t0}) = {x0}, x\'({t0}) = {v0}.\ SSE from model\'s mck = {np.around(SSE_from_model, decimals = 6 ) } \n \ GDA start: c0 = {c0}, k0 = {k0}. \ After {GDAiterations} iterations of GDA: \ cn = {np. round (b[ 3 ] / b[ 2 ], 3 )}, \ kn = {np. round (b[ 4 ] / b[ 2 ], 3 )}. \n \ GDA learning rate eta = {eta}. \ The h for numerical gradient is h = {h}. \ Euler dt = {dt}. \ SSE from GDA\ 's mck = {np.around(SSE_from_GDA, decimals=6)}' cc = b2[:, 3 ] # vector of c found from GDA kk = b2[:, 4 ] # vector of k found from GDA # ========================= range of (c,k) points found during GDA cmin = np. min (b2[:, 3 ]) cmax = np. max (b2[:, 3 ]) kmin = np. min (b2[:, 4 ]) kmax = np. max (b2[:, 4 ]) # ========================= print range of (c,k) print ( 'c min max' ) print (cmin) print (cmax) print ( 'k min max' ) print (kmin) print (kmax) # =============================================== 3d graph and contour plot if (Do3DandContourPlot = = 1 ): ccon = np.arange(cmin - 1 , cmax + 1 , (cmax - cmin) / ckGrid) # was 200 kcon = np.arange(kmin - 1 , kmax + 1 , (kmax - kmin) / ckGrid) # was 200 Xc, Yc = np.meshgrid(ccon, kcon) # backwards notation Zc = SSE_plot(Xc,Yc) # 3DPlot fig = plt.figure(figsize = ( 12 , 10 )) ax = plt.axes(projection = '3d' ) Z10 = np.log10(Zc) sse_ck = np.log10(SSE_plot_pts(cc,kk)) # use log scale for GDA trajectory surf = ax.plot_wireframe(Xc, Yc, Z10, cmap = plt.cm.cividis) ax.scatter(cc, kk, sse_ck, c = 'r' , s = 50 , label = 'GDA points (c,k, log SSE(c,k))' ) ax.scatter(cc[ 0 ], kk[ 0 ], sse_ck[ 0 ], c = 'black' , s = 200 , label = 'GDA starting point' ) # plot_surface or plot_wireframe can used ax.set_xlabel( 'c' , labelpad = 20 ) ax.set_ylabel( 'k' , labelpad = 20 ) ax.set_zlabel( 'SSE log scale' , labelpad = 20 ) #fig.colorbar(surf, shrink=0.5, aspect=8) # to put color bar next to plot leg = ax.legend(loc = 3 ) plt.title(TitleString, fontsize = 20 ) # was 18 plt.show() # contour plot # legend 1 upleft 2 upright 3 lowleft 4 lowright ToColor = 20 ccStart = cc[ 0 :ToColor] kkStart = kk[ 0 :ToColor] ccEnd = cc[(GDAiterations - ToColor):(GDAiterations - 1 )] kkEnd = kk[(GDAiterations - ToColor):(GDAiterations - 1 )] # to plot using log scale Zc = np.log10(Zc) fig, ax = plt.subplots() ax.plot(cc, kk, 'o' , color = 'r' , label = 'GDA points (c,k)' ) ax.plot(cc, kk, 'y-' ,linewidth = 2.0 , label = 'GDA path' ) ax.plot(ccStart, kkStart, 'o' , color = 'g' , label = 'GDA starting points (c,k)' ) ax.plot(ccEnd, kkEnd, 'o' , color = 'b' , label = 'GDA ending points (c,k)' ) cp = ax.contour(Xc, Yc, Zc, levels = contourLevels) fig.colorbar(cp) # Add a colorbar to a plot ax.set_title( 'Filled Contours Plot' ) ax.set_xlabel( 'c \n Contour lines drawn at log SSE' ) ax.set_ylabel( 'k' ) leg = ax.legend(loc = legendLocationContour) plt.title(TitleString, fontsize = 20 ) # was 18 plt.show() # =============================================================== # Plot data points and x(t) actual and with estimated parameters # ======================= Calculate Euler Points from GDA and Models mck x0 = b[ 0 ] v0 = b[ 1 ] mGDA = b[ 2 ] cGDA = b[ 3 ] kGDA = b[ 4 ] EulerFromGDA = np.array(EulerX(ODE, x0, v0, mGDA,cGDA,kGDA )) # Euler from GDA EX = np.array(EulerX(ODE, x0, v0, m,c,k )) # Euler from model's mck # ============================================================================= fig, ax = plt.subplots() ax.plot(dataT, dataX, 'o' ,color = 'r' , label = 'data' ) ax.plot(ET, EX, 'y-' ,linewidth = 5.0 , label = 'Euler using mck from model' ) # plot Euler from model's mck ax.plot(ET, EulerFromGDA, 'b-' ,linewidth = 3.0 , label = 'Euler using mck from GDA' ) # plot Euler from GDA #ax.axis('equal') leg = ax.legend() plt.grid(linewidth = '3' , color = 'black' ) plt.title(TitleString, fontsize = 20 ) # was 18 ax.set_xlabel( 'time t' ) ax.set_ylabel( 'position x' ) plt.show() #================================= animation video import matplotlib.animation as animation bb = b2[ 0 ] x0 = bb[ 0 ] v0 = bb[ 1 ] mGDA = bb[ 2 ] cGDA = bb[ 3 ] kGDA = bb[ 4 ] EulerFromGDA = np.array(EulerX(ODE, x0, v0, mGDA,cGDA,kGDA )) # Euler from GDA # fig, ax = plt.subplots() line, = ax.plot(ET, EulerFromGDA, 'b-' ,linewidth = 3.0 , label = 'Euler using mck from GDA' ) # plot Euler from GDA modelPlot = ax.plot(ET, EX, 'y-' ,linewidth = 5.0 , label = 'Euler using mck from model' ) # plot Euler from model's mck dataPlot = ax.plot(dataT, dataX, 'o' ,color = 'r' , label = 'data' ) leg = ax.legend() plt.grid(linewidth = '3' , color = 'black' ) plt.title(TitleString, fontsize = 20 ) # was 18 ax.set_xlabel( 'time t' ) ax.set_ylabel( 'position x' ) title = ax.text( 0.5 , 0.1 , "", bbox = { 'facecolor' : 'w' , 'alpha' : 1.0 , 'pad' : 5 }, transform = ax.transAxes, ha = "center" ) def animate(i): bb = b2[i] x0 = bb[ 0 ] v0 = bb[ 1 ] mGDA = bb[ 2 ] cGDA = bb[ 3 ] kGDA = bb[ 4 ] c3dec = "{:.3f}" . format (np. round (cGDA, 3 )) k3dec = "{:.3f}" . format (np. round (kGDA, 3 )) EulerFromGDA = np.array(EulerX(ODE, x0, v0, mGDA,cGDA,kGDA )) # Euler from GDA line.set_ydata(EulerFromGDA) # update the data. title.set_text(f 'GDA iteration = {i} \n c = {c3dec} \n k = {k3dec} ' ) return line, title, ani = animation.FuncAnimation( fig, animate, interval = 75 , frames = GDAiterations, blit = True , save_count = 50 ) # ===================================== end animation |
Some technical notes about the above Python implementation.
- Linear Interpolation. When the Euler method is used to numerically solve a differential equation what is actually produced is a finite set of discrete points
called Euler points. We convert the Euler points into a continuous function using Linear Interpolation. Basically, we connect the Euler points with straight line segments. See image below. - We actually use a modification of the classical GDA.
What we use is an example of an adaptive GDA. We adapt the learning rate to the size of the gradient. If the length of the gradient vector we replace with . This has the effect of insuring that the distance between GDA points is at most . Note. stands for “norm of the gradient” which is a fancy way to say the length of the gradient vector.
Note. The GDA method is based on the fact that the negative of the gradient points in the direction of fast descent. However, this is a local property. We don’t know for how far we can go in that direction (in the direction of the negative of the gradient) and still be descending. If we move too far in that direction, instead of descending, we might be ascending. This is like if you jump over a hole and instead of descending into it, you end up on higher ground. The entire purpose of the GDA is to go down into the hole (to reach a local minimum).