Repository: commons-math
Updated Branches:
  refs/heads/master 86b92b4e5 -> 69273dca6


fixed too long first step in fixed Runge-Kutta integrators.

This change is similar to the one done two years ago for adaptive step sizes 
integrator.

JIRA: MATH-727


Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/69273dca
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/69273dca
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/69273dca

Branch: refs/heads/master
Commit: 69273dca6188a3d7d629d0d32dcf9cdb5b6c1036
Parents: 86b92b4
Author: Luc Maisonobe <[email protected]>
Authored: Wed Oct 8 14:25:05 2014 +0200
Committer: Luc Maisonobe <[email protected]>
Committed: Wed Oct 8 14:25:05 2014 +0200

----------------------------------------------------------------------
 .../ode/nonstiff/RungeKuttaIntegrator.java      | 14 +++++++++++-
 .../ClassicalRungeKuttaIntegratorTest.java      | 24 ++++++++++++++++++++
 2 files changed, 37 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-math/blob/69273dca/src/main/java/org/apache/commons/math3/ode/nonstiff/RungeKuttaIntegrator.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/commons/math3/ode/nonstiff/RungeKuttaIntegrator.java 
b/src/main/java/org/apache/commons/math3/ode/nonstiff/RungeKuttaIntegrator.java
index 68bd8b0..5f7d5d8 100644
--- 
a/src/main/java/org/apache/commons/math3/ode/nonstiff/RungeKuttaIntegrator.java
+++ 
b/src/main/java/org/apache/commons/math3/ode/nonstiff/RungeKuttaIntegrator.java
@@ -119,7 +119,19 @@ public abstract class RungeKuttaIntegrator extends 
AbstractIntegrator {
 
     // set up integration control objects
     stepStart = equations.getTime();
-    stepSize  = forward ? step : -step;
+    if (forward) {
+        if (stepStart + step >= t) {
+            stepSize = t - stepStart;
+        } else {
+            stepSize = step;
+        }
+    } else {
+        if (stepStart - step <= t) {
+            stepSize = t - stepStart;
+        } else {
+            stepSize = -step;
+        }
+    }
     initIntegration(equations.getTime(), y0, t);
 
     // main integration loop

http://git-wip-us.apache.org/repos/asf/commons-math/blob/69273dca/src/test/java/org/apache/commons/math3/ode/nonstiff/ClassicalRungeKuttaIntegratorTest.java
----------------------------------------------------------------------
diff --git 
a/src/test/java/org/apache/commons/math3/ode/nonstiff/ClassicalRungeKuttaIntegratorTest.java
 
b/src/test/java/org/apache/commons/math3/ode/nonstiff/ClassicalRungeKuttaIntegratorTest.java
index 8136596..c527680 100644
--- 
a/src/test/java/org/apache/commons/math3/ode/nonstiff/ClassicalRungeKuttaIntegratorTest.java
+++ 
b/src/test/java/org/apache/commons/math3/ode/nonstiff/ClassicalRungeKuttaIntegratorTest.java
@@ -310,4 +310,28 @@ public class ClassicalRungeKuttaIntegratorTest {
       }, 0.0, new double[] { 0.0 }, 5.0, new double[1]);
   }
 
+  @Test
+  public void testTooLargeFirstStep() {
+
+      RungeKuttaIntegrator integ = new ClassicalRungeKuttaIntegrator(0.5);
+      final double start = 0.0;
+      final double end   = 0.001;
+      FirstOrderDifferentialEquations equations = new 
FirstOrderDifferentialEquations() {
+
+          public int getDimension() {
+              return 1;
+          }
+
+          public void computeDerivatives(double t, double[] y, double[] yDot) {
+              Assert.assertTrue(t >= FastMath.nextAfter(start, 
Double.NEGATIVE_INFINITY));
+              Assert.assertTrue(t <= FastMath.nextAfter(end,   
Double.POSITIVE_INFINITY));
+              yDot[0] = -100.0 * y[0];
+          }
+
+      };
+
+      integ.integrate(equations, start, new double[] { 1.0 }, end, new 
double[1]);
+
+  }
+
 }

Reply via email to