exp_comparison/
exp_comparison.rs

1
2extern crate maths_traits;
3extern crate numerical_integration;
4
5use numerical_integration::{Integrator, EULER, RK4};
6
7
8fn main() {
9
10    //
11    //A comparison between Euler, RK4, and intrinsics for computing the exponential of a real number
12    //
13
14    //the derivative of the exponential is itself
15    fn f(_t: f64, y: f64) -> f64 { y }
16
17    //the time-step
18    let dt = 0.125;
19
20    //the initial time and values
21    let mut t = 0.0;
22    let mut y1 = EULER.init(1.0, dt, &f);
23    let mut y2 = RK4.init(1.0, dt, &f);
24
25    //table column lables
26    for _ in 0..(9+11*3) { print!("_"); }
27    println!();
28    println!("|      t|     Euler|       RK4|f64::exp()|");
29
30    for _ in 0..100 {
31        //compute the next step and print
32        println!(
33            "|{: >7.3}|{: >10.2}|{: >10.2}|{: >10.2}|",
34            t+dt,
35            EULER.step(t, y1.as_mut(), dt, &f),
36            RK4.step(t, y2.as_mut(), dt, &f),
37            (t+dt).exp()
38        );
39        t += dt;
40    }
41
42    for _ in 0..(9+11*3) { print!("_"); }
43    println!();
44
45
46}