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

Conversation

atocil
Copy link
Contributor

@atocil atocil commented Oct 17, 2018

Edits are fine, but please provide any feedback you can so I can make my next PRs better

@atocil
Copy link
Contributor Author

atocil commented Oct 17, 2018

I did also note that there was a PR open for this, but it's been 28 days so figured I'd do my best to help

@Gathros Gathros added Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.) Hacktoberfest The label for all Hacktoberfest related things! labels Oct 17, 2018
Copy link
Contributor

@Butt4cak3 Butt4cak3 left a comment

Choose a reason for hiding this comment

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

Hello and thank you for the contribution!

@CDsigma is usually an active and competent member of the community, so I was willing to wait for him to come back and finish the PR with me, but my review has been pending for almost 2 weeks now and he hasn't shown up anywhere else either, so let's go with your code.

You messed up some of the indexing, probably because you worked off of the Julia code. Arrays are 1-indexed in Julia and 0-indexed in Java. The same happened to me a few weeks ago, so don't worry.

Other than that I have a few minor style complaints.

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.

@@ -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.

//Setting the initial condition
eulerResult[1] = 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);


for(int i = 1; i < eulerResult.length; i++) {
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.

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.

@Butt4cak3 Butt4cak3 self-assigned this Oct 17, 2018
@atocil
Copy link
Contributor Author

atocil commented Oct 18, 2018

You're spot on with why my indexing mistakes were there! I also incorporated all of the style changes, let me know if I missed anything or if you notice something else!

boolean isApprox = true;

for(int i = 0; i < eulerResult.length; i++) {
double time = (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.

I found another error that leads back to the indexing difference between Julia and Java. time is just i * timestep.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

D'oh

@@ -14,7 +14,7 @@ private static boolean checkResult(double[] eulerResult, double threshold, doubl
boolean isApprox = true;

for(int i = 0; i < eulerResult.length; i++) {
double time = (i - 1) * timestep;
double time = (i) * timestep;
Copy link
Contributor

@Butt4cak3 Butt4cak3 Oct 22, 2018

Choose a reason for hiding this comment

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

Uhm... So, why exactly is i still in parentheses? =P

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ha! Because I'm lazy and didn't even look that closely

@Butt4cak3 Butt4cak3 merged commit 1bb82d9 into algorithm-archivists:master Oct 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Hacktoberfest The label for all Hacktoberfest related things! Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants