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.math.ode;
19  
20  /**
21   * This class is used in the junit tests for the ODE integrators.
22  
23   * <p>This specific problem is the following differential equation :
24   * <pre>
25   *    y' = -y
26   * </pre>
27   * the solution of this equation is a simple exponential function :
28   * <pre>
29   *   y (t) = y (t0) exp (t0-t)
30   * </pre>
31   * </p>
32  
33   */
34  public class TestProblem1
35    extends TestProblemAbstract {
36  
37    /** Serializable version identifier. */
38    private static final long serialVersionUID = 1977870815289373164L;
39  
40    /** theoretical state */
41    private double[] y;
42  
43    /**
44     * Simple constructor.
45     */
46    public TestProblem1() {
47      super();
48      double[] y0 = { 1.0, 0.1 };
49      setInitialConditions(0.0, y0);
50      setFinalConditions(4.0);
51      double[] errorScale = { 1.0, 1.0 };
52      setErrorScale(errorScale);
53      y = new double[y0.length];
54    }
55   
56    /**
57     * Copy constructor.
58     * @param problem problem to copy
59     */
60    public TestProblem1(TestProblem1 problem) {
61      super(problem);
62      y = problem.y.clone();
63    }
64  
65    /** {@inheritDoc} */
66    public TestProblem1 copy() {
67      return new TestProblem1(this);
68    }
69  
70    @Override
71    public void doComputeDerivatives(double t, double[] y, double[] yDot) {
72  
73      // compute the derivatives
74      for (int i = 0; i < n; ++i)
75        yDot[i] = -y[i];
76  
77    }
78  
79    @Override
80    public double[] computeTheoreticalState(double t) {
81      double c = Math.exp (t0 - t);
82      for (int i = 0; i < n; ++i) {
83        y[i] = c * y0[i];
84      }
85      return y;
86    }
87  
88  }