-
-
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
Changes from all commits
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,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); | ||
} | ||
|
||
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); | ||
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. There's a method double solution = Math.exp(-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 commentThe reason will be displayed to describe this comment to others. Learn more. You're missing a 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; | ||
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. 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 commentThe 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"); | ||
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 use
instead of this:
|
||
} else { | ||
System.out.print("Value(s) not in threshold"); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 commentThe 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 %} | ||
|
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 ofn
(double[] result = new double[n]
), add the initial value of1
(result[0] = 1
) and then fill the array like this: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 anappend
method for an array, I'd use anArrayList
instead.