#!/usr/bin/ruby

#
## https://rosettacode.org/wiki/Euler_method#Sidef
#

func euler_method(t0, t1, k, step_size) {
    var results = [[0, t0]];
    step_size.to(100).by(step_size).each { |s|
        t0 -= ((t0 - t1) * k * step_size);
        results << [s, t0];
    }
    return results;
}

func analytical(t0, t1, k, time) {
    (t0 - t1) * exp(-time * k) + t1;
}

var (T0, T1, k) = (100, 20, .07);
var r2  = euler_method(T0, T1, k,  2).grep { _[0] %% 10 };
var r5  = euler_method(T0, T1, k,  5).grep { _[0] %% 10 };
var r10 = euler_method(T0, T1, k, 10).grep { _[0] %% 10 };

say "Time\t      2     err(%)      5     err(%)    10      err(%)  Analytic";
say "-"*76;

r2.range.each { |i|
    var an = analytical(T0, T1, k, r2[i][0]);
    printf("%4d\t#{'%9.3f' * 7}\n",
                r2[i][0],
                r2[i][1], (r2[i][1] / an) * 100 - 100,
                r5[i][1], (r5[i][1] / an) * 100 - 100,
                r10[i][1], (r10[i][1] / an) * 100 - 100,
                an);
}
