-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathsimpsons_integration.rs
127 lines (116 loc) · 3.67 KB
/
simpsons_integration.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
pub fn simpsons_integration<F>(f: F, a: f64, b: f64, n: usize) -> f64
where
F: Fn(f64) -> f64,
{
let h = (b - a) / n as f64;
(0..n)
.map(|i| {
let x0 = a + i as f64 * h;
let x1 = x0 + h / 2.0;
let x2 = x0 + h;
(h / 6.0) * (f(x0) + 4.0 * f(x1) + f(x2))
})
.sum()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_simpsons_integration() {
let f = |x: f64| x.powi(2);
let a = 0.0;
let b = 1.0;
let n = 100;
let result = simpsons_integration(f, a, b, n);
assert!((result - 1.0 / 3.0).abs() < 1e-6);
}
#[test]
fn test_error() {
let f = |x: f64| x.powi(2);
let a = 0.0;
let b = 1.0;
let n = 100;
let result = simpsons_integration(f, a, b, n);
let error = (1.0 / 3.0 - result).abs();
assert!(error < 1e-6);
}
#[test]
fn test_convergence() {
let f = |x: f64| x.powi(2);
let a = 0.0;
let b = 1.0;
let n = 100;
let result1 = simpsons_integration(f, a, b, n);
let result2 = simpsons_integration(f, a, b, 2 * n);
let result3 = simpsons_integration(f, a, b, 4 * n);
let result4 = simpsons_integration(f, a, b, 8 * n);
assert!((result1 - result2).abs() < 1e-6);
assert!((result2 - result3).abs() < 1e-6);
assert!((result3 - result4).abs() < 1e-6);
}
#[test]
fn test_negative() {
let f = |x: f64| -x.powi(2);
let a = 0.0;
let b = 1.0;
let n = 100;
let result = simpsons_integration(f, a, b, n);
assert!((result + 1.0 / 3.0).abs() < 1e-6);
}
#[test]
fn test_non_zero_lower_bound() {
let f = |x: f64| x.powi(2);
let a = 1.0;
let b = 2.0;
let n = 100;
let result = simpsons_integration(f, a, b, n);
assert!((result - 7.0 / 3.0).abs() < 1e-6);
}
#[test]
fn test_non_zero_upper_bound() {
let f = |x: f64| x.powi(2);
let a = 0.0;
let b = 2.0;
let n = 100;
let result = simpsons_integration(f, a, b, n);
assert!((result - 8.0 / 3.0).abs() < 1e-6);
}
#[test]
fn test_non_zero_lower_and_upper_bound() {
let f = |x: f64| x.powi(2);
let a = 1.0;
let b = 2.0;
let n = 100;
let result = simpsons_integration(f, a, b, n);
assert!((result - 7.0 / 3.0).abs() < 1e-6);
}
#[test]
fn test_non_zero_lower_and_upper_bound_negative() {
let f = |x: f64| -x.powi(2);
let a = 1.0;
let b = 2.0;
let n = 100;
let result = simpsons_integration(f, a, b, n);
assert!((result + 7.0 / 3.0).abs() < 1e-6);
}
#[test]
fn parabola_curve_length() {
// Calculate the length of the curve f(x) = x^2 for -5 <= x <= 5
// We should integrate sqrt(1 + (f'(x))^2)
let function = |x: f64| -> f64 { (1.0 + 4.0 * x * x).sqrt() };
let result = simpsons_integration(function, -5.0, 5.0, 1_000);
let integrated = |x: f64| -> f64 { (x * function(x) / 2.0) + ((2.0 * x).asinh() / 4.0) };
let expected = integrated(5.0) - integrated(-5.0);
assert!((result - expected).abs() < 1e-9);
}
#[test]
fn area_under_cosine() {
use std::f64::consts::PI;
// Calculate area under f(x) = cos(x) + 5 for -pi <= x <= pi
// cosine should cancel out and the answer should be 2pi * 5
let function = |x: f64| -> f64 { x.cos() + 5.0 };
let result = simpsons_integration(function, -PI, PI, 1_000);
let expected = 2.0 * PI * 5.0;
assert!((result - expected).abs() < 1e-9);
}
}