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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
pub const STEPSIZE_CENTRAL5: f64 = 1e-3;
pub fn deriv_and_errors_central5<A>(at_x: f64, f: fn(f64, &mut A) -> f64, args: &mut A, h: f64) -> (f64, f64, f64) {
let fm1 = f(at_x - h, args);
let fp1 = f(at_x + h, args);
let fmh = f(at_x - h / 2.0, args);
let fph = f(at_x + h / 2.0, args);
let r3 = 0.5 * (fp1 - fm1);
let r5 = (4.0 / 3.0) * (fph - fmh) - (1.0 / 3.0) * r3;
let dfdx = r5 / h;
let e3 = (f64::abs(fp1) + f64::abs(fm1)) * f64::EPSILON;
let e5 = 2.0 * (f64::abs(fph) + f64::abs(fmh)) * f64::EPSILON + e3;
let dy = f64::max(f64::abs(r3 / h), f64::abs(r5 / h)) * (f64::abs(at_x) / h) * f64::EPSILON;
let abs_trunc_err = f64::abs((r5 - r3) / h);
let abs_round_err = f64::abs(e5 / h) + dy;
(dfdx, abs_trunc_err, abs_round_err)
}
pub fn deriv_central5<A>(at_x: f64, f: fn(f64, &mut A) -> f64, args: &mut A) -> f64 {
let h = STEPSIZE_CENTRAL5;
let (dfdx, err, rerr) = deriv_and_errors_central5(at_x, f, args, h);
let err_total = err + rerr;
if err == 0.0 || rerr == 0.0 {
return dfdx;
}
if err < rerr {
return dfdx;
}
let h_improv = h * f64::powf(rerr / (2.0 * err), 1.0 / 3.0);
let (dfdx_improv, err_improv, rerr_improv) = deriv_and_errors_central5(at_x, f, args, h_improv);
let err_total_improv = err_improv + rerr_improv;
if err_total_improv > err_total {
return dfdx;
}
if f64::abs(dfdx_improv - dfdx) > 4.0 * err_total {
return dfdx;
}
dfdx_improv
}
#[cfg(test)]
mod tests {
use super::{deriv_and_errors_central5, deriv_central5};
use std::f64::consts::PI;
struct Arguments {}
struct TestFunction {
pub name: &'static str,
pub f: fn(f64, &mut Arguments) -> f64,
pub g: fn(f64, &mut Arguments) -> f64,
pub at_x: f64,
pub tol_diff: f64,
pub tol_err: f64,
pub tol_rerr: f64,
pub improv_tol_diff: f64,
}
fn gen_functions() -> Vec<TestFunction> {
vec![
TestFunction {
name: "x",
f: |x, _| x,
g: |_, _| 1.0,
at_x: 0.0,
tol_diff: 1e-15,
tol_err: 1e-15,
tol_rerr: 1e-15,
improv_tol_diff: 1e-15,
},
TestFunction {
name: "x²",
f: |x, _| x * x,
g: |x, _| 2.0 * x,
at_x: 1.0,
tol_diff: 1e-12,
tol_err: 1e-13,
tol_rerr: 1e-11,
improv_tol_diff: 1e-12,
},
TestFunction {
name: "exp(x)",
f: |x, _| f64::exp(x),
g: |x, _| f64::exp(x),
at_x: 2.0,
tol_diff: 1e-11,
tol_err: 1e-5,
tol_rerr: 1e-10,
improv_tol_diff: 1e-10,
},
TestFunction {
name: "exp(-x²)",
f: |x, _| f64::exp(-x * x),
g: |x, _| -2.0 * x * f64::exp(-x * x),
at_x: 2.0,
tol_diff: 1e-13,
tol_err: 1e-6,
tol_rerr: 1e-13,
improv_tol_diff: 1e-11,
},
TestFunction {
name: "1/x",
f: |x, _| 1.0 / x,
g: |x, _| -1.0 / (x * x),
at_x: 0.2,
tol_diff: 1e-8,
tol_err: 1e-3,
tol_rerr: 1e-11,
improv_tol_diff: 1e-9,
},
TestFunction {
name: "x⋅√x",
f: |x, _| x * f64::sqrt(x),
g: |x, _| 1.5 * f64::sqrt(x),
at_x: 25.0,
tol_diff: 1e-10,
tol_err: 1e-9,
tol_rerr: 1e-9,
improv_tol_diff: 1e-10,
},
TestFunction {
name: "sin(1/x)",
f: |x, _| f64::sin(1.0 / x),
g: |x, _| -f64::cos(1.0 / x) / (x * x),
at_x: 0.5,
tol_diff: 1e-10,
tol_err: 1e-4,
tol_rerr: 1e-11,
improv_tol_diff: 1e-10,
},
TestFunction {
name: "cos(π⋅x/2)",
f: |x, _| f64::cos(PI * x / 2.0),
g: |x, _| -f64::sin(PI * x / 2.0) * PI / 2.0,
at_x: 1.0,
tol_diff: 1e-12,
tol_err: 1e-6,
tol_rerr: 1e-12,
improv_tol_diff: 1e-10,
},
]
}
#[test]
fn deriv_and_errors_central5_works() {
let tests = gen_functions();
println!(
"{:>10}{:>15}{:>22}{:>11}{:>10}{:>10}",
"function", "numerical", "analytical", "|num-ana|", "err", "rerr"
);
for test in &tests {
let args = &mut Arguments {};
let (d, err, rerr) = deriv_and_errors_central5(test.at_x, test.f, args, 1e-3);
let d_correct = (test.g)(test.at_x, args);
println!(
"{:>10}{:15.9}{:22}{:11.2e}{:10.2e}{:10.2e}",
test.name,
d,
d_correct,
f64::abs(d - d_correct),
err,
rerr,
);
assert!(f64::abs(d - d_correct) < test.tol_diff);
assert!(err < test.tol_err);
assert!(rerr < test.tol_rerr);
}
}
#[test]
fn deriv_central5_works() {
let tests = gen_functions();
println!(
"{:>10}{:>15}{:>22}{:>11}",
"function", "numerical", "analytical", "|num-ana|"
);
for test in &tests {
let args = &mut Arguments {};
let d = deriv_central5(test.at_x, test.f, args);
let d_correct = (test.g)(test.at_x, args);
println!(
"{:>10}{:15.9}{:22}{:11.2e}",
test.name,
d,
d_correct,
f64::abs(d - d_correct),
);
assert!(f64::abs(d - d_correct) < test.improv_tol_diff);
}
}
}