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' = 3x^5 - y
30   * </pre>
31   * when the initial condition is y(0) = -360, the solution of this
32   * equation degenerates to a simple quintic polynomial function :
33   * <pre>
34   *   y (t) = 3x^5 - 15x^4 + 60x^3 - 180x^2 + 360x - 360
35   * </pre>
36   * </p>
37  
38   * @param <T> the type of the field elements
39   */
40  public class TestFieldProblem6<T extends RealFieldElement<T>>
41      extends TestFieldProblemAbstract<T> {
42  
43      /**
44       * Simple constructor.
45       * @param field field to which elements belong
46       */
47      public TestFieldProblem6(Field<T> field) {
48          super(field);
49          setInitialConditions(convert(0.0), convert( new double[] { -360.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  
59          // compute the derivatives
60          T t2 = t.multiply(t);
61          T t4 = t2.multiply(t2);
62          T t5 = t4.multiply(t);
63          for (int i = 0; i < getDimension(); ++i) {
64              yDot[i] = t5.multiply(3).subtract(y[i]);
65          }
66  
67          return yDot;
68      }
69  
70      @Override
71      public T[] computeTheoreticalState(T t) {
72  
73          final T[] y = MathArrays.buildArray(getField(), getDimension());
74  
75          for (int i = 0; i < getDimension(); ++i) {
76              y[i] = t.multiply(3).subtract(15).multiply(t).add(60).multiply(t).subtract(180).multiply(t).add(360).multiply(t).subtract(360);
77          }
78  
79          return y;
80      }
81  }