Skip to content

Instantly share code, notes, and snippets.

@DSCF-1224
Created November 9, 2019 09:20
Show Gist options
  • Save DSCF-1224/f83930f214a736317270d26cb2d08cb8 to your computer and use it in GitHub Desktop.
Save DSCF-1224/f83930f214a736317270d26cb2d08cb8 to your computer and use it in GitHub Desktop.
Euler Method about ODE
{- reference -}
-- ISBN978-4-320-01343-8
-- http://lqtmirage.hatenablog.com/entry/2017/05/29/125134
{- module to import -}
import Prelude
{- own defined structures -}
data Solution = Solution { step::Int, idvl::Double, sltn::Double }
{- own defined functions -}
methodEulerStep :: (Solution -> Double) -> Solution -> Double -> Solution
methodEulerStep arg_fncOde dataSltnCrnt stepIdvl
= Solution {
step = (step dataSltnCrnt) + 1,
idvl = (idvl dataSltnCrnt) + stepIdvl,
sltn = (sltn dataSltnCrnt) + stepIdvl * (arg_fncOde dataSltnCrnt)
}
methodEuler :: (Solution -> Double) -> Solution -> Double -> Int -> IO ()
methodEuler arg_fncOde dataSltnCrnt stepIdvl numStepEnd = do
Prelude.print $ [ (idvl dataSltnCrnt), (sltn dataSltnCrnt), tanh( idvl dataSltnCrnt ) ]
if statNumStep then methodEuler arg_fncOde dataSltnNext stepIdvl numStepEnd
else return ()
where
dataSltnNext = methodEulerStep arg_fncOde dataSltnCrnt stepIdvl
statNumStep = (step dataSltnCrnt) < numStepEnd
fncOde :: Solution -> Double
fncOde dataInput = 1.0 - (sltn dataInput) * (sltn dataInput)
{- main process -}
main :: IO ()
main = do
let init = Solution { step = 0, idvl = 0.0, sltn = 0.0 }
methodEuler fncOde init 0.1 16
{- EOF -}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment