Struct ohsl::newton::Newton

source ·
pub struct Newton<T> { /* private fields */ }

Implementations§

source§

impl<T> Newton<T>

source

pub const fn new(guess: T) -> Self

Create a new newton object

Examples found in repository?
examples/root_finding.rs (line 16)
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
fn main() {
    println!( "------------------ Root finding ------------------" );

    println!( " * Let us solve the equation cos(x) - x = 0 ");
    println!( " * with initial guess x_0 = 1.");

    fn function(x: f64) -> f64 {
        f64::cos( x ) - x
    }
    let newton = Newton::<f64>::new( 1.0 );
    let solution = newton.solve( &function );
    println!( " * Our solution is x = {:.6}", solution.unwrap() );
    println!( " * to six decimal places.");
    println!( "---------------------------------------------------" );
    println!( " * Now we shall solve the set of equations ");
    println!( " * x^3 + y - 1 = 0, " );
    println!( " * y^3 - x + 1 = 0, " );
    println!( " * using Newton's method and the initial guess " );
    let guess = Vec64::create( vec![ 0.5, 0.25 ] );
    println!( " * ( x_0, y_0 ) = {}.", guess.clone() );

    fn vector_function( x: Vec64 ) -> Vec64 {
        let mut f = Vec64::new( 2, 0.0 );
        f[0] = f64::powf( x[0], 3.0 ) + x[1] -1.0;
        f[1] = f64::powf( x[1], 3.0 ) - x[0] + 1.0;
        f
    }
    let newton = Newton::<Vec64>::new( guess );
    let solution = newton.solve( &vector_function ).unwrap();
    println!( " * Our solution is x = {:.2} and", solution[0] );
    println!( " * y = {:.2} to two decimal places.", solution[1] );
    println!( "-------------------- FINISHED ---------------------" );
}
source

pub fn tolerance(&mut self, tolerance: f64)

Edit the convergence tolerance

source

pub fn delta(&mut self, delta: f64)

Edit the finite difference derivative step

source

pub fn iterations(&mut self, iterations: usize)

Edit the maximum number of iterations

source

pub fn guess(&mut self, guess: T)

Edit the initial guess

source§

impl<T: Copy> Newton<T>

source

pub fn parameters(&self) -> (f64, f64, usize, T)

Return the current parameters as a tuple

source§

impl Newton<f64>

source

pub fn solve(&self, func: &dyn Fn(f64) -> f64) -> Result<f64, f64>

Solve the equation via Newton iteration

Examples found in repository?
examples/root_finding.rs (line 17)
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
fn main() {
    println!( "------------------ Root finding ------------------" );

    println!( " * Let us solve the equation cos(x) - x = 0 ");
    println!( " * with initial guess x_0 = 1.");

    fn function(x: f64) -> f64 {
        f64::cos( x ) - x
    }
    let newton = Newton::<f64>::new( 1.0 );
    let solution = newton.solve( &function );
    println!( " * Our solution is x = {:.6}", solution.unwrap() );
    println!( " * to six decimal places.");
    println!( "---------------------------------------------------" );
    println!( " * Now we shall solve the set of equations ");
    println!( " * x^3 + y - 1 = 0, " );
    println!( " * y^3 - x + 1 = 0, " );
    println!( " * using Newton's method and the initial guess " );
    let guess = Vec64::create( vec![ 0.5, 0.25 ] );
    println!( " * ( x_0, y_0 ) = {}.", guess.clone() );

    fn vector_function( x: Vec64 ) -> Vec64 {
        let mut f = Vec64::new( 2, 0.0 );
        f[0] = f64::powf( x[0], 3.0 ) + x[1] -1.0;
        f[1] = f64::powf( x[1], 3.0 ) - x[0] + 1.0;
        f
    }
    let newton = Newton::<Vec64>::new( guess );
    let solution = newton.solve( &vector_function ).unwrap();
    println!( " * Our solution is x = {:.2} and", solution[0] );
    println!( " * y = {:.2} to two decimal places.", solution[1] );
    println!( "-------------------- FINISHED ---------------------" );
}
source§

impl Newton<Vec64>

source

pub fn solve(&self, func: &dyn Fn(Vec64) -> Vec64) -> Result<Vec64, Vec64>

Solve the vector equation via Newton iteration

Examples found in repository?
examples/root_finding.rs (line 35)
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
fn main() {
    println!( "------------------ Root finding ------------------" );

    println!( " * Let us solve the equation cos(x) - x = 0 ");
    println!( " * with initial guess x_0 = 1.");

    fn function(x: f64) -> f64 {
        f64::cos( x ) - x
    }
    let newton = Newton::<f64>::new( 1.0 );
    let solution = newton.solve( &function );
    println!( " * Our solution is x = {:.6}", solution.unwrap() );
    println!( " * to six decimal places.");
    println!( "---------------------------------------------------" );
    println!( " * Now we shall solve the set of equations ");
    println!( " * x^3 + y - 1 = 0, " );
    println!( " * y^3 - x + 1 = 0, " );
    println!( " * using Newton's method and the initial guess " );
    let guess = Vec64::create( vec![ 0.5, 0.25 ] );
    println!( " * ( x_0, y_0 ) = {}.", guess.clone() );

    fn vector_function( x: Vec64 ) -> Vec64 {
        let mut f = Vec64::new( 2, 0.0 );
        f[0] = f64::powf( x[0], 3.0 ) + x[1] -1.0;
        f[1] = f64::powf( x[1], 3.0 ) - x[0] + 1.0;
        f
    }
    let newton = Newton::<Vec64>::new( guess );
    let solution = newton.solve( &vector_function ).unwrap();
    println!( " * Our solution is x = {:.2} and", solution[0] );
    println!( " * y = {:.2} to two decimal places.", solution[1] );
    println!( "-------------------- FINISHED ---------------------" );
}
source

pub fn solve_jacobian( &self, func: &dyn Fn(Vec64) -> Vec64, jac: &dyn Fn(Vec64) -> Mat64 ) -> Result<Vec64, Vec64>

Solve the vector equation via Newton iteration using the exact Jacobian

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for Newton<T>where T: RefUnwindSafe,

§

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

§

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

§

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

§

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

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

source§

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, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

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

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

Performs the conversion.
source§

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

§

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

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

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

Performs the conversion.
§

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

§

fn vzip(self) -> V