Newton

Struct Newton 

Source
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 diverge

Implementations§

Source§

impl<T, F, D> Newton<T, F, D>
where T: Float, F: Fn(T) -> T, D: Fn(T) -> T,

Source

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

Source

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);
Source

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));
Source

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-6

Auto Trait Implementations§

§

impl<T, F, D> Freeze for Newton<T, F, D>
where F: Freeze, D: Freeze, T: Freeze,

§

impl<T, F, D> RefUnwindSafe for Newton<T, F, D>

§

impl<T, F, D> Send for Newton<T, F, D>
where F: Send, D: Send, T: Send,

§

impl<T, F, D> Sync for Newton<T, F, D>
where F: Sync, D: Sync, T: Sync,

§

impl<T, F, D> Unpin for Newton<T, F, D>
where F: Unpin, D: Unpin, T: Unpin,

§

impl<T, F, D> UnwindSafe for Newton<T, F, D>
where F: UnwindSafe, D: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

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

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V