forked from algorithm-archivists/algorithm-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForwardEuler.java
38 lines (31 loc) · 1.18 KB
/
ForwardEuler.java
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
public class ForwardEuler {
private static double[] solveEuler(double timestep, int n) {
double[] eulerResult = new double[n];
//Setting the initial condition
eulerResult[0] = 1;
for(int i = 1; i < eulerResult.length; i++) {
eulerResult[i] = eulerResult[i - 1] - (3 * eulerResult[i - 1] * timestep);
}
return eulerResult;
}
private static boolean checkResult(double[] eulerResult, double threshold, double timestep) {
boolean isApprox = true;
for(int i = 0; i < eulerResult.length; i++) {
double time = i * timestep;
double solution = Math.exp(-3 * time);
if(Math.abs(eulerResult[i] - solution) > threshold) {
System.out.println(eulerResult[i] + " " + solution);
isApprox = false;
}
}
return isApprox;
}
public static void main(String[] args) {
double timestep = 0.1;
int n = 100;
double threshold = 0.1;
double[] eulerResult = solveEuler(timestep, n);
boolean isApprox = checkResult(eulerResult, threshold, timestep);
System.out.println(isApprox);
}
}