Euler method in c++

Post any maths and/or physics related questions here.

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

Euler method in c++

Postby mora78 » Mon May 02, 2011 8:12 am

Hi Everybody
I am beginner in c++ and I need your help plz. 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(i.e. from t=1 to t=0) what i have to change in my code?
Code:
Code: Select all
/* 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)
{
if (i==0) return(x-y[0]);                 /* derivative of first equation */
}


many thanks in advance!
mora78
 
Posts: 1
Joined: Mon May 02, 2011 7:49 am

Re: Euler method in c++

Postby Riley103 » Wed Apr 25, 2012 9:31 pm

Thanks you for sharing
__________________
Watch The Five Year Engagement Online Free
Riley103
 
Posts: 1
Joined: Wed Apr 25, 2012 8:53 pm


Return to Maths & Physics

Who is online

Users browsing this forum: No registered users and 2 guests