public class EulerMethod extends RungeKutta
The integer constructor specifies the number of grid points,
including both endpoints: new EulerMethod(6) uses five intervals.
The double constructor specifies a step size: new EulerMethod(0.2).
For a forward interval supply a positive step size. Read the returned
x() values instead of assuming an exactly uniform grid, because the
last grid point is replaced with the requested endpoint.
Euler is first order and can be unstable. Check convergence by reducing the step size, or choose an appropriate higher-order solver.
import dev.nm.algebra.linear.vector.doubles.Vector;
import dev.nm.algebra.linear.vector.doubles.dense.DenseVector;
import dev.nm.analysis.differentialequation.ode.ivp.problem.DerivativeFunction;
import dev.nm.analysis.differentialequation.ode.ivp.problem.ODE1stOrder;
import dev.nm.analysis.differentialequation.ode.ivp.solver.EulerMethod;
import dev.nm.analysis.differentialequation.ode.ivp.solver.ODESolution;
DerivativeFunction derivative = new DerivativeFunction() {
public int dimension() { return 1; }
public Vector evaluate(double t, Vector y) {
return new DenseVector(new double[]{-y.get(1)});
}
};
ODE1stOrder problem = new ODE1stOrder(derivative,
new DenseVector(new double[]{1.0}), 0.0, 1.0);
ODESolution solution = new EulerMethod(6).solve(problem);
assert solution.x().length == 6; // arrays use zero-based indexing
assert solution.x()[0] == 0.0;
assert solution.x()[5] == 1.0;
assert Math.abs(solution.y()[5].get(1) - Math.pow(0.8, 5)) < 1e-12;
| Constructor and Description |
|---|
EulerMethod(double h)
Constructs an Euler's method instance, with the given step size.
|
EulerMethod(int N)
Constructs an Euler's method instance, with the given number of grid points, including both endpoints.
|
public EulerMethod(double h)
h - the constant step sizepublic EulerMethod(int N)
N - the number of grid points, including both endpoints; at least 2Copyright © 2010-2026 NM Group. All Rights Reserved.