Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
Java/src/main/java/com/thealgorithms/maths/EulerMethod.java at master · datpp/Java · GitHub
Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Small and medium teams
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
datpp
Java
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
Java
/
src
/
main
/
java
/
com
/
thealgorithms
/
maths
/
EulerMethod.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
101 lines (91 loc) · 4.44 KB
master
Breadcrumbs
Java
/
src
/
main
/
java
/
com
/
thealgorithms
/
maths
/
EulerMethod.java
Copy path
Top
File metadata and controls
Code
Blame
101 lines (91 loc) · 4.44 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package
com
.
thealgorithms
.
maths
;
import
java
.
util
.
ArrayList
;
import
java
.
util
.
function
.
BiFunction
;
/**
* In mathematics and computational science, the Euler method (also called
* forward Euler method) is a first-order numerical procedure for solving
* ordinary differential equations (ODEs) with a given initial value. It is the
* most basic explicit method for numerical integration of ordinary differential
* equations. The method proceeds in a series of steps. At each step the y-value
* is calculated by evaluating the differential equation at the previous step,
* multiplying the result with the step-size and adding it to the last y-value:
* y_n+1 = y_n + stepSize * f(x_n, y_n). (description adapted from
* https://en.wikipedia.org/wiki/Euler_method ) (see also:
* https://www.geeksforgeeks.org/euler-method-solving-differential-equation/ )
*/
public
final
class
EulerMethod
{
private
EulerMethod
() {
}
/**
* Illustrates how the algorithm is used in 3 examples and prints the
* results to the console.
*/
public
static
void
main
(
String
[]
args
) {
System
.
out
.
println
(
"example 1:"
);
BiFunction
<
Double
,
Double
,
Double
>
exampleEquation1
= (
x
,
y
) ->
x
;
ArrayList
<
double
[]>
points1
=
eulerFull
(
0
,
4
,
0.1
,
0
,
exampleEquation1
);
assert
points1
.
get
(
points1
.
size
() -
1
)[
1
] ==
7.800000000000003
;
points1
.
forEach
(
point
->
System
.
out
.
printf
(
"x: %1$f; y: %2$f%n"
,
point
[
0
],
point
[
1
]));
// example from https://en.wikipedia.org/wiki/Euler_method
System
.
out
.
println
(
"
\n
\n
example 2:"
);
BiFunction
<
Double
,
Double
,
Double
>
exampleEquation2
= (
x
,
y
) ->
y
;
ArrayList
<
double
[]>
points2
=
eulerFull
(
0
,
4
,
0.1
,
1
,
exampleEquation2
);
assert
points2
.
get
(
points2
.
size
() -
1
)[
1
] ==
45.25925556817596
;
points2
.
forEach
(
point
->
System
.
out
.
printf
(
"x: %1$f; y: %2$f%n"
,
point
[
0
],
point
[
1
]));
// example from https://www.geeksforgeeks.org/euler-method-solving-differential-equation/
System
.
out
.
println
(
"
\n
\n
example 3:"
);
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
]));
}
/**
* calculates the next y-value based on the current value of x, y and the
* stepSize the console.
*
* @param xCurrent Current x-value.
* @param stepSize Step-size on the x-axis.
* @param yCurrent Current y-value.
* @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
) {
if
(
stepSize
<=
0
) {
throw
new
IllegalArgumentException
(
"stepSize should be greater than zero"
);
}
return
yCurrent
+
stepSize
*
differentialEquation
.
apply
(
xCurrent
,
yCurrent
);
}
/**
* Loops through all the steps until xEnd is reached, adds a point for each
* step and then returns all the points
*
* @param xStart First x-value.
* @param xEnd Last x-value.
* @param stepSize Step-size on the x-axis.
* @param yStart First y-value.
* @param differentialEquation The differential equation to be solved.
* @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
) {
if
(
xStart
>=
xEnd
) {
throw
new
IllegalArgumentException
(
"xEnd should be greater than xStart"
);
}
if
(
stepSize
<=
0
) {
throw
new
IllegalArgumentException
(
"stepSize should be greater than zero"
);
}
ArrayList
<
double
[]>
points
=
new
ArrayList
<
double
[]>();
double
[]
firstPoint
= {
xStart
,
yStart
};
points
.
add
(
firstPoint
);
double
yCurrent
=
yStart
;
double
xCurrent
=
xStart
;
while
(
xCurrent
<
xEnd
) {
// Euler's method for next step
yCurrent
=
eulerStep
(
xCurrent
,
stepSize
,
yCurrent
,
differentialEquation
);
xCurrent
+=
stepSize
;
double
[]
point
= {
xCurrent
,
yCurrent
};
points
.
add
(
point
);
}
return
points
;
}
}
You can’t perform that action at this time.