integral/
integral.rs

1extern crate numerical_integration;
2
3use numerical_integration::{Integrator, EULER, RK4};
4
5
6fn main() {
7
8    use std::f64::consts::*;
9
10    //
11    //Computes pi by numerically approximating the integral of e^(-x^2)
12    //
13
14    //the function e^(-t^2) that we are trying to integrate
15    fn f(t: f64, _y: f64) -> f64 { (-t*t).exp() }
16
17    let n = 1000; //the number of steps
18    let l = 100.0; //the size of the interval to integrate over
19    let dt = l / (n as f64);
20
21    //init
22    let mut t = -l/2.0;
23    let mut y1 = EULER.init(0.0, dt, &f);
24    let mut y2 = RK4.init(0.0, dt, &f);
25
26    for _ in 0..n {
27        println!("{} {} {}", EULER.step(t, y1.as_mut(), dt, &f), RK4.step(t, y2.as_mut(), dt, &f), PI.sqrt());
28        t += dt;
29    }
30
31    //the result should be sqrt(PI)
32    println!("{} {} {}", y1[0]*y1[0], y2[0]*y2[0], PI);
33
34
35}