Added parameters

This commit is contained in:
Connor Johnstone
2023-03-15 15:28:56 -06:00
parent 8daee11ae7
commit 63b603a57c
7 changed files with 123 additions and 48 deletions

View File

@@ -39,20 +39,30 @@ use differential_equations::controller::PIController;
use differential_equations::callback::stop;
use differential_equations::problem::*;
// Define the system
fn derivative(_t: f64, y: Vector3<f64>) -> Vector3<f64> { y }
// Define the system (parameters, derivative, and initial state)
type Params = (f64, bool);
let params = (34.0, true);
fn derivative(t: f64, y: Vector3<f64>, p: &Params) -> Vector3<f64> {
if p.1 { -y } else { y * t }
}
let y0 = Vector3::new(1.0, 1.0, 1.0);
let ode = ODE::new(&derivative, 0.0, 10.0, y0);
// Set up the problem (ODE, Integrator, Controller, and Callbacks)
let ode = ODE::new(&derivative, 0.0, 10.0, y0, params);
let dp45 = DormandPrince45::new(1e-12_f64, 1e-5_f64);
let controller = PIController::default();
let value_too_high = Callback {
event: &|_: f64, y: SVector<f64,3>| { 10.0 - y[0] },
event: &|_: f64, y: Vector3<f64>, _: &Params| { 10.0 - y[0] },
effect: &stop,
};
// Solve the problem
let mut problem = Problem::new(ode, dp45, controller).with_callback(value_too_high);
let solution = problem.solve();
// Can interpolate solutions to whatever you want
let interpolated_answer = solution.interpolate(8.2);
```