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' = -y
026     * </pre>
027     * the solution of this equation is a simple exponential function :
028     * <pre>
029     *   y (t) = y (t0) exp (t0-t)
030     * </pre>
031     * </p>
032    
033     */
034    public class TestProblem1
035      extends TestProblemAbstract {
036    
037      /** Serializable version identifier. */
038      private static final long serialVersionUID = 1977870815289373164L;
039    
040      /** theoretical state */
041      private double[] y;
042    
043      /**
044       * Simple constructor.
045       */
046      public TestProblem1() {
047        super();
048        double[] y0 = { 1.0, 0.1 };
049        setInitialConditions(0.0, y0);
050        setFinalConditions(4.0);
051        double[] errorScale = { 1.0, 1.0 };
052        setErrorScale(errorScale);
053        y = new double[y0.length];
054      }
055     
056      /**
057       * Copy constructor.
058       * @param problem problem to copy
059       */
060      public TestProblem1(TestProblem1 problem) {
061        super(problem);
062        y = problem.y.clone();
063      }
064    
065      /** {@inheritDoc} */
066      public TestProblem1 copy() {
067        return new TestProblem1(this);
068      }
069    
070      @Override
071      public void doComputeDerivatives(double t, double[] y, double[] yDot) {
072    
073        // compute the derivatives
074        for (int i = 0; i < n; ++i)
075          yDot[i] = -y[i];
076    
077      }
078    
079      @Override
080      public double[] computeTheoreticalState(double t) {
081        double c = Math.exp (t0 - t);
082        for (int i = 0; i < n; ++i) {
083          y[i] = c * y0[i];
084        }
085        return y;
086      }
087    
088    }