Euler method: Difference between revisions

Content added Content deleted
Line 1,089: Line 1,089:


<lang Futhark>
<lang Futhark>
let analytic(t0: f64) (time: f64): f64 =
import "futlib/math"

fun analytic(t0: f64) (time: f64): f64 =
20.0 + (t0 - 20.0) * f64.exp(-0.07*time)
20.0 + (t0 - 20.0) * f64.exp(-0.07*time)


fun cooling(_time: f64) (temperature: f64): f64 =
let cooling(_time: f64) (temperature: f64): f64 =
-0.07 * (temperature-20.0)
-0.07 * (temperature-20.0)


fun main(t0: f64) (a: f64) (b: f64) (h: f64): []f64 =
let main(t0: f64) (a: f64) (b: f64) (h: f64): []f64 =
let steps = i32((b-a)/h)
let steps = i32.f64 ((b-a)/h)
let temps = replicate steps 0.0
let temps = replicate steps 0.0
loop ((t,temps)=(t0,temps)) = for i < steps do
let (_,temps) = loop (t,temps)=(t0,temps) for i < steps do
let x = a + f64(i) * h
let x = a + f64.i32 i * h
let temps[i] = f64.abs(t-analytic t0 x)
let temps[i] = f64.abs(t-analytic t0 x)
in (t + h * cooling x t,
in (t + h * cooling x t,