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 018package org.apache.commons.math3.ode.nonstiff; 019 020import org.apache.commons.math3.Field; 021import org.apache.commons.math3.RealFieldElement; 022import org.apache.commons.math3.ode.FieldEquationsMapper; 023import org.apache.commons.math3.ode.FieldODEStateAndDerivative; 024import org.apache.commons.math3.util.MathArrays; 025 026/** 027 * This class implements a simple Euler integrator for Ordinary 028 * Differential Equations. 029 * 030 * <p>The Euler algorithm is the simplest one that can be used to 031 * integrate ordinary differential equations. It is a simple inversion 032 * of the forward difference expression : 033 * <code>f'=(f(t+h)-f(t))/h</code> which leads to 034 * <code>f(t+h)=f(t)+hf'</code>. The interpolation scheme used for 035 * dense output is the linear scheme already used for integration.</p> 036 * 037 * <p>This algorithm looks cheap because it needs only one function 038 * evaluation per step. However, as it uses linear estimates, it needs 039 * very small steps to achieve high accuracy, and small steps lead to 040 * numerical errors and instabilities.</p> 041 * 042 * <p>This algorithm is almost never used and has been included in 043 * this package only as a comparison reference for more useful 044 * integrators.</p> 045 * 046 * @see MidpointFieldIntegrator 047 * @see ClassicalRungeKuttaFieldIntegrator 048 * @see GillFieldIntegrator 049 * @see ThreeEighthesFieldIntegrator 050 * @see LutherFieldIntegrator 051 * @param <T> the type of the field elements 052 * @since 3.6 053 */ 054 055public class EulerFieldIntegrator<T extends RealFieldElement<T>> extends RungeKuttaFieldIntegrator<T> { 056 057 /** Simple constructor. 058 * Build an Euler integrator with the given step. 059 * @param field field to which the time and state vector elements belong 060 * @param step integration step 061 */ 062 public EulerFieldIntegrator(final Field<T> field, final T step) { 063 super(field, "Euler", step); 064 } 065 066 /** {@inheritDoc} */ 067 public T[] getC() { 068 return MathArrays.buildArray(getField(), 0); 069 } 070 071 /** {@inheritDoc} */ 072 public T[][] getA() { 073 return MathArrays.buildArray(getField(), 0, 0); 074 } 075 076 /** {@inheritDoc} */ 077 public T[] getB() { 078 final T[] b = MathArrays.buildArray(getField(), 1); 079 b[0] = getField().getOne(); 080 return b; 081 } 082 083 /** {@inheritDoc} */ 084 @Override 085 protected EulerFieldStepInterpolator<T> 086 createInterpolator(final boolean forward, T[][] yDotK, 087 final FieldODEStateAndDerivative<T> globalPreviousState, 088 final FieldODEStateAndDerivative<T> globalCurrentState, 089 final FieldEquationsMapper<T> mapper) { 090 return new EulerFieldStepInterpolator<T>(getField(), forward, yDotK, 091 globalPreviousState, globalCurrentState, 092 globalPreviousState, globalCurrentState, 093 mapper); 094 } 095 096}