mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-23 20:44:39 +08:00
@ -37,15 +37,8 @@ public class EulerMethod {
|
||||
|
||||
// example from https://www.geeksforgeeks.org/euler-method-solving-differential-equation/
|
||||
System.out.println("\n\nexample 3:");
|
||||
BiFunction<Double, Double, Double> exampleEquation3 = (x, y) ->
|
||||
x + y + x * y;
|
||||
ArrayList<double[]> points3 = eulerFull(
|
||||
0,
|
||||
0.1,
|
||||
0.025,
|
||||
1,
|
||||
exampleEquation3
|
||||
);
|
||||
BiFunction<Double, Double, Double> exampleEquation3 = (x, y) -> x + y + x * y;
|
||||
ArrayList<double[]> points3 = eulerFull(0, 0.1, 0.025, 1, exampleEquation3);
|
||||
assert points3.get(points3.size() - 1)[1] == 1.1116729841674804;
|
||||
points3.forEach(point -> System.out.printf("x: %1$f; y: %2$f%n", point[0], point[1]));
|
||||
}
|
||||
@ -60,16 +53,10 @@ public class EulerMethod {
|
||||
* @param differentialEquation The differential equation to be solved.
|
||||
* @return The next y-value.
|
||||
*/
|
||||
public static double eulerStep(
|
||||
double xCurrent,
|
||||
double stepSize,
|
||||
double yCurrent,
|
||||
BiFunction<Double, Double, Double> differentialEquation
|
||||
) {
|
||||
public static double eulerStep(double xCurrent, double stepSize, double yCurrent,
|
||||
BiFunction<Double, Double, Double> differentialEquation) {
|
||||
if (stepSize <= 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"stepSize should be greater than zero"
|
||||
);
|
||||
throw new IllegalArgumentException("stepSize should be greater than zero");
|
||||
}
|
||||
return yCurrent + stepSize * differentialEquation.apply(xCurrent, yCurrent);
|
||||
}
|
||||
@ -86,36 +73,26 @@ public class EulerMethod {
|
||||
* @return The points constituting the solution of the differential
|
||||
* equation.
|
||||
*/
|
||||
public static ArrayList<double[]> eulerFull(
|
||||
double xStart,
|
||||
double xEnd,
|
||||
double stepSize,
|
||||
double yStart,
|
||||
BiFunction<Double, Double, Double> differentialEquation
|
||||
) {
|
||||
public static ArrayList<double[]> eulerFull(double xStart, double xEnd, double stepSize,
|
||||
double yStart, BiFunction<Double, Double, Double> differentialEquation) {
|
||||
if (xStart >= xEnd) {
|
||||
throw new IllegalArgumentException(
|
||||
"xEnd should be greater than xStart"
|
||||
);
|
||||
throw new IllegalArgumentException("xEnd should be greater than xStart");
|
||||
}
|
||||
if (stepSize <= 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"stepSize should be greater than zero"
|
||||
);
|
||||
throw new IllegalArgumentException("stepSize should be greater than zero");
|
||||
}
|
||||
|
||||
ArrayList<double[]> points = new ArrayList<double[]>();
|
||||
double[] firstPoint = { xStart, yStart };
|
||||
double[] firstPoint = {xStart, yStart};
|
||||
points.add(firstPoint);
|
||||
double yCurrent = yStart;
|
||||
double xCurrent = xStart;
|
||||
|
||||
while (xCurrent < xEnd) {
|
||||
// Euler method for next step
|
||||
yCurrent =
|
||||
eulerStep(xCurrent, stepSize, yCurrent, differentialEquation);
|
||||
yCurrent = eulerStep(xCurrent, stepSize, yCurrent, differentialEquation);
|
||||
xCurrent += stepSize;
|
||||
double[] point = { xCurrent, yCurrent };
|
||||
double[] point = {xCurrent, yCurrent};
|
||||
points.add(point);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user