Skip to content

Add Java implementation of Forward Euler #518

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 27, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions contents/forward_euler_method/code/java/Euler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
public class Euler {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd go with ForwardEuler as the class name because there is also the Backward Euler method. Don't forget to also rename the file and adjust the import in the .md file.

private static double[] solveEuler(double timestep, int n) {
double[] eulerResult = new double[n];

//Setting the initial condition
eulerResult[1] = 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arrays in Java are 0-indexed, so this should be eulerResult[0]. The same goes for the next line, where i should be initialized with 1.

for(int i = 2; i < eulerResult.length; i++) {
eulerResult[i] = eulerResult[i-1] - (3 * eulerResult[i-1] * timestep);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two things in thise line:

  1. The parentheses around (3 * eulerResult[i-1] * timestep) aren't needed, although I know why you added them. They make the expression a bit more readable and I think I agree, so I guess we'll keep it.

  2. You didn't space out [i-1] and again, I understand why. But in this case, I think I like [i - 1] more. What do you think?

Suggested change
eulerResult[i] = eulerResult[i-1] - (3 * eulerResult[i-1] * timestep);
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 = 1; i < eulerResult.length; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above. Arrays start at index 0.

double time = (i - 1) * timestep;
double solution = Math.exp(-3*time);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please space the contents of the parentheses of Math.exp() out.

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);
}
}
2 changes: 2 additions & 0 deletions contents/forward_euler_method/forward_euler_method.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ Full code for the visualization follows:
[import, lang:"go"](code/golang/euler.go)
{% sample lang="asm-x64" %}
[import, lang:"asm-x64"](code/asm-x64/euler.s)
{% sample lang="java" %}
[import, lang:"java"](code/java/euler.java)
{% endmethod %}

<script>
Expand Down