Ok fixed now

This commit is contained in:
Jérôme 2023-03-25 23:04:48 +01:00
parent 59c27b110d
commit f89389b4c2
2 changed files with 14 additions and 5 deletions

View file

@ -24,8 +24,11 @@ fn main() {
let x0 : f64 = 1.0; let x0 : f64 = 1.0;
let tol : f64 = 1e-10; let tol : f64 = 1e-10;
let max_iter : u32 = 100; let max_iter : u32 = 100;
let dx_num : f64 = 1e-6;
let x_mathematica = -3.26650043678562449167148755288; let x_mathematica = -3.26650043678562449167148755288;
let x_newton = univariate_solvers::newton_solve(&(fct as fn(f64) -> f64), &(dfct as fn(f64) -> f64), x0, tol, max_iter); let x_newton = univariate_solvers::newton_solve(&(fct as fn(f64) -> f64), &(dfct as fn(f64) -> f64), x0, tol, max_iter);
let x_newton_num: f64 = univariate_solvers::newton_solve_num(&(fct as fn(f64) -> f64), x0, tol, dx_num, max_iter);
println!("Mathematica : x = {}", x_mathematica); println!("Mathematica : x = {}", x_mathematica);
println!("Newton's method : x = {}", x_newton); println!("Newton's method : x = {}", x_newton);
println!("Newton's method (num) : x = {}", x_newton_num);
} }

View file

@ -5,7 +5,9 @@
/// @param tol tolerance /// @param tol tolerance
/// @param max_iter maximum number of iterations /// @param max_iter maximum number of iterations
/// @return solution /// @return solution
pub fn newton_solve<F: Fn(f64) -> f64, F2: Fn(f64) -> f64>(f : &F, df : &F2, x0 : f64, tol : f64, max_iter : u32) -> f64 { pub fn newton_solve<F, F2>(f : F, df : F2, x0 : f64, tol : f64, max_iter : u32) -> f64
where F : Fn(f64) -> f64, F2 : Fn(f64) -> f64
{
let mut x: f64 = x0; let mut x: f64 = x0;
let mut dx: f64; let mut dx: f64;
let mut fx: f64; let mut fx: f64;
@ -33,7 +35,11 @@ pub fn newton_solve<F: Fn(f64) -> f64, F2: Fn(f64) -> f64>(f : &F, df : &F2, x0
/// @param tol tolerance /// @param tol tolerance
/// @param max_iter maximum number of iterations /// @param max_iter maximum number of iterations
/// @return solution /// @return solution
pub fn newton_solve_num<F: Fn(f64) -> f64>(f : &F, x0 : f64, tol : f64, dx_num : f64, max_iter : u32) -> f64 { pub fn newton_solve_num<F>(f : F, x0 : f64, tol : f64, dx_num : f64, max_iter : u32) -> f64
return newton_solve(f, &((|x: f64| (f(x + dx_num) - f(x - dx_num))/(2.0*dx_num)) as fn(f64) -> f64), x0, tol, max_iter); where F : Fn(f64) -> f64
{
return newton_solve(&f, |x: f64| {
(f(x + dx_num) - f(x - dx_num))/(2.0*dx_num)
}, x0, tol, max_iter);
} }