Skip to content

Conversation

@kle96
Copy link
Contributor

@kle96 kle96 commented Nov 20, 2025

What type of PR is this? (check all applicable)

  • Refactor
  • Feature
  • Bug Fix
  • Optimization
  • Example
  • Documentation

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.

  • Yes
  • No, and this is why: please replace this line with details on why tests
    have not been included
  • I need help with writing tests

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?

@kle96
Copy link
Contributor Author

kle96 commented Dec 1, 2025

Hi @valeriabarra , @jbrzensk ,
I would like to follow up and check if there any updates on my PR request.

Copy link
Collaborator

@jbrzensk jbrzensk left a 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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Backward Euler

Copy link
Collaborator

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)*
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove (if available)

@@ -0,0 +1,70 @@
/**
* @file backward_euler.cpp
* @brief Solves a first-order ODE with backward euler method with an initial value condition.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Backward Euler

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
Copy link
Collaborator

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.

// Backward Euler method
for (int i = 0; i < t.size()-1; i++) {
double y_old = y(i);
double tol = 1e-6;
Copy link
Collaborator

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

@kle96
Copy link
Contributor Author

kle96 commented Dec 2, 2025

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.

Copy link
Collaborator

@jbrzensk jbrzensk left a 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

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
Copy link
Collaborator

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.
Copy link
Collaborator

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}$.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Euler

@@ -0,0 +1,66 @@
/**
* @file backward_euler.cpp
* @brief Solves a first-order ODE with backward euler with an initial value condition.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Euler

@kle96
Copy link
Contributor Author

kle96 commented Dec 3, 2025

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.

@jbrzensk jbrzensk merged commit 4ea1cfa into csrc-sdsu:main Dec 5, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement Backward Euler C++ Example and Update Documentation

2 participants