Rebol3 Code Examplex


Euler method

Solve differential equations with the basic Euler numerical scheme.

Rebol [
    title: "Rosetta code: Euler method"
    file:  %Euler_method.r3
    url:   https://rosettacode.org/wiki/Euler_method
]

euler: func[
    "Euler's method for Newton's cooling law, with formatted comparison"
    step      [integer!] "time step in seconds"
    precision [decimal!] "rounding granularity for printed numbers"
][
    print ["^/STEP:" step]
    print "Time  Euler      Analytic"
    print "-------------------------"

    ;; Initialize:
    ;; b: upper time bound (seconds) — here we reuse the initial temperature value 100
    ;; y: the current Euler-approximated temperature; initial condition T(0) = 100
    b: y: 100
    for time 0 b step [
        printf [-3 " | " 9 "| "] reduce [
            time
            round/to y precision
            round/to (20 + (80 * exp (-0.07 * time))) precision
        ]
        ;; Euler step update:
        ;; This applies the discrete forward Euler update using the ODE's RHS
        y: y + (step * (-0.07 * (y - 20)))
    ]
]
;; Run three experiments with different step sizes
euler 2  0.0001
euler 5  0.0001
euler 10 0.0001

Output:


STEP: 2
Time  Euler      Analytic
-------------------------
  0 | 100.0    | 100.0
  2 | 88.8     | 89.5487
  4 | 79.168   | 80.4627
  6 | 70.8845  | 72.5637
  8 | 63.7607  | 65.6967
 10 | 57.6342  | 59.7268
 12 | 52.3654  | 54.5368
 14 | 47.8342  | 50.0249
 16 | 43.9374  | 46.1024
 18 | 40.5862  | 42.6923
 20 | 37.7041  | 39.7278
 22 | 35.2255  | 37.1505
 24 | 33.094   | 34.9099
 26 | 31.2608  | 32.9621
 28 | 29.6843  | 31.2687
 30 | 28.3285  | 29.7965
 32 | 27.1625  | 28.5167
 34 | 26.1598  | 27.404
 36 | 25.2974  | 26.4368
 38 | 24.5558  | 25.5959
 40 | 23.918   | 24.8648
 42 | 23.3694  | 24.2293
 44 | 22.8977  | 23.6767
 46 | 22.492   | 23.1964
 48 | 22.1432  | 22.7788
 50 | 21.8431  | 22.4158
 52 | 21.5851  | 22.1002
 54 | 21.3632  | 21.8258
 56 | 21.1723  | 21.5873
 58 | 21.0082  | 21.3799
 60 | 20.867   | 21.1996
 62 | 20.7457  | 21.0429
 64 | 20.6413  | 20.9067
 66 | 20.5515  | 20.7882
 68 | 20.4743  | 20.6852
 70 | 20.4079  | 20.5957
 72 | 20.3508  | 20.5179
 74 | 20.3017  | 20.4502
 76 | 20.2594  | 20.3914
 78 | 20.2231  | 20.3403
 80 | 20.1919  | 20.2958
 82 | 20.165   | 20.2572
 84 | 20.1419  | 20.2236
 86 | 20.122   | 20.1944
 88 | 20.105   | 20.169
 90 | 20.0903  | 20.1469
 92 | 20.0776  | 20.1277
 94 | 20.0668  | 20.111
 96 | 20.0574  | 20.0965
 98 | 20.0494  | 20.0839
100 | 20.0425  | 20.073

STEP: 5
Time  Euler      Analytic
-------------------------
  0 | 100.0    | 100.0
  5 | 72.0     | 76.375
 10 | 53.8     | 59.7268
 15 | 41.97    | 47.995
 20 | 34.2805  | 39.7278
 25 | 29.2823  | 33.9019
 30 | 26.0335  | 29.7965
 35 | 23.9218  | 26.9035
 40 | 22.5492  | 24.8648
 45 | 21.657   | 23.4282
 50 | 21.077   | 22.4158
 55 | 20.7001  | 21.7024
 60 | 20.455   | 21.1996
 65 | 20.2958  | 20.8454
 70 | 20.1923  | 20.5957
 75 | 20.125   | 20.4198
 80 | 20.0812  | 20.2958
 85 | 20.0528  | 20.2085
 90 | 20.0343  | 20.1469
 95 | 20.0223  | 20.1035
100 | 20.0145  | 20.073

STEP: 10
Time  Euler      Analytic
-------------------------
  0 | 100.0    | 100.0
 10 | 44.0     | 59.7268
 20 | 27.2     | 39.7278
 30 | 22.16    | 29.7965
 40 | 20.648   | 24.8648
 50 | 20.1944  | 22.4158
 60 | 20.0583  | 21.1996
 70 | 20.0175  | 20.5957
 80 | 20.0052  | 20.2958
 90 | 20.0016  | 20.1469
100 | 20.0005  | 20.073