pub struct Newton<T> { /* private fields */ }
Implementations§
source§impl<T> Newton<T>
impl<T> Newton<T>
sourcepub const fn new(guess: T) -> Self
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 ---------------------" );
}
sourcepub fn iterations(&mut self, iterations: usize)
pub fn iterations(&mut self, iterations: usize)
Edit the maximum number of iterations
source§impl<T: Copy> Newton<T>
impl<T: Copy> Newton<T>
sourcepub fn parameters(&self) -> (f64, f64, usize, T)
pub fn parameters(&self) -> (f64, f64, usize, T)
Return the current parameters as a tuple
source§impl Newton<f64>
impl Newton<f64>
sourcepub fn solve(&self, func: &dyn Fn(f64) -> f64) -> Result<f64, f64>
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>
impl Newton<Vec64>
sourcepub fn solve(&self, func: &dyn Fn(Vec64) -> Vec64) -> Result<Vec64, Vec64>
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 ---------------------" );
}
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more