Skip to content
This repository was archived by the owner on Sep 12, 2023. It is now read-only.

Commit 84b81c8

Browse files
committed
Fix bad numerical derivative (fixes #79)
1 parent 21a4ebf commit 84b81c8

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

TeamCode/src/main/java/org/firstinspires/ftc/teamcode/util/RegressionUtil.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,14 @@ public AccelResult(double kA, double rSquare) {
5252
*/
5353
private static List<Double> numericalDerivative(List<Double> x, List<Double> y) {
5454
List<Double> deriv = new ArrayList<>(x.size());
55-
deriv.add(0.0);
56-
for (int i = 0; i < x.size(); i++) {
57-
deriv.add((y.get(i + 1) - y.get(i - 1)) / (x.get(i + 1) - x.get(i - 1)));
55+
for (int i = 1; i < x.size() - 1; i++) {
56+
deriv.add(
57+
(y.get(i + 1) - y.get(i - 1)) /
58+
(x.get(i + 1) - x.get(i - 1))
59+
);
5860
}
59-
deriv.set(0, deriv.get(1));
61+
// copy endpoints to pad output
62+
deriv.add(0, deriv.get(0));
6063
deriv.add(deriv.get(deriv.size() - 1));
6164
return deriv;
6265
}

0 commit comments

Comments
 (0)