Scrapbook-0.4.0: code examples
Copyright© Frank Jung 2025
LicenseGPL-3
Safe HaskellSafe-Inferred
LanguageHaskell2010

HarmonicOscillation

Description

This Haskell code defines a module to simulate simple harmonic oscillation, like the motion of a pendulum. It does this using a State monad to manage the changing position and time.

Example usage

To compute the position of a harmonic oscillator:

let positions = evalState harmonic (HarmonicOscillation 0 0)
take 10 positions
evalState harmonic (HarmonicOscillation 0 0)
This runs the harmonic computation, starting with an initial state where position is 0 and time is 0.
evalState
discards the final state and gives you just the result, which is an infinite list of positions.
take 10 positions
Since positions is an infinite list, you use a function like take to get a finite number of elements from it.
Synopsis

Documentation

data HarmonicOscillation Source #

Represents the state of a harmonic oscillator. This is a simple record that holds the state of the oscillator at any given moment: its current position and the current time.

Constructors

HarmonicOscillation 

Fields

harmonic :: State HarmonicOscillation [Double] Source #

This function generates an infinite list of positions over time.

step :: State HarmonicOscillation Double Source #

Step function to update the state of the harmonic oscillator. It calculates the new position based on the sine function and increments the time. Returns the new position.