10 Plot

For Rust, there are some plot libraries but, still difficult to use. Practically, using python is best choice to plot. And there is awesome crate - inline-python.

Let’s combine peroxide & inline-python. Let’s see below code.

extern crate peroxide;
use peroxide::*;

fn main() {
    let init_state = State::<f64>::new(0f64, c!(1), c!(0));

    let mut ode_solver = ExplicitODE::new(test_fn);

    ode_solver
        .set_method(ExMethod::RK4)
        .set_initial_condition(init_state)
        .set_step_size(0.01)
        .set_times(1000);

    let result = ode_solver.integrate();

    let mut st = SimpleWriter::new();
    st.set_path("example_data/rk4_test.pickle")
        .insert_matrix(result)
        .write_pickle();
}

fn test_fn(st: &mut State<f64>) {
    let x = st.param;
    let y = &st.value;
    let dy = &mut st.deriv;
    dy[0] = (5f64*x.powi(2) - y[0]) / (x + y[0]).exp();
}

Now, let’s modify this code to below. Then it works surprisingly!

#![feature(proc_macro_hygiene)]
extern crate inline_python;
extern crate peroxide;
use peroxide::*;
use inline_python::python;

fn main() {
    let init_state = State::<f64>::new(0f64, c!(1), c!(0));

    let mut ode_solver = ExplicitODE::new(test_fn);

    ode_solver
        .set_method(ExMethod::RK4)
        .set_initial_condition(init_state)
        .set_step_size(0.01)
        .set_times(1000);

    let result = ode_solver.integrate();
    let x = result.col(0);
    let y = result.col(1);

    python! {
        import pylab as plt
        plt.plot('x, 'y)
        plt.show()
    }
}

fn test_fn(st: &mut State<f64>) {
    let x = st.param;
    let y = &st.value;
    let dy = &mut st.deriv;
    dy[0] = (5f64*x.powi(2) - y[0]) / (x + y[0]).exp();
}