-
-
Notifications
You must be signed in to change notification settings - Fork 359
Forward Euler in JavaScript #418
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 17 commits
0619e57
ecf82ab
3e86449
b9e0828
5d1602a
1ea8e18
b9f0caa
76f5ca2
99b3b36
46f7300
2917f32
2bb14be
824060b
7d8917e
64a2fc8
252f9c3
fd1842c
14aa6fa
ce1dc90
71d7fa2
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,31 @@ | ||
function forwardEuler(time_step, n) { | ||
const arr = [1]; | ||
for (let i = 1; i <= n; i++) { | ||
arr[i] = arr[i - 1] - 3 * arr[i - 1] * time_step; | ||
} | ||
return arr; | ||
} | ||
|
||
function checkEuler(arr, time_step, threshold) { | ||
const is_approx = true; | ||
arr.forEach(function callback(value, 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. I'll go from the point where this thread ended.
The arrow ( This is how an arrow function with a full function body would look: const func1 = (param1, param2) => {
// Function body goes here
}; If a function only has one line in it, which is a return statement, you can omit the const func1 = (param1, param2) => {
return param1 + param2;
};
const func2 = (param1, param2) => param1 + param2; I know what you're thinking. This looks kind of weird and unreadable. But trust me, it's just a syntax that you're not used to and once you're used to seeing it, it will look completely normal and you'll be able to quickly understand its meaning. If an arrow function has only one parameter, you can also omit the const double1 = (num) => num * 2;
const double2 = num => num * 2; To give you a practical example of why you would use these forms of arrow functions, here is a snippet of code that takes an array with numbers and creates a second array where all the numbers are doubled: const doubleArr = arr.map(num => num * 2); And finally, this is how you could use an arrow function in your code: arr.forEach((_value, i) => {
// Function body goes here
}); Also note how I called the first parameter 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. ok, I think I got it at least for the first part. This short syntax for functions is easy, because you declare a variables value to the result of a function one evokes with a set of parameters like:
But using the same syntax just to define a function on to parameters without a result being assigned to a variable outside of it. Intuitively I would write it somehow that inside the checkEuler function the Something like: const isApprox = arr.forEach(( _value, i) => {
[...] // here is the calculation of the solution variable A which gets compared later on with threshold B
if ( A > B) {
[...]
return (A > B) // returning the logical value of the check here, but I can't tell if this valid
}
}); If this would somehow be possible, then we are saving at least one line of code there. Because Edit: I see a problem with this procedure: If I felt smart for a little moment in time. |
||
const solution = Math.exp(-3 * time_step * i); | ||
|
||
if (Math.abs(arr[i] - solution) > threshold) { | ||
console.log(arr[i], solution); | ||
is_approx = false; | ||
} | ||
}); | ||
return is_approx; | ||
} | ||
|
||
function main() { | ||
const time_step = 0.01; | ||
const threshold = 0.01; | ||
const n = 100; | ||
var euler_result = forwardEuler(time_step, n); | ||
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 changed
function foo() {
// <- This is where JavaScript moves the var declaration
if (true) {
var bar; // JavaScript moves this declaration to the top of the function and outside of the block
let baz;
}
} So in case 1 |
||
var check_result = checkEuler(euler_result, time_step, threshold); | ||
console.log(check_result); | ||
} | ||
|
||
main(); |
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.
What was already said about snake_case vs. camelCase also applies to variables. This should be
timeStep
. The same goes foris_approx
,euler_result
andcheck_result
further down.