-
-
Notifications
You must be signed in to change notification settings - Fork 359
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
Adding Forward Euler in Java #387
Conversation
Hey @CDsigma I think you know, but we are missing some Java expertise, so I don't know who will review this. I'll do so if no one steps up. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apart from this the code looks good
public static void main(String[] args) { | ||
double timeStep = 0.01; | ||
int n = 100; | ||
double threshold = 0.01; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright. I'll go ahead and review this one. I'm not the most proficient person in Java out there, but it'll do.
First of all, your indentation is all over the place. We use 4 spaces in the AAA, but you used 2. Some lines with closing braces (12, 36) are not indented at all, even though they should be. Please fix that.
Secondly, your class (ForwardEuler
) does not have the same name as the file it's in (Euler.java
). I suggest you rename the file to ForwardEuler.java
.
boolean isApprox = checkResult(result, threshold, timeStep); | ||
|
||
if(isApprox) { | ||
System.out.print("All values within threshold"); |
There was a problem hiding this comment.
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>
double solution = Math.pow(Math.E, -3 * i * timeStep); | ||
|
||
if(Math.abs(result[i] - solution) > threshold) { | ||
isApprox = false; |
There was a problem hiding this comment.
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
double[] result = {1}; | ||
|
||
for(int i = 0; i < n; i++) { | ||
result = append(result, result[i] - 3 * result[i] * timeStep); |
There was a problem hiding this comment.
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.
boolean isApprox = true; | ||
|
||
for(int i = 0; i < result.length; i++) { | ||
double solution = Math.pow(Math.E, -3 * i * timeStep); |
There was a problem hiding this comment.
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);
@@ -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) |
There was a problem hiding this comment.
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.
This PR has been stale for over 10 days now and you haven't shown up in Discord either. I'm assuming you're on a vacation or you're preoccupied by something else. Either way, we have another PR for Forward Euler in Java in #518, so I'm going to go ahead and work with that one. I'm sure you'll understand. |
Added Forward Euler in Java