View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.math4.legacy.ode;
19  
20  import org.apache.commons.math4.core.jdkmath.JdkMath;
21  
22  /**
23   * This class is used in the junit tests for the ODE integrators.
24  
25   * <p>This specific problem is the following differential equation :
26   * <pre>
27   *    y' = -y
28   * </pre>
29   * the solution of this equation is a simple exponential function :
30   * <pre>
31   *   y (t) = y (t0) exp (t0-t)
32   * </pre>
33   * </p>
34  
35   */
36  public class TestProblem1
37    extends TestProblemAbstract {
38  
39    /** theoretical state */
40    private double[] y;
41  
42    /**
43     * Simple constructor.
44     */
45    public TestProblem1() {
46      super();
47      double[] y0 = { 1.0, 0.1 };
48      setInitialConditions(0.0, y0);
49      setFinalConditions(4.0);
50      double[] errorScale = { 1.0, 1.0 };
51      setErrorScale(errorScale);
52      y = new double[y0.length];
53    }
54  
55    @Override
56    public void doComputeDerivatives(double t, double[] y, double[] yDot) {
57  
58      // compute the derivatives
59      for (int i = 0; i < getDimension(); ++i) {
60          yDot[i] = -y[i];
61      }
62    }
63  
64    @Override
65    public double[] computeTheoreticalState(double t) {
66      double c = JdkMath.exp (getInitialTime() - t);
67      for (int i = 0; i < getDimension(); ++i) {
68        y[i] = c * getInitialState()[i];
69      }
70      return y;
71    }
72  }