-
Notifications
You must be signed in to change notification settings - Fork 63
Added backward euler C++ example and documentation #236
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
Conversation
|
Hi @valeriabarra , @jbrzensk , |
jbrzensk
left a comment
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.
Thanks @kle96 for the code. It is super nice, and with documentation!
I added some minor notes about the code, minor things. It looks good otherwise.
| @@ -0,0 +1,43 @@ | |||
| ### Backward Euler Method | |||
|
|
|||
| This example solves the first-order ordinary differential equation using the backward euler method. | |||
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.
nit: Backward Euler
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.
Euler needs to be capitalized. Line 3.
|
|
||
| This example is implemented in: | ||
| - [MATLAB/Octave](https://github.com/csrc-sdsu/mole/blob/main/examples/matlab_octave/backward_euler.m) | ||
| - [C++](https://github.com/csrc-sdsu/mole/blob/main/examples/cpp/backward_euler.cpp) *(if available)* |
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.
remove (if available)
examples/cpp/backward_euler.cpp
Outdated
| @@ -0,0 +1,70 @@ | |||
| /** | |||
| * @file backward_euler.cpp | |||
| * @brief Solves a first-order ODE with backward euler method with an initial value condition. | |||
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.
nit: Backward Euler
examples/cpp/backward_euler.cpp
Outdated
| double tol = 1e-6; | ||
| for (int k = 0; k < 100; k++) { // fixed-point iteration for rootfinding | ||
| y(i+1) = y_old + h*f(t(i+1), y_old); // Backward euler method | ||
| if (std::abs(y(i+1) - y_old) / y(i+1) < tol) { // Stopping criteria for approximate relative error |
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.
Maybe remove the division? I am leary when there is a divide without a check to see if it is zero? I think it should work just as well without the division.
examples/cpp/backward_euler.cpp
Outdated
| // Backward Euler method | ||
| for (int i = 0; i < t.size()-1; i++) { | ||
| double y_old = y(i); | ||
| double tol = 1e-6; |
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.
Set outside the loop if not changing, and declare as constexpr
|
Thank you @jbrzensk! I made updates to the example and documentation with your feedback. Please let me know if there are any more updates I should include. |
jbrzensk
left a comment
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.
Euler is a name, has to be capitalized. And the vec needs the arma:: , so others know it is Armadillos vec, not the standard c++ vec
examples/cpp/backward_euler.cpp
Outdated
| int main() { | ||
| // Problem parameters | ||
| constexpr double h = 0.01; // Step-size h | ||
| vec t = arma::regspace(0, h, 5); // Calculates up to y(5) at step-size h |
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.
arma::vec
| @@ -0,0 +1,43 @@ | |||
| ### Backward Euler Method | |||
|
|
|||
| This example solves the first-order ordinary differential equation using the backward euler method. | |||
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.
Euler needs to be capitalized. Line 3.
|
|
||
| #### Mathematical Background | ||
|
|
||
| The backward euler method is an implicit method for solving first-order differential equations where we are evaluating the function at the next point in time $y_{i+1}$. |
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.
Euler
examples/cpp/backward_euler.cpp
Outdated
| @@ -0,0 +1,66 @@ | |||
| /** | |||
| * @file backward_euler.cpp | |||
| * @brief Solves a first-order ODE with backward euler with an initial value condition. | |||
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.
Euler
|
Thank you for the additional feedback @jbrzensk. I capitalized Euler and added arma:: when declaring the vector in line 18. Please let me know if there are any additional updates I should make. |
What type of PR is this? (check all applicable)
Description
Added a C++ example for backward euler and generated documentation for this example under Time-Integrators category.
Related Issues & Documents
QA Instructions, Screenshots, Recordings
Please replace this line with instructions on how to test your changes, a note
on the devices and browsers this has been tested on, as well as any relevant
images for UI changes.
Added/updated tests?
_We encourage you to test all code included with MOLE, including examples.
have not been included
Read Contributing Guide and Code of Conduct
[optional] Are there any post deployment tasks we need to perform?
[optional] What gif best describes this PR or how it makes you feel?