Skip to content

Commit 7287231

Browse files
authored
Add Euler method (from master) (TheAlgorithms#2148)
* readded EulerMethod.java after sync * add package
1 parent a850509 commit 7287231

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

Maths/EulerMethod.java

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package Maths;
2+
3+
import java.util.ArrayList;
4+
import java.util.function.BiFunction;
5+
6+
/**
7+
* In mathematics and computational science, the Euler method (also called forward Euler method) is
8+
* a first-order numerical procedure for solving ordinary differential equations (ODEs) with a given
9+
* initial value. It is the most basic explicit method for numerical integration of ordinary
10+
* differential equations. The method proceeds in a series of steps. At each step the y-value is
11+
* calculated by evaluating the differential equation at the previous step, multiplying the result
12+
* with the step-size and adding it to the last y-value: y_n+1 = y_n + stepSize * f(x_n, y_n).
13+
* (description adapted from https://en.wikipedia.org/wiki/Euler_method ) (see also:
14+
* https://www.geeksforgeeks.org/euler-method-solving-differential-equation/ )
15+
*/
16+
public class EulerMethod {
17+
18+
/** Illustrates how the algorithm is used in 3 examples and prints the results to the console. */
19+
public static void main(String[] args) {
20+
System.out.println("example 1:");
21+
BiFunction<Double, Double, Double> exampleEquation1 = (x, y) -> x;
22+
ArrayList<double[]> points1 = eulerFull(0, 4, 0.1, 0, exampleEquation1);
23+
assert points1.get(points1.size() - 1)[1] == 7.800000000000003;
24+
points1.forEach(
25+
point -> System.out.println(String.format("x: %1$f; y: %2$f", point[0], point[1])));
26+
27+
// example from https://en.wikipedia.org/wiki/Euler_method
28+
System.out.println("\n\nexample 2:");
29+
BiFunction<Double, Double, Double> exampleEquation2 = (x, y) -> y;
30+
ArrayList<double[]> points2 = eulerFull(0, 4, 0.1, 1, exampleEquation2);
31+
assert points2.get(points2.size() - 1)[1] == 45.25925556817596;
32+
points2.forEach(
33+
point -> System.out.println(String.format("x: %1$f; y: %2$f", point[0], point[1])));
34+
35+
// example from https://www.geeksforgeeks.org/euler-method-solving-differential-equation/
36+
System.out.println("\n\nexample 3:");
37+
BiFunction<Double, Double, Double> exampleEquation3 = (x, y) -> x + y + x * y;
38+
ArrayList<double[]> points3 = eulerFull(0, 0.1, 0.025, 1, exampleEquation3);
39+
assert points3.get(points3.size() - 1)[1] == 1.1116729841674804;
40+
points3.forEach(
41+
point -> System.out.println(String.format("x: %1$f; y: %2$f", point[0], point[1])));
42+
}
43+
44+
/**
45+
* calculates the next y-value based on the current value of x, y and the stepSize the console.
46+
*
47+
* @param xCurrent Current x-value.
48+
* @param stepSize Step-size on the x-axis.
49+
* @param yCurrent Current y-value.
50+
* @param differentialEquation The differential equation to be solved.
51+
* @return The next y-value.
52+
*/
53+
public static double eulerStep(
54+
double xCurrent,
55+
double stepSize,
56+
double yCurrent,
57+
BiFunction<Double, Double, Double> differentialEquation) {
58+
if (stepSize <= 0) {
59+
throw new IllegalArgumentException("stepSize should be greater than zero");
60+
}
61+
double yNext = yCurrent + stepSize * differentialEquation.apply(xCurrent, yCurrent);
62+
return yNext;
63+
}
64+
65+
/**
66+
* Loops through all the steps until xEnd is reached, adds a point for each step and then returns
67+
* all the points
68+
*
69+
* @param xStart First x-value.
70+
* @param xEnd Last x-value.
71+
* @param stepSize Step-size on the x-axis.
72+
* @param yStart First y-value.
73+
* @param differentialEquation The differential equation to be solved.
74+
* @return The points constituting the solution of the differential equation.
75+
*/
76+
public static ArrayList<double[]> eulerFull(
77+
double xStart,
78+
double xEnd,
79+
double stepSize,
80+
double yStart,
81+
BiFunction<Double, Double, Double> differentialEquation) {
82+
if (xStart >= xEnd) {
83+
throw new IllegalArgumentException("xEnd should be greater than xStart");
84+
}
85+
if (stepSize <= 0) {
86+
throw new IllegalArgumentException("stepSize should be greater than zero");
87+
}
88+
89+
ArrayList<double[]> points = new ArrayList<double[]>();
90+
double[] firstPoint = {xStart, yStart};
91+
points.add(firstPoint);
92+
double yCurrent = yStart;
93+
double xCurrent = xStart;
94+
95+
while (xCurrent < xEnd) {
96+
// Euler method for next step
97+
yCurrent = eulerStep(xCurrent, stepSize, yCurrent, differentialEquation);
98+
xCurrent += stepSize;
99+
double[] point = {xCurrent, yCurrent};
100+
points.add(point);
101+
}
102+
103+
return points;
104+
}
105+
}

0 commit comments

Comments
 (0)