basic_integration/
basic_integration.rs

1// cargo run --example basic_integration
2
3extern crate ohsl;
4
5pub use ohsl::vector::{Vector, Vec64};
6pub use ohsl::mesh1d::Mesh1D;
7
8
9fn main() {
10    println!("--------- Basic integration ---------");
11
12    println!( "  * Create a 1D mesh with 2 variables and set " );
13    println!( "  * one equal 2x and the other to x^2. " );
14
15    let nodes = Vec64::linspace( 0.0, 1.0, 101 );
16    let mut mesh = Mesh1D::<f64, f64>::new( nodes.clone(), 2 );
17    for i in 0..nodes.size() {
18        let x = nodes[i];
19        mesh[i][0] = 2.0 * x;
20        mesh[i][1] = x * x;
21    }
22
23    println!( "  * number of nodes = {}", mesh.nnodes() );
24    println!( "  * number of variables = {}", mesh.nvars() );
25    println!( "  * Interpolate the value of each of the ");
26    println!( "  * variables at x = 0.314 ");
27    let vars = mesh.get_interpolated_vars( 0.314 );
28    println!( "  * vars = {}", vars );
29
30    println!( "  * Numerically integrate the variables over " );
31    println!( "  * the domain (from 0 to 1) " );
32    println!( "  * Integral 2x = {}", mesh.trapezium( 0 ) );
33    println!( "  * Integral x^2 = {}", mesh.trapezium( 1 ) );
34
35    // The mesh may be printed to a file using
36    // mesh.output( "./output.txt", 5 );
37    // here 5 is the precision of the output values.
38    // Similarly a pre-existing file can be read into a mesh using
39    // mesh.read( "./output.txt" );
40    
41    println!( "--- FINISHED ---" );
42}
43