fix newtons_method handling of non-finite derivatives
This commit is contained in:
parent
d665b72a4c
commit
782d567302
@ -253,8 +253,14 @@ pub fn newtons_method(
|
||||
) -> Option<f64> {
|
||||
let mut x1: f64 = *start_x;
|
||||
let mut x2: f64;
|
||||
let mut derivative: f64;
|
||||
loop {
|
||||
x2 = x1 - (f(x1) / f_1(x1));
|
||||
derivative = f_1(x1);
|
||||
if !derivative.is_finite() {
|
||||
return None;
|
||||
}
|
||||
|
||||
x2 = x1 - (f(x1) / derivative);
|
||||
if !x2.is_finite() | !range.contains(&x2) {
|
||||
return None;
|
||||
}
|
||||
|
||||
@ -201,4 +201,22 @@ fn newtons_method() {
|
||||
&f64::EPSILON,
|
||||
);
|
||||
assert_eq!(data, None);
|
||||
|
||||
let data = newtons_method(
|
||||
&|_: f64| f64::INFINITY,
|
||||
&|x: f64| x.sin(),
|
||||
&0.0,
|
||||
&(-10.0..10.0),
|
||||
&f64::EPSILON,
|
||||
);
|
||||
assert_eq!(data, None);
|
||||
|
||||
let data = newtons_method(
|
||||
&|x: f64| x.sin(),
|
||||
&|_: f64| f64::INFINITY,
|
||||
&0.0,
|
||||
&(-10.0..10.0),
|
||||
&f64::EPSILON,
|
||||
);
|
||||
assert_eq!(data, None);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user