-
-
Notifications
You must be signed in to change notification settings - Fork 359
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,38 @@ | ||||||
public class Euler { | ||||||
private static double[] solveEuler(double timestep, int n) { | ||||||
double[] eulerResult = new double[n]; | ||||||
|
||||||
//Setting the initial condition | ||||||
eulerResult[1] = 1; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Arrays in Java are 0-indexed, so this should be |
||||||
for(int i = 2; i < eulerResult.length; i++) { | ||||||
eulerResult[i] = eulerResult[i-1] - (3 * eulerResult[i-1] * timestep); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two things in thise line:
Suggested change
|
||||||
} | ||||||
return eulerResult; | ||||||
} | ||||||
|
||||||
private static boolean checkResult(double[] eulerResult, double threshold, double timestep) { | ||||||
boolean isApprox = true; | ||||||
|
||||||
for(int i = 1; i < eulerResult.length; i++) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please space the contents of the parentheses of |
||||||
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); | ||||||
} | ||||||
} |
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'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.