001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.math.ode;
019    
020    /**
021     * This class is used in the junit tests for the ODE integrators.
022    
023     * <p>This specific problem is the following differential equation :
024     * <pre>
025     *    y' = t^3 - t y
026     * </pre>
027     * with the initial condition y (0) = 0. The solution of this equation
028     * is the following function :
029     * <pre>
030     *   y (t) = t^2 + 2 (exp (- t^2 / 2) - 1)
031     * </pre>
032     * </p>
033    
034     */
035    public class TestProblem2
036      extends TestProblemAbstract {
037    
038      /** Serializable version identifier. */
039      private static final long serialVersionUID = 8330741783213512366L;
040    
041      /** theoretical state */
042      private double[] y;
043    
044      /**
045       * Simple constructor.
046       */
047      public TestProblem2() {
048        super();
049        double[] y0 = { 0.0 };
050        setInitialConditions(0.0, y0);
051        setFinalConditions(1.0);
052        double[] errorScale = { 1.0 };
053        setErrorScale(errorScale);
054        y = new double[y0.length];
055      }
056     
057      /**
058       * Copy constructor.
059       * @param problem problem to copy
060       */
061      public TestProblem2(TestProblem2 problem) {
062        super(problem);
063        y = problem.y.clone();
064      }
065    
066      /** {@inheritDoc} */
067      public TestProblem2 copy() {
068        return new TestProblem2(this);
069      }
070    
071      @Override
072      public void doComputeDerivatives(double t, double[] y, double[] yDot) {
073    
074        // compute the derivatives
075        for (int i = 0; i < n; ++i)
076          yDot[i] = t * (t * t - y[i]);
077    
078      }
079    
080      @Override
081      public double[] computeTheoreticalState(double t) {
082        double t2 = t * t;
083        double c = t2 + 2 * (Math.exp (-0.5 * t2) - 1);
084        for (int i = 0; i < n; ++i) {
085          y[i] = c;
086        }
087        return y;
088      }
089    
090    }