From 84b81c888893ea6f321c26382bc18330b6687ee6 Mon Sep 17 00:00:00 2001 From: Ryan Brott Date: Fri, 5 Jun 2020 06:25:27 -0700 Subject: [PATCH] Fix bad numerical derivative (fixes #79) --- .../ftc/teamcode/util/RegressionUtil.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/TeamCode/src/main/java/org/firstinspires/ftc/teamcode/util/RegressionUtil.java b/TeamCode/src/main/java/org/firstinspires/ftc/teamcode/util/RegressionUtil.java index 47eff956..fbe7823c 100644 --- a/TeamCode/src/main/java/org/firstinspires/ftc/teamcode/util/RegressionUtil.java +++ b/TeamCode/src/main/java/org/firstinspires/ftc/teamcode/util/RegressionUtil.java @@ -52,11 +52,14 @@ public AccelResult(double kA, double rSquare) { */ private static List numericalDerivative(List x, List y) { List deriv = new ArrayList<>(x.size()); - deriv.add(0.0); - for (int i = 0; i < x.size(); i++) { - deriv.add((y.get(i + 1) - y.get(i - 1)) / (x.get(i + 1) - x.get(i - 1))); + for (int i = 1; i < x.size() - 1; i++) { + deriv.add( + (y.get(i + 1) - y.get(i - 1)) / + (x.get(i + 1) - x.get(i - 1)) + ); } - deriv.set(0, deriv.get(1)); + // copy endpoints to pad output + deriv.add(0, deriv.get(0)); deriv.add(deriv.get(deriv.size() - 1)); return deriv; }