pátek 27. června 2014

Harmonographs in Python

I came across thing called harmonograph recently. I have never heard of it before, but was I couldn't believe my eyes, when I saw the beauty that "harmonographs" represent. It looks like an art masterpiece and actually it is all based on physics. It's hard to imagine, but basically a pen is attached to two pendulums, both moving differently, and as a result to their motion the final drawing on a paper (that is located right below the pen) is something looking absolutely amazing.
source: http://harmonographs.freewebspace.com/images/image_(22).jpg

I found out that the graphs of harmonographs can be described by a mathematical equation and can be computer generated.

I single one axis pendulum would produce a movement described by the equation:
source: http://en.wikipedia.org/wiki/Harmonograph

If we have the pen dependent on more than one pendulum than the equation would look like this:

source: http://en.wikipedia.org/wiki/Harmonograph

The variables in the equation are: frequency f, amplitude A, phase shift p, time t, damping coefficient d.

This parametric equation describes the movement of the pen connected and dependent on the movement of two pendulums. The simple process of addition of two pendulum equation is due to the superposition principle stating:

"In physics and systems theory the superposition principle, also known as superposition property, states that, for all linear systems, the net response at a given place and time caused by two or more stimuli is the sum of the responses which would have been caused by each stimulus individually. So that if input A produces response X and input B produces response Y then input (A+ B) produces response (X+ Y)."
source: http://en.wikipedia.org/wiki/Superposition_principle

Therefore creating a program that can simulate the movement a pen connected to two pendulums is very easy. We have only one unknown variable, which is time that we have to determine and we have all the variables set by our wishes.

------------------------------------------------------------
import turtle
from math import sin, pi, e

decay1, decay2, decay3, decay4 = 0.02, 0.5, 0.2, 0.005  #The oscillation decay coefficients
phase1, phase2, phase3, phase4 = pi/6, pi/2, pi/6, pi/2  #Phase shifts
freq1, freq2, freq3, freq4 = 3, 2, 6, 4 #Frequencies

t = 0 #Initial time
dt = 0.05 #Time shift between each points of the graph

while t < 10: #Drawing graph finishes at time t = 10 seconds
    x = 100 * pow(e, -decay1 * t) * sin(t * freq1 + phase1) + 100 * pow(e, -decay2 * t) * sin(t * freq2 + phase2) #x axis position via formula
    y = 100 * pow(e, -decay3 * t) * sin(t * freq3 + phase3) + 100 * pow(e, -decay4 * t) * sin(t * freq4 + phase4) #y axis position
    turtle.setposition(x, y) #Setting the position of the pen
    t += dt #Next (new) value of time
------------------------------------------------------------

This easy program will have this kind of result, using the Python Turtle Graphics.
If we let the program last longer, the objects created will be more sophisticated and beautiful. 

The program that I wrote isn't anything new at all. It's pretty similar to the one I was inspired by. Here are some resources I was inspired by and that I gained knowledge from. It was definitely fun learning about something so magically beautiful and yet purely mathematically calculable.


Lukas Cerny, 24. 6. 2014

Žádné komentáře:

Okomentovat