C/C++ Solve ODEs Backwards in Time with C++ Euler Method

Click For Summary
To run the Euler method backward in time for the ordinary differential equation (ODE) y' = x - y, the primary adjustments needed in the code include changing the step size "dist" to a negative value. Additionally, the time loop condition must be modified to reflect the backward iteration, specifically changing the loop test from "j*dist <= MAX" to accommodate the new direction of time. It is also advisable to redefine the constant MAX to MIN to better represent the backward time range from t=1 to t=0. These changes will allow the code to correctly compute the ODE in reverse.
amr07
Messages
5
Reaction score
0
Hi Everybody
I am beginner in c++ and I need your help please. I implemented euler method for solving simple ODEs (y' = x -y, y(0)=1)and it is forward in time(from t=0 to t=1) and it worked well, my question is : I want to run this code backward in time(t=1 to t=0) what i have to change in my code?
code:

Code:
/* Euler for a set of first order differential equations */

#include <stdio.h>
#include <math.h>
#define dist 0.2               /* stepsize in t*/
#define MAX 1.0                /* max for t */
int N=1;
void euler(double x, double y[], double step); /* Euler function */

double f(double x, double y[], int i);          /* function for derivatives */

main()
{
double t, y[N];
int j;

y[0]=1.0;                                       /* initial condition */

 
for (j=0; j*dist<=MAX ;j++)                     /* time loop */
{
   t=j*dist;
   printf("j =  %d,t = %f y[0] = %f\n", j,t, y[0]);
   euler(t, y, dist);

}

}

void euler(double x, double y[], double step)
{
double  s[N];      /* for euler */
int i;
{
for (i=0;i<N;i++)
 {     s[i]=step*f(x, y, i);
}
}

{
for (i=0;i<N;i++) 
     y[i]+=s[i];
}
}
double  f(double x, double y[], int i)
{
    return(x-y[0]);                 /* derivative of first equation */
}
many thanks in advance!
 
Last edited:
Technology news on Phys.org
The main change is just to make "dist" a negative value.

You will also need to change the test in the time loop

for (j=0; j*dist<=MAX ;j++)

(and you might want to change the constant MAX to MIN).
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
6K
Replies
22
Views
5K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
1
Views
2K
  • · Replies 0 ·
Replies
0
Views
627
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 5 ·
Replies
5
Views
2K