pub struct Newton<T, F, D> { /* private fields */ }Expand description
§Newton-Raphson
Newton solves an equation f(x) = 0 given the function f and its derivative df as closures that takes a Float and outputs a Float.
This function uses the Newton-Raphson’s method (Wikipedia).
Default Tolerance: 1e-6
Default Max Iterations: 50
§Examples
§A solution exists
// Want to solve x in cos(x) = sin(x). This is equivalent to solving x in cos(x) - sin(x) = 0.
use eqsolver::single_variable::Newton;
let f = |x: f64| x.cos() - x.sin();
let df = |x: f64| -x.sin() - x.cos(); // Derivative of f
// Solve with Newton's Method. Error is less than 1E-6. Starting guess is around 0.8.
let solution = Newton::new(f, df)
.with_tol(1e-6)
.solve(0.8)
.unwrap();
assert!((solution - std::f64::consts::FRAC_PI_4).abs() <= 1e-6); // Exactly x = pi/4§A solution does not exist
use eqsolver::{single_variable::Newton, SolverError};
let f = |x: f64| x*x + 1.;
let df = |x: f64| 2.*x;
// Solve with Newton's Method. Error is less than 1E-6. Starting guess is around 1.
let solution = Newton::new(f, df).solve(1.);
assert_eq!(solution.err().unwrap(), SolverError::NotANumber); // No solution, will divergeImplementations§
Source§impl<T, F, D> Newton<T, F, D>
impl<T, F, D> Newton<T, F, D>
Sourcepub fn new(f: F, df: D) -> Self
pub fn new(f: F, df: D) -> Self
Set up the solver
Instantiates the solver using the given closure representing the function f to find roots for. This function also takes f’s derivative df
Sourcepub fn with_tol(&mut self, tol: T) -> &mut Self
pub fn with_tol(&mut self, tol: T) -> &mut Self
Updates the solver’s tolerance (Magnitude of Error).
Default Tolerance: 1e-6
§Examples
use eqsolver::single_variable::Newton;
let f = |x: f64| x*x - 2.; // Solve x^2 = 2
let df = |x: f64| 2.*x; // Derivative of f
let solution = Newton::new(f, df)
.with_tol(1e-12)
.solve(1.4)
.unwrap();
assert!((solution - 2_f64.sqrt()).abs() <= 1e-12);Sourcepub fn with_itermax(&mut self, max: usize) -> &mut Self
pub fn with_itermax(&mut self, max: usize) -> &mut Self
Updates the solver’s amount of iterations done before terminating the iteration
Default Max Iterations: 50
§Examples
use eqsolver::{single_variable::Newton, SolverError};
let f = |x: f64| x.powf(-x); // Solve x^-x = 0
let df = |x: f64| -x.powf(-x) * (1. + x.ln()); // Derivative of f
let solution = Newton::new(f, df)
.with_itermax(20)
.solve(1.); // Solver will terminate after 20 iterations
assert_eq!(solution.err().unwrap(), SolverError::MaxIterReached(21));Sourcepub fn solve(&self, x0: T) -> SolverResult<T>
pub fn solve(&self, x0: T) -> SolverResult<T>
Solves for x in f(x) = 0 where f is the stored function.
§Examples
use eqsolver::{DEFAULT_TOL, single_variable::Newton};
let f = |x: f64| x*x - 2.; // Solve x^2 = 2
let df = |x: f64| 2.*x; // Derivative of f
let solution = Newton::new(f, df)
.solve(1.4)
.unwrap();
assert!((solution - 2_f64.sqrt()).abs() <= DEFAULT_TOL); // Default Tolerance = 1e-6Auto Trait Implementations§
impl<T, F, D> Freeze for Newton<T, F, D>
impl<T, F, D> RefUnwindSafe for Newton<T, F, D>
impl<T, F, D> Send for Newton<T, F, D>
impl<T, F, D> Sync for Newton<T, F, D>
impl<T, F, D> Unpin for Newton<T, F, D>
impl<T, F, D> UnwindSafe for Newton<T, F, D>
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.