/** * Fit an harmonic function to the observed points. * * @return harmonic function best fitting the observed points * @throws OptimizationException if the sample is too short or if the first guess cannot be * computed */ public HarmonicFunction fit() throws OptimizationException { // shall we compute the first guess of the parameters ourselves ? if (parameters == null) { final WeightedObservedPoint[] observations = fitter.getObservations(); if (observations.length < 4) { throw new OptimizationException( LocalizedFormats.INSUFFICIENT_OBSERVED_POINTS_IN_SAMPLE, observations.length, 4); } HarmonicCoefficientsGuesser guesser = new HarmonicCoefficientsGuesser(observations); guesser.guess(); parameters = new double[] { guesser.getGuessedAmplitude(), guesser.getGuessedPulsation(), guesser.getGuessedPhase() }; } try { double[] fitted = fitter.fit(new ParametricHarmonicFunction(), parameters); return new HarmonicFunction(fitted[0], fitted[1], fitted[2]); } catch (FunctionEvaluationException fee) { // should never happen throw new RuntimeException(fee); } }
/** * Add an observed weighted (x,y) point to the sample. * * @param weight weight of the observed point in the fit * @param x abscissa of the point * @param y observed value of the point at x, after fitting we should have P(x) as close as * possible to this value */ public void addObservedPoint(double weight, double x, double y) { fitter.addObservedPoint(weight, x, y); }