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