#![deny(missing_docs)]
pub struct NewtonRaphson {
pub value: f64,
pub root: f64,
}
impl NewtonRaphson {}
pub fn find_root(f: fn(f64) -> f64, df: fn(f64) -> f64, guess: f64, iterations: i32) -> f64 {
let mut result = guess;
let iteration =
|f: fn(f64) -> f64, df: fn(f64) -> f64, guess: f64| -> f64 { guess - f(guess) / df(guess) };
for _ in 0..iterations {
result = iteration(f, df, result);
}
result
}
#[cfg(test)]
mod tests {
use crate::assert_approx_equal;
use super::*;
#[test]
fn test_newton_raphson() {
fn f(x: f64) -> f64 {
x * x
}
fn df(x: f64) -> f64 {
2.0 * x
}
let root = find_root(f, df, 10.0, 100);
assert_approx_equal!(root, 0_f64, 1e-10);
println!("ROOT = {}", root);
}
}