-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsingle_variable.rs
More file actions
19 lines (14 loc) · 740 Bytes
/
Copy pathsingle_variable.rs
File metadata and controls
19 lines (14 loc) · 740 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
use eqsolver::single_variable::{FDNewton, Newton, Secant};
fn main() {
let f = |x: f64| x.cos() - x.sin();
let df = |x: f64| -x.sin() - x.cos(); // Derivative of f
let solution_newton = Newton::new(f, df).solve(0.8).unwrap(); // Starting guess is 0.8
// Finite difference Newton requires no derivative to be inputted (it
// approximates it)
let solution_fdnewton = FDNewton::new(f).solve(0.8).unwrap(); // Starting guess is 0.8
// Secant method requires no derivative too but needs two starting points
let solution_secant = Secant::new(f).solve(0.5, 1.).unwrap();
println!("Newton: {solution_newton}");
println!("FDNewton: {solution_fdnewton}");
println!("Secant: {solution_secant}");
}