Skip to content

Adding Forward Euler in Java #387

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
52 changes: 52 additions & 0 deletions contents/forward_euler_method/code/java/Euler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
public class ForwardEuler {

public static double[] append(double[] oldArray, double newItem) {
double[] newArray = new double[oldArray.length + 1];

for(int i = 0; i < oldArray.length; i++) {
newArray[i] = oldArray[i];
}
newArray[newArray.length - 1] = newItem;

return newArray;
}

public static double[] solveEuler(double timeStep, int n) {
double[] result = {1};

for(int i = 0; i < n; i++) {
result = append(result, result[i] - 3 * result[i] * timeStep);
Copy link
Contributor

Choose a reason for hiding this comment

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

You're thinking way too complicated here. You could just make result an array with a length of n (double[] result = new double[n]), add the initial value of 1 (result[0] = 1) and then fill the array like this:

result[i] = result[i - 1] - 3 * result[i - 1] * timeStep;

The loop would of course have to start from 1 to skip the first element.

This way, you could drop the append method above. In addition to that, if I'd need an append method for an array, I'd use an ArrayList instead.

}

return result;
}

public static boolean checkResult(double[] result, double threshold, double timeStep) {
boolean isApprox = true;

for(int i = 0; i < result.length; i++) {
double solution = Math.pow(Math.E, -3 * i * timeStep);
Copy link
Contributor

@Butt4cak3 Butt4cak3 Oct 6, 2018

Choose a reason for hiding this comment

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

There's a method Math.exp(double a) which does exactly this.

double solution = Math.exp(-3 * i * timeStep);


if(Math.abs(result[i] - solution) > threshold) {
isApprox = false;
Copy link
Contributor

Choose a reason for hiding this comment

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

You're missing a println before inside this block. If you look at the reference implementation (Julia), you'll see this:

if (abs(euler_result[i] - solution) > threshold)
    println(euler_result[i], solution)
    is_approx = false
end

}
}

return isApprox;
}

public static void main(String[] args) {
double timeStep = 0.01;
int n = 100;
double threshold = 0.01;
Copy link
Contributor

Choose a reason for hiding this comment

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

You can make these variables as memebers of the class so that all methods can access them without needing to pass them as arguments

Copy link
Contributor

@Butt4cak3 Butt4cak3 Oct 6, 2018

Choose a reason for hiding this comment

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

I disagree. These should be passed as arguments.


double[] result = solveEuler(timeStep, n);
boolean isApprox = checkResult(result, threshold, timeStep);

if(isApprox) {
System.out.print("All values within threshold");
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use System.out.println here or otherwise add a newline to the output. I assume you're using Windows, so everything works as expected, but when I run this on Linux, my shell does this:

user@hostname:aaa> java ForwardEuler
All values within thresholduser@hostname:aaa>

instead of this:

user@hostname:aaa> java ForwardEuler
All values within threshold
user@hostname:aaa>

} else {
System.out.print("Value(s) not in threshold");
}
}
}
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 @@ -121,6 +121,8 @@ Full code for the visualization follows:
[import, lang:"matlab"](code/matlab/euler.m)
{% sample lang="swift" %}
[import, lang:"swift"](code/swift/euler.swift)
{% sample lang="java" %}
[import, lang:"java"](code/java/Euler.java)
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't forget to update the file name in case you rename the source file.

{% sample lang="f90" %}
[import, lang:"fortran"](code/fortran/euler.f90)
{% endmethod %}
Expand Down