Euler method

func euler_method(t0, t1, k, step_size) {
    var results = [[0, t0]]
    for s in (step_size..100 -> by(step_size)) {
        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)
}

Output:

Time          2     err(%)      5     err(%)    10      err(%)  Analytic
----------------------------------------------------------------------------
   0      100.000    0.000  100.000    0.000  100.000    0.000  100.000
  10       57.634   -3.504   53.800   -9.923   44.000  -26.331   59.727
  20       37.704   -5.094   34.281  -13.711   27.200  -31.534   39.728
  30       28.328   -4.927   26.034  -12.629   22.160  -25.629   29.797
  40       23.918   -3.808   22.549   -9.313   20.648  -16.959   24.865
  50       21.843   -2.555   21.077   -5.972   20.194   -9.910   22.416
  60       20.867   -1.569   20.455   -3.512   20.058   -5.384   21.200
  70       20.408   -0.912   20.192   -1.959   20.017   -2.808   20.596
  80       20.192   -0.512   20.081   -1.057   20.005   -1.432   20.296
  90       20.090   -0.281   20.034   -0.559   20.002   -0.721   20.147
 100       20.042   -0.152   20.014   -0.291   20.000   -0.361   20.073