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.legacy.core.Field;
21  import org.apache.commons.math4.legacy.core.RealFieldElement;
22  import org.apache.commons.math4.legacy.core.MathArrays;
23  
24  /**
25   * This class is used in the junit tests for the ODE integrators.
26  
27   * <p>This specific problem is the following differential equation :
28   * <pre>
29   *    y' = t^3 - t y
30   * </pre>
31   * with the initial condition y (0) = 0. The solution of this equation
32   * is the following function :
33   * <pre>
34   *   y (t) = t^2 + 2 (exp (- t^2 / 2) - 1)
35   * </pre>
36   * </p>
37  
38   * @param <T> the type of the field elements
39   */
40  public class TestFieldProblem2<T extends RealFieldElement<T>>
41      extends TestFieldProblemAbstract<T> {
42  
43      /**
44       * Simple constructor.
45       * @param field field to which elements belong
46       */
47      public TestFieldProblem2(Field<T> field) {
48          super(field);
49          setInitialConditions(convert(0.0), convert(new double[] { 0.0 }));
50          setFinalConditions(convert(1.0));
51          setErrorScale(convert(new double[] { 1.0 }));
52      }
53  
54      @Override
55      public T[] doComputeDerivatives(T t, T[] y) {
56  
57          final T[] yDot = MathArrays.buildArray(getField(), getDimension());
58          // compute the derivatives
59          for (int i = 0; i < getDimension(); ++i) {
60              yDot[i] = t.multiply(t.multiply(t).subtract(y[i]));
61          }
62  
63          return yDot;
64      }
65  
66      @Override
67      public T[] computeTheoreticalState(T t) {
68          final T[] y = MathArrays.buildArray(getField(), getDimension());
69          T t2 = t.multiply(t);
70          T c = t2.add(t2.multiply(-0.5).exp().subtract(1).multiply(2));
71          for (int i = 0; i < getDimension(); ++i) {
72              y[i] = c;
73          }
74          return y;
75      }
76  }