Skip to content

Instantly share code, notes, and snippets.

@lotz84
Created October 27, 2018 00:53
Show Gist options
  • Save lotz84/60fb783af7b90b1a3f69f46828e1f74c to your computer and use it in GitHub Desktop.
Save lotz84/60fb783af7b90b1a3f69f46828e1f74c to your computer and use it in GitHub Desktop.
単振り子のシミュレーション
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ViewPatterns #-}
module Main where
import Data.Maybe
import Graphics.Gloss
import Numeric.Hamilton
import Numeric.LinearAlgebra.Static hiding ((<>))
import qualified Data.Vector.Sized as V
pendulum :: System 2 1
pendulum = mkSystem' mass transform potential
where
mass = vec2 1 1
transform (V.head -> t) =
let (x, y) = (sin t, -cos t)
in fromJust $ V.fromList [x, y]
potential = V.last -- y座標の値
type Model = Phase 1
draw :: Model -> Picture
draw (Phs qs _) =
let t = realToFrac $ qs <.> 180 / pi -- 度数法に変換
stick = translate 0 (-75) $ rectangleSolid 1 150 -- 棒
weight = translate 0 (-150) $ circleSolid 10 -- 重り
in rotate (-t) $ stick <> weight
main :: IO ()
main = simulate inWindow white 24 initModel draw (const step)
where
inWindow = InWindow "Haskell Day 2018" (640, 480) (100, 100)
initModel = Phs 1 0
step dt = stepHam (realToFrac dt) pendulum
@lotz84
Copy link
Author

lotz84 commented Oct 27, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment