org.apache.commons.math3.analysis.function.HarmonicOscillator Java Examples

The following examples show how to use org.apache.commons.math3.analysis.function.HarmonicOscillator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HarmonicFitterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testNoError() {
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    HarmonicFitter fitter =
        new HarmonicFitter(new LevenbergMarquardtOptimizer());
    for (double x = 0.0; x < 1.3; x += 0.01) {
        fitter.addObservedPoint(1, x, f.value(x));
    }

    final double[] fitted = fitter.fit();
    Assert.assertEquals(a, fitted[0], 1.0e-13);
    Assert.assertEquals(w, fitted[1], 1.0e-13);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1e-13);

    HarmonicOscillator ff = new HarmonicOscillator(fitted[0], fitted[1], fitted[2]);

    for (double x = -1.0; x < 1.0; x += 0.01) {
        Assert.assertTrue(FastMath.abs(f.value(x) - ff.value(x)) < 1e-13);
    }
}
 
Example #2
Source File: HarmonicCurveFitterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test1PercentError() {
    final Random randomizer = new Random(64925784252L);
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    final HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    final WeightedObservedPoints points = new WeightedObservedPoints();
    for (double x = 0.0; x < 10.0; x += 0.1) {
        points.add(1, x, f.value(x) + 0.01 * randomizer.nextGaussian());
    }

    final HarmonicCurveFitter fitter = HarmonicCurveFitter.create();
    final double[] fitted = fitter.fit(points.toList());
    Assert.assertEquals(a, fitted[0], 7.6e-4);
    Assert.assertEquals(w, fitted[1], 2.7e-3);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.3e-2);
}
 
Example #3
Source File: HarmonicFitterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testInitialGuess() {
    Random randomizer = new Random(45314242l);
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    HarmonicFitter fitter =
        new HarmonicFitter(new LevenbergMarquardtOptimizer());
    for (double x = 0.0; x < 10.0; x += 0.1) {
        fitter.addObservedPoint(1, x,
                                f.value(x) + 0.01 * randomizer.nextGaussian());
    }

    final double[] fitted = fitter.fit(new double[] { 0.15, 3.6, 4.5 });
    Assert.assertEquals(a, fitted[0], 1.2e-3);
    Assert.assertEquals(w, fitted[1], 3.3e-3);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.7e-2);
}
 
Example #4
Source File: HarmonicFitterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test1PercentError() {
    Random randomizer = new Random(64925784252l);
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    HarmonicFitter fitter =
        new HarmonicFitter(new LevenbergMarquardtOptimizer());
    for (double x = 0.0; x < 10.0; x += 0.1) {
        fitter.addObservedPoint(1, x,
                                f.value(x) + 0.01 * randomizer.nextGaussian());
    }

    final double[] fitted = fitter.fit();
    Assert.assertEquals(a, fitted[0], 7.6e-4);
    Assert.assertEquals(w, fitted[1], 2.7e-3);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.3e-2);
}
 
Example #5
Source File: HarmonicFitterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testNoError() {
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    HarmonicFitter fitter =
        new HarmonicFitter(new LevenbergMarquardtOptimizer());
    for (double x = 0.0; x < 1.3; x += 0.01) {
        fitter.addObservedPoint(1, x, f.value(x));
    }

    final double[] fitted = fitter.fit();
    Assert.assertEquals(a, fitted[0], 1.0e-13);
    Assert.assertEquals(w, fitted[1], 1.0e-13);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1e-13);

    HarmonicOscillator ff = new HarmonicOscillator(fitted[0], fitted[1], fitted[2]);

    for (double x = -1.0; x < 1.0; x += 0.01) {
        Assert.assertTrue(FastMath.abs(f.value(x) - ff.value(x)) < 1e-13);
    }
}
 
Example #6
Source File: HarmonicCurveFitterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testNoError() {
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    final HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    final WeightedObservedPoints points = new WeightedObservedPoints();
    for (double x = 0.0; x < 1.3; x += 0.01) {
        points.add(1, x, f.value(x));
    }

    final HarmonicCurveFitter fitter = HarmonicCurveFitter.create();
    final double[] fitted = fitter.fit(points.toList());
    Assert.assertEquals(a, fitted[0], 1.0e-13);
    Assert.assertEquals(w, fitted[1], 1.0e-13);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1e-13);

    final HarmonicOscillator ff = new HarmonicOscillator(fitted[0], fitted[1], fitted[2]);
    for (double x = -1.0; x < 1.0; x += 0.01) {
        Assert.assertTrue(FastMath.abs(f.value(x) - ff.value(x)) < 1e-13);
    }
}
 
Example #7
Source File: HarmonicFitterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testNoError() {
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    HarmonicFitter fitter =
        new HarmonicFitter(new LevenbergMarquardtOptimizer());
    for (double x = 0.0; x < 1.3; x += 0.01) {
        fitter.addObservedPoint(1, x, f.value(x));
    }

    final double[] fitted = fitter.fit();
    Assert.assertEquals(a, fitted[0], 1.0e-13);
    Assert.assertEquals(w, fitted[1], 1.0e-13);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1e-13);

    HarmonicOscillator ff = new HarmonicOscillator(fitted[0], fitted[1], fitted[2]);

    for (double x = -1.0; x < 1.0; x += 0.01) {
        Assert.assertTrue(FastMath.abs(f.value(x) - ff.value(x)) < 1e-13);
    }
}
 
Example #8
Source File: HarmonicFitterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test1PercentError() {
    Random randomizer = new Random(64925784252l);
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    HarmonicFitter fitter =
        new HarmonicFitter(new LevenbergMarquardtOptimizer());
    for (double x = 0.0; x < 10.0; x += 0.1) {
        fitter.addObservedPoint(1, x,
                                f.value(x) + 0.01 * randomizer.nextGaussian());
    }

    final double[] fitted = fitter.fit();
    Assert.assertEquals(a, fitted[0], 7.6e-4);
    Assert.assertEquals(w, fitted[1], 2.7e-3);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.3e-2);
}
 
Example #9
Source File: HarmonicFitterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testInitialGuess() {
    Random randomizer = new Random(45314242l);
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    HarmonicFitter fitter =
        new HarmonicFitter(new LevenbergMarquardtOptimizer());
    for (double x = 0.0; x < 10.0; x += 0.1) {
        fitter.addObservedPoint(1, x,
                                f.value(x) + 0.01 * randomizer.nextGaussian());
    }

    final double[] fitted = fitter.fit(new double[] { 0.15, 3.6, 4.5 });
    Assert.assertEquals(a, fitted[0], 1.2e-3);
    Assert.assertEquals(w, fitted[1], 3.3e-3);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.7e-2);
}
 
Example #10
Source File: HarmonicFitterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testInitialGuess() {
    Random randomizer = new Random(45314242l);
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    HarmonicFitter fitter =
        new HarmonicFitter(new LevenbergMarquardtOptimizer());
    for (double x = 0.0; x < 10.0; x += 0.1) {
        fitter.addObservedPoint(1, x,
                                f.value(x) + 0.01 * randomizer.nextGaussian());
    }

    final double[] fitted = fitter.fit(new double[] { 0.15, 3.6, 4.5 });
    Assert.assertEquals(a, fitted[0], 1.2e-3);
    Assert.assertEquals(w, fitted[1], 3.3e-3);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.7e-2);
}
 
Example #11
Source File: HarmonicFitterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test1PercentError() {
    Random randomizer = new Random(64925784252l);
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    HarmonicFitter fitter =
        new HarmonicFitter(new LevenbergMarquardtOptimizer());
    for (double x = 0.0; x < 10.0; x += 0.1) {
        fitter.addObservedPoint(1, x,
                                f.value(x) + 0.01 * randomizer.nextGaussian());
    }

    final double[] fitted = fitter.fit();
    Assert.assertEquals(a, fitted[0], 7.6e-4);
    Assert.assertEquals(w, fitted[1], 2.7e-3);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.3e-2);
}
 
Example #12
Source File: HarmonicFitterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testNoError() {
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    HarmonicFitter fitter =
        new HarmonicFitter(new LevenbergMarquardtOptimizer());
    for (double x = 0.0; x < 1.3; x += 0.01) {
        fitter.addObservedPoint(1, x, f.value(x));
    }

    final double[] fitted = fitter.fit();
    Assert.assertEquals(a, fitted[0], 1.0e-13);
    Assert.assertEquals(w, fitted[1], 1.0e-13);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1e-13);

    HarmonicOscillator ff = new HarmonicOscillator(fitted[0], fitted[1], fitted[2]);

    for (double x = -1.0; x < 1.0; x += 0.01) {
        Assert.assertTrue(FastMath.abs(f.value(x) - ff.value(x)) < 1e-13);
    }
}
 
Example #13
Source File: HarmonicFitterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testNoError() {
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    HarmonicFitter fitter =
        new HarmonicFitter(new LevenbergMarquardtOptimizer());
    for (double x = 0.0; x < 1.3; x += 0.01) {
        fitter.addObservedPoint(1, x, f.value(x));
    }

    final double[] fitted = fitter.fit();
    Assert.assertEquals(a, fitted[0], 1.0e-13);
    Assert.assertEquals(w, fitted[1], 1.0e-13);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1e-13);

    HarmonicOscillator ff = new HarmonicOscillator(fitted[0], fitted[1], fitted[2]);

    for (double x = -1.0; x < 1.0; x += 0.01) {
        Assert.assertTrue(FastMath.abs(f.value(x) - ff.value(x)) < 1e-13);
    }
}
 
Example #14
Source File: HarmonicFitterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test1PercentError() {
    Random randomizer = new Random(64925784252l);
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    HarmonicFitter fitter =
        new HarmonicFitter(new LevenbergMarquardtOptimizer());
    for (double x = 0.0; x < 10.0; x += 0.1) {
        fitter.addObservedPoint(1, x,
                                f.value(x) + 0.01 * randomizer.nextGaussian());
    }

    final double[] fitted = fitter.fit();
    Assert.assertEquals(a, fitted[0], 7.6e-4);
    Assert.assertEquals(w, fitted[1], 2.7e-3);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.3e-2);
}
 
Example #15
Source File: HarmonicFitterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testInitialGuess() {
    Random randomizer = new Random(45314242l);
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    HarmonicFitter fitter =
        new HarmonicFitter(new LevenbergMarquardtOptimizer());
    for (double x = 0.0; x < 10.0; x += 0.1) {
        fitter.addObservedPoint(1, x,
                                f.value(x) + 0.01 * randomizer.nextGaussian());
    }

    final double[] fitted = fitter.fit(new double[] { 0.15, 3.6, 4.5 });
    Assert.assertEquals(a, fitted[0], 1.2e-3);
    Assert.assertEquals(w, fitted[1], 3.3e-3);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.7e-2);
}
 
Example #16
Source File: HarmonicFitterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testNoError() {
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    HarmonicFitter fitter =
        new HarmonicFitter(new LevenbergMarquardtOptimizer());
    for (double x = 0.0; x < 1.3; x += 0.01) {
        fitter.addObservedPoint(1, x, f.value(x));
    }

    final double[] fitted = fitter.fit();
    Assert.assertEquals(a, fitted[0], 1.0e-13);
    Assert.assertEquals(w, fitted[1], 1.0e-13);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1e-13);

    HarmonicOscillator ff = new HarmonicOscillator(fitted[0], fitted[1], fitted[2]);

    for (double x = -1.0; x < 1.0; x += 0.01) {
        Assert.assertTrue(FastMath.abs(f.value(x) - ff.value(x)) < 1e-13);
    }
}
 
Example #17
Source File: HarmonicFitterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test1PercentError() {
    Random randomizer = new Random(64925784252l);
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    HarmonicFitter fitter =
        new HarmonicFitter(new LevenbergMarquardtOptimizer());
    for (double x = 0.0; x < 10.0; x += 0.1) {
        fitter.addObservedPoint(1, x,
                                f.value(x) + 0.01 * randomizer.nextGaussian());
    }

    final double[] fitted = fitter.fit();
    Assert.assertEquals(a, fitted[0], 7.6e-4);
    Assert.assertEquals(w, fitted[1], 2.7e-3);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.3e-2);
}
 
Example #18
Source File: HarmonicFitterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testInitialGuess() {
    Random randomizer = new Random(45314242l);
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    HarmonicFitter fitter =
        new HarmonicFitter(new LevenbergMarquardtOptimizer());
    for (double x = 0.0; x < 10.0; x += 0.1) {
        fitter.addObservedPoint(1, x,
                                f.value(x) + 0.01 * randomizer.nextGaussian());
    }

    final double[] fitted = fitter.fit(new double[] { 0.15, 3.6, 4.5 });
    Assert.assertEquals(a, fitted[0], 1.2e-3);
    Assert.assertEquals(w, fitted[1], 3.3e-3);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.7e-2);
}
 
Example #19
Source File: HarmonicFitterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testInitialGuess() {
    Random randomizer = new Random(45314242l);
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    HarmonicFitter fitter =
        new HarmonicFitter(new LevenbergMarquardtOptimizer());
    for (double x = 0.0; x < 10.0; x += 0.1) {
        fitter.addObservedPoint(1, x,
                                f.value(x) + 0.01 * randomizer.nextGaussian());
    }

    final double[] fitted = fitter.fit(new double[] { 0.15, 3.6, 4.5 });
    Assert.assertEquals(a, fitted[0], 1.2e-3);
    Assert.assertEquals(w, fitted[1], 3.3e-3);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.7e-2);
}
 
Example #20
Source File: HarmonicFitterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testNoError() {
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    HarmonicFitter fitter =
        new HarmonicFitter(new LevenbergMarquardtOptimizer());
    for (double x = 0.0; x < 1.3; x += 0.01) {
        fitter.addObservedPoint(1, x, f.value(x));
    }

    final double[] fitted = fitter.fit();
    Assert.assertEquals(a, fitted[0], 1.0e-13);
    Assert.assertEquals(w, fitted[1], 1.0e-13);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1e-13);

    HarmonicOscillator ff = new HarmonicOscillator(fitted[0], fitted[1], fitted[2]);

    for (double x = -1.0; x < 1.0; x += 0.01) {
        Assert.assertTrue(FastMath.abs(f.value(x) - ff.value(x)) < 1e-13);
    }
}
 
Example #21
Source File: HarmonicFitterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test1PercentError() {
    Random randomizer = new Random(64925784252l);
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    HarmonicFitter fitter =
        new HarmonicFitter(new LevenbergMarquardtOptimizer());
    for (double x = 0.0; x < 10.0; x += 0.1) {
        fitter.addObservedPoint(1, x,
                                f.value(x) + 0.01 * randomizer.nextGaussian());
    }

    final double[] fitted = fitter.fit();
    Assert.assertEquals(a, fitted[0], 7.6e-4);
    Assert.assertEquals(w, fitted[1], 2.7e-3);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.3e-2);
}
 
Example #22
Source File: HarmonicFitterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testInitialGuess() {
    Random randomizer = new Random(45314242l);
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    HarmonicFitter fitter =
        new HarmonicFitter(new LevenbergMarquardtOptimizer());
    for (double x = 0.0; x < 10.0; x += 0.1) {
        fitter.addObservedPoint(1, x,
                                f.value(x) + 0.01 * randomizer.nextGaussian());
    }

    final double[] fitted = fitter.fit(new double[] { 0.15, 3.6, 4.5 });
    Assert.assertEquals(a, fitted[0], 1.2e-3);
    Assert.assertEquals(w, fitted[1], 3.3e-3);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.7e-2);
}
 
Example #23
Source File: HarmonicFitterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test1PercentError() {
    Random randomizer = new Random(64925784252l);
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    HarmonicFitter fitter =
        new HarmonicFitter(new LevenbergMarquardtOptimizer());
    for (double x = 0.0; x < 10.0; x += 0.1) {
        fitter.addObservedPoint(1, x,
                                f.value(x) + 0.01 * randomizer.nextGaussian());
    }

    final double[] fitted = fitter.fit();
    Assert.assertEquals(a, fitted[0], 7.6e-4);
    Assert.assertEquals(w, fitted[1], 2.7e-3);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.3e-2);
}
 
Example #24
Source File: HarmonicFitterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testNoError() {
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    HarmonicFitter fitter =
        new HarmonicFitter(new LevenbergMarquardtOptimizer());
    for (double x = 0.0; x < 1.3; x += 0.01) {
        fitter.addObservedPoint(1, x, f.value(x));
    }

    final double[] fitted = fitter.fit();
    Assert.assertEquals(a, fitted[0], 1.0e-13);
    Assert.assertEquals(w, fitted[1], 1.0e-13);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1e-13);

    HarmonicOscillator ff = new HarmonicOscillator(fitted[0], fitted[1], fitted[2]);

    for (double x = -1.0; x < 1.0; x += 0.01) {
        Assert.assertTrue(FastMath.abs(f.value(x) - ff.value(x)) < 1e-13);
    }
}
 
Example #25
Source File: HarmonicFitterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testNoError() {
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    HarmonicFitter fitter =
        new HarmonicFitter(new LevenbergMarquardtOptimizer());
    for (double x = 0.0; x < 1.3; x += 0.01) {
        fitter.addObservedPoint(1, x, f.value(x));
    }

    final double[] fitted = fitter.fit();
    Assert.assertEquals(a, fitted[0], 1.0e-13);
    Assert.assertEquals(w, fitted[1], 1.0e-13);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1e-13);

    HarmonicOscillator ff = new HarmonicOscillator(fitted[0], fitted[1], fitted[2]);

    for (double x = -1.0; x < 1.0; x += 0.01) {
        Assert.assertTrue(FastMath.abs(f.value(x) - ff.value(x)) < 1e-13);
    }
}
 
Example #26
Source File: HarmonicFitterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test1PercentError() {
    Random randomizer = new Random(64925784252l);
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    HarmonicFitter fitter =
        new HarmonicFitter(new LevenbergMarquardtOptimizer());
    for (double x = 0.0; x < 10.0; x += 0.1) {
        fitter.addObservedPoint(1, x,
                                f.value(x) + 0.01 * randomizer.nextGaussian());
    }

    final double[] fitted = fitter.fit();
    Assert.assertEquals(a, fitted[0], 7.6e-4);
    Assert.assertEquals(w, fitted[1], 2.7e-3);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.3e-2);
}
 
Example #27
Source File: HarmonicFitterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testInitialGuess() {
    Random randomizer = new Random(45314242l);
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    HarmonicFitter fitter =
        new HarmonicFitter(new LevenbergMarquardtOptimizer());
    for (double x = 0.0; x < 10.0; x += 0.1) {
        fitter.addObservedPoint(1, x,
                                f.value(x) + 0.01 * randomizer.nextGaussian());
    }

    final double[] fitted = fitter.fit(new double[] { 0.15, 3.6, 4.5 });
    Assert.assertEquals(a, fitted[0], 1.2e-3);
    Assert.assertEquals(w, fitted[1], 3.3e-3);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.7e-2);
}
 
Example #28
Source File: HarmonicFitterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testInitialGuess() {
    Random randomizer = new Random(45314242l);
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    HarmonicFitter fitter =
        new HarmonicFitter(new LevenbergMarquardtOptimizer());
    for (double x = 0.0; x < 10.0; x += 0.1) {
        fitter.addObservedPoint(1, x,
                                f.value(x) + 0.01 * randomizer.nextGaussian());
    }

    final double[] fitted = fitter.fit(new double[] { 0.15, 3.6, 4.5 });
    Assert.assertEquals(a, fitted[0], 1.2e-3);
    Assert.assertEquals(w, fitted[1], 3.3e-3);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.7e-2);
}
 
Example #29
Source File: HarmonicFitterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testNoError() {
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    HarmonicFitter fitter =
        new HarmonicFitter(new LevenbergMarquardtOptimizer());
    for (double x = 0.0; x < 1.3; x += 0.01) {
        fitter.addObservedPoint(1, x, f.value(x));
    }

    final double[] fitted = fitter.fit();
    Assert.assertEquals(a, fitted[0], 1.0e-13);
    Assert.assertEquals(w, fitted[1], 1.0e-13);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1e-13);

    HarmonicOscillator ff = new HarmonicOscillator(fitted[0], fitted[1], fitted[2]);

    for (double x = -1.0; x < 1.0; x += 0.01) {
        Assert.assertTrue(FastMath.abs(f.value(x) - ff.value(x)) < 1e-13);
    }
}
 
Example #30
Source File: HarmonicFitterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test1PercentError() {
    Random randomizer = new Random(64925784252l);
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    HarmonicFitter fitter =
        new HarmonicFitter(new LevenbergMarquardtOptimizer());
    for (double x = 0.0; x < 10.0; x += 0.1) {
        fitter.addObservedPoint(1, x,
                                f.value(x) + 0.01 * randomizer.nextGaussian());
    }

    final double[] fitted = fitter.fit();
    Assert.assertEquals(a, fitted[0], 7.6e-4);
    Assert.assertEquals(w, fitted[1], 2.7e-3);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.3e-2);
}