-
-
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 16 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,32 @@ | ||
function forward_euler(time_step, n) { | ||
var arr = []; | ||
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. In more modern JS people tend to avoid 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 think I got behind the usage of var, let and const. After reading about their scopes in the Mozilla Web Dev documents. If I got it right with
Both are only valid for the local control or programming structure, including further inner functions. Thus not globally available to other functions or blocks. 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. @depate Yes, that's pretty much right. Your terminology might not be 100% correct, but I see that you understand the concept. |
||
arr[0] = 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. I would just inline this with the array initialization: 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, if I got this right, this initialises, the I am more used to fixed array dimensions which are allocated or set at the time of the initialisation. So I get confused all the time. But Python also is more 'flexibel' with memory allocation so I could've known better in the first place. |
||
for (var i = 1; i <= n; 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 thing about |
||
arr[i] = arr[i - 1] - 3 * arr[i - 1] * time_step; | ||
} | ||
return arr; | ||
} | ||
|
||
function check_euler(arr, time_step, threshold) { | ||
var is_approx = true; | ||
for (var i = 1; i <= arr.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. The range is wrong, it should go from arr.forEach((value, i) => {
// const solution = ...
}); 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. Okay, here I get the general concept. One can iterate through the array directly because the array structure supports it as a method internally, great. I am not sure about the As the callback function returns the actual value of the array element, I could "simplify" the following threshold check by passing |
||
var solution = Math.exp(-3 * time_step * i); | ||
|
||
if (Math.abs(arr[i] - solution) > threshold) { | ||
alert(arr[i], solution); | ||
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 think a |
||
is_approx = false; | ||
} | ||
} | ||
return is_approx; | ||
} | ||
|
||
function main() { | ||
time_step = 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. These should all be declared with const timeStep = 0.01;
const threshold = ...; |
||
threshold = 0.01; | ||
n = 100; | ||
var euler_result = forward_euler(time_step, n); | ||
var check_result = check_euler(euler_result, time_step, threshold); | ||
alert(check_result); | ||
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. Similarly to the previous comment, |
||
} | ||
|
||
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.
As a general note, JS uses
camelCase
for everything, so the underscores kind of stand out. I would recommend sticking with the de-facto standard formattingThere 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.
Thanks for the information.
I confess, I did write this first attempt on JavaScript Syntax quite quickly and did not read through a style guide. I could have noticed that there. I'll catch up on the style guides, promise!