1. This is more a question of algorithm than C++, since without knowledge of the Euler algorithm it's not easy to give a correct answer. You should have added 'algorithm' as a keyword.
2. Eulers method requires a starting value. Do you have the starting value for
t=1.0
? Or do you want to start at
t=0.0
and run backward to
t=-1.0
?
3. Your function
f()
should always return a value. It currently does so only for
i==0
. Your compiler should have issued a warning about that - do not ignore it! Even though at the moment nothing bad happens, it will if you ever change your value for N!
Regarding your question, you only need to use a negative dist value and modify the for loop so that
t
gets decremented. Note that unless you really want your steps numbered in your printout, you do not need the variable
j
. For a loop from
t=1.0
backwards to
0.0
you can define the
for
loop just like this:
#define dist -0.2
for (t = 1.0; t >= 0.0; t += dist)
{
printf(...);
euler(...);
}
[edit: fixed the loop to run from 1.0 to 0.0]
[edit 2: fixed dist value to make sure
euler()
gets the correct, negative step value]