Function find_real_roots_of_polynomial::bisection_method [−][src]
pub fn bisection_method<K>(
f: &Polynomial<K>,
limit: &K,
left: K,
right: K
) -> (K, K) where
K: Sized + Clone + Ord + Zero + One + for<'x> AddAssign<&'x K> + for<'x> MulAssign<&'x K>,
for<'x> &'x K: Add<Output = K> + Sub<Output = K> + Mul<Output = K> + Div<Output = K>,
Expand description
Find root of f
from (left, right]
by bisection method.
Require: f
has exact one solution on (left, right]
.
Iteration stops if right - left < limit
.
use find_real_roots_of_polynomial::bisection_method; use polynomial_ring::{polynomial, Polynomial}; use num::{BigRational, BigInt}; let v = [-2, 0, 1].iter().map(|x| BigRational::from(BigInt::from(*x))).collect::<Vec<_>>(); let f = Polynomial::new(v); // f=x^2-2 let limit = BigRational::new(BigInt::from(1), BigInt::from(10)); // 1/10 let left = BigRational::from(BigInt::from(0)); let right = BigRational::from(BigInt::from(2)); // (left, right] = (0, 2] let (l, r) = bisection_method::<BigRational>(&f, &limit, left, right); assert!(&r - &l < limit); assert_eq!(l, BigRational::new(BigInt::from(11), BigInt::from(8))); // 11/8 = 1.375 assert_eq!(r, BigRational::new(BigInt::from(23), BigInt::from(16))); // 23/16 = 1.4375