-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathroot_test.rs
61 lines (55 loc) · 1.38 KB
/
root_test.rs
1
2
3
4
5
6
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use anyhow::Result;
use peroxide::fuga::*;
fn main() -> Result<()> {
let problem = Cubic;
let bisect = BisectionMethod {
max_iter: 100,
tol: 1e-6,
};
let newton = NewtonMethod {
max_iter: 100,
tol: 1e-6,
};
let false_pos = FalsePositionMethod {
max_iter: 100,
tol: 1e-6,
};
let secant = SecantMethod {
max_iter: 100,
tol: 1e-6,
};
let result_bisect = bisect.find(&problem)?;
let result_newton = newton.find(&problem)?;
let result_false_pos = false_pos.find(&problem)?;
let result_secant = secant.find(&problem)?;
println!("{:?}", result_bisect);
println!("{:?}", result_newton);
println!("{:?}", result_false_pos);
println!("{:?}", result_secant);
Ok(())
}
struct Cubic;
impl Cubic {
fn eval(&self, x: Pt<1>) -> Result<Pt<1>> {
Ok([(x[0] - 1f64).powi(3)])
}
}
impl RootFindingProblem<1, 1, (f64, f64)> for Cubic {
fn function(&self, x: Pt<1>) -> Result<Pt<1>> {
self.eval(x)
}
fn initial_guess(&self) -> (f64, f64) {
(0.0, 2.0)
}
}
impl RootFindingProblem<1, 1, f64> for Cubic {
fn function(&self, x: Pt<1>) -> Result<Pt<1>> {
self.eval(x)
}
fn initial_guess(&self) -> f64 {
0.0
}
fn derivative(&self, x: Pt<1>) -> Result<Jaco<1, 1>> {
Ok([[3.0 * (x[0] - 1f64).powi(2)]])
}
}