<commit_before>---
layout: post
title: Simulating a simple harmonic oscillator in Python
---
[So](https://www.tbray.org/ongoing/When/201x/2015/04/11/So-What), I've been going through Caltech's [beautifully-designed online version of the Feynman Lectures](http://www.feynmanlectures.caltech.edu) (to "get myself some physics", it being a subject I used to love -- that is, until having to remember, *off the top of my head*, which class of lever has an MA always > 1 kinda put me off physics and pushed me toward combi) and, as you probably know, they're brilliant. I got done with the first few chapters (they're enjoyably short, so you tend to think "Wow, that's a lot I got done" quite frequently!) and got to [chapter nine, section six](http://www.feynmanlectures.caltech.edu/I_09.html) (do a Ctrl-F "solu" or scroll down, I can't figure out how to do the # thing for some reason), where I decided to do a little Python simulation after a long time.

The section concerns numerically tracing the path of a simple harmonic oscillator -- in this case, a squeezed (or stretched) spring -- using Hooke's law. It's fairly simple if you've done this kind of thing before, and the code is somewhat self-explanatory (it never is). One thing to note is that Feynman assumes that $$k/m = 1$$, which I haven't done.

The equations we use are: 

$$
x(t+\epsilon) = x(t) + \epsilon v(t+\epsilon/2)
$$

$$
v(t+\epsilon/2) = v(t-\epsilon/2) + \epsilon a(t)
$$

$$
a(t) = -x(t)
$$

In addition, we use $$v(\epsilon/2) = v(0) + (\epsilon/2)a(0)$$ to get things started. 

It's a run-of-the-mill "enter your parameters, gape at this beautiful graph, do you want to do this again?" thing. I would've loved to do a whole animation of a spring with a little weight and everything, but my matplotlib-fu really isn't up to par at the moment, so . . . here's the code for a glorified $$\cos$$ plotter.

{% highlight python %}
#!/usr/bin/python3

import matplotlib.pyplot as plt

epsilon = 0.1000       #The time step
cycles = 100           #The number of steps<commit_msg>Add link<commit_after>---
layout: post
title: Simulating a simple harmonic oscillator in Python
---
[So](https://www.tbray.org/ongoing/When/201x/2015/04/11/So-What), I've been going through Caltech's [beautifully-designed online version of the Feynman Lectures](http://www.feynmanlectures.caltech.edu) (to "get myself some physics", it being a subject I used to love -- that is, until having to remember, *off the top of my head*, which class of lever has an MA always > 1 kinda put me off physics and pushed me toward combi) and, as you probably know, they're brilliant. I got done with the first few chapters (they're enjoyably short, so you tend to think "Wow, that's a lot I got done" quite frequently!) and got to [chapter nine, section six](http://www.feynmanlectures.caltech.edu/I_09.html#Ch9-S6), where I decided to do a little Python simulation after a long time.

The section concerns numerically tracing the path of a simple harmonic oscillator -- in this case, a squeezed (or stretched) spring -- using Hooke's law. It's fairly simple if you've done this kind of thing before, and the code is somewhat self-explanatory (it never is). One thing to note is that Feynman assumes that $$k/m = 1$$, which I haven't done.

The equations we use are: 

$$
x(t+\epsilon) = x(t) + \epsilon v(t+\epsilon/2)
$$

$$
v(t+\epsilon/2) = v(t-\epsilon/2) + \epsilon a(t)
$$

$$
a(t) = -x(t)
$$

In addition, we use $$v(\epsilon/2) = v(0) + (\epsilon/2)a(0)$$ to get things started. 

It's a run-of-the-mill "enter your parameters, gape at this beautiful graph, do you want to do this again?" thing. I would've loved to do a whole animation of a spring with a little weight and everything, but my matplotlib-fu really isn't up to par at the moment, so . . . here's the code for a glorified $$\cos$$ plotter.

{% highlight python %}
#!/usr/bin/python3

import matplotlib.pyplot as plt

epsilon = 0.1000       #The time step
cycles = 100           #The number of steps
{'fraction_non_alphanumeric': 0.11022443890274314, 'fraction_numerical': 0.01596009975062344, 'mean_word_length': 3.797846889952153, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3012130', 'n_tokens_mistral': 1295, 'n_tokens_neox': 1192, 'n_words': 575}