Updated readme example to make more sense

This commit is contained in:
Connor Johnstone
2023-03-15 15:50:47 -06:00
parent d5c8911d21
commit 98ea87adcb
2 changed files with 27 additions and 20 deletions

View File

@@ -33,26 +33,30 @@ and field line tracing:
For now, here is a simple example of using the propagator to solve a simple system: For now, here is a simple example of using the propagator to solve a simple system:
```rust ```rust
use nalgebra::Vector3; use nalgebra::Vector2;
use differential_equations::prelude::*; use differential_equations::prelude::*;
use std::f64::consts::PI;
// Define the system (parameters, derivative, and initial state) // Define the system (parameters, derivative, and initial state)
type Params = (f64, bool); type Params = (f64, f64); // Gravity and Length of Pendulum
let params = (34.0, true); let params = (9.81, 1.0);
fn derivative(t: f64, y: Vector3<f64>, p: &Params) -> Vector3<f64> { fn derivative(_t: f64, y: Vector2<f64>, p: &Params) -> Vector2<f64> {
if p.1 { -y } else { y * t } let &(g, l) = p;
let theta = y[0];
let d_theta = y[1];
Vector2::new( d_theta, -(g/l) * theta.sin() )
} }
let y0 = Vector3::new(1.0, 1.0, 1.0); let y0 = Vector2::new(0.0, PI/2.0);
// Set up the problem (ODE, Integrator, Controller, and Callbacks) // Set up the problem (ODE, Integrator, Controller, and Callbacks)
let ode = ODE::new(&derivative, 0.0, 10.0, y0, params); let ode = ODE::new(&derivative, 0.0, 6.3, y0, params);
let dp45 = DormandPrince45::new(1e-12_f64, 1e-5_f64); let dp45 = DormandPrince45::new(1e-12_f64, 1e-6_f64);
let controller = PIController::default(); let controller = PIController::default();
let value_too_high = Callback { let value_too_high = Callback {
event: &|_: f64, y: Vector3<f64>, _: &Params| { 10.0 - y[0] }, event: &|t: f64, _y: Vector2<f64>, _p: &Params| { 5.0 - t },
effect: &stop, effect: &stop,
}; };
@@ -61,5 +65,5 @@ let mut problem = Problem::new(ode, dp45, controller).with_callback(value_too_hi
let solution = problem.solve(); let solution = problem.solve();
// Can interpolate solutions to whatever you want // Can interpolate solutions to whatever you want
let interpolated_answer = solution.interpolate(8.2); let _interpolated_answer = solution.interpolate(4.4);
``` ```

View File

@@ -16,7 +16,7 @@ pub mod prelude {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use nalgebra::{Vector3, Vector6}; use nalgebra::{Vector2, Vector6};
use approx::assert_relative_eq; use approx::assert_relative_eq;
use crate::prelude::*; use crate::prelude::*;
use std::f64::consts::PI; use std::f64::consts::PI;
@@ -24,22 +24,25 @@ mod tests {
#[test] #[test]
fn test_readme() { fn test_readme() {
// Define the system (parameters, derivative, and initial state) // Define the system (parameters, derivative, and initial state)
type Params = (f64, bool); type Params = (f64, f64); // Gravity and Length of Pendulum
let params = (34.0, true); let params = (9.81, 1.0);
fn derivative(t: f64, y: Vector3<f64>, p: &Params) -> Vector3<f64> { fn derivative(_t: f64, y: Vector2<f64>, p: &Params) -> Vector2<f64> {
if p.1 { -y } else { y * t } let &(g, l) = p;
let theta = y[0];
let d_theta = y[1];
Vector2::new( d_theta, -(g/l) * theta.sin() )
} }
let y0 = Vector3::new(1.0, 1.0, 1.0); let y0 = Vector2::new(0.0, PI/2.0);
// Set up the problem (ODE, Integrator, Controller, and Callbacks) // Set up the problem (ODE, Integrator, Controller, and Callbacks)
let ode = ODE::new(&derivative, 0.0, 10.0, y0, params); let ode = ODE::new(&derivative, 0.0, 6.3, y0, params);
let dp45 = DormandPrince45::new(1e-12_f64, 1e-5_f64); let dp45 = DormandPrince45::new(1e-12_f64, 1e-6_f64);
let controller = PIController::default(); let controller = PIController::default();
let value_too_high = Callback { let value_too_high = Callback {
event: &|_: f64, y: Vector3<f64>, _: &Params| { 10.0 - y[0] }, event: &|t: f64, _y: Vector2<f64>, _p: &Params| { 5.0 - t },
effect: &stop, effect: &stop,
}; };
@@ -48,7 +51,7 @@ mod tests {
let solution = problem.solve(); let solution = problem.solve();
// Can interpolate solutions to whatever you want // Can interpolate solutions to whatever you want
let _interpolated_answer = solution.interpolate(8.2); let _interpolated_answer = solution.interpolate(4.4);
} }
#[test] #[test]