-5

I'm running a program that preforms a Euler Approximation of an Ordinary Differential Equation. The smaller the step size that is chosen, the more accurate the approximation is. I can get it to work for a set step size using this code:

#include <iostream>
using std::cout;

double f (double x, double t)
 {
     return t*x*x-t;
 }

 int main()
 {
     double x=0.0,t=0.0,t1=2.0;
     int n=20;
     double h = (t1-t) / double(n);

 // ----- EULERS METHOD

     for (int i=0; i<n; i++)
     {
         x += h*f(x,t);
         t += h;
     }

     cout << h << " " << x << "\n";

 }

So this code runs a Eulers approximation for n=20 which corresponds to a step size of 0.1 and outputs the step size along with the approximation for x(2). I want top know how to loop this code (for different values of n) so that it outputs this followed by increasingly smaller step sizes with corresponding approximations. i.e an output something like this:

0.1   -0.972125
0.01  -0.964762
0.001 -0.9641

etc.

So I tried a for-loop inside a for-loop but its giving me a weird output of extreme values.

#include <iostream>
using std::cout;

double f (double x, double t)
 {
     return t*x*x-t;
 }

int main()
 {
     double x=0.0,t=0.0,t1=2.0;

     for (int n=20;n<40;n++)
     {
         double h = (t1-t)/n;
         for (int i=0;i<n;i++)
         {
             x += h*f(x,t);
             t += h;
         }
         cout << h << " " << x << "\n";

     }

 }
J.M
  • 15
  • 3
  • Why does `n` need to be hard-coded? Imagine what you could do with `for (int n = 20; n < 30; ++n){ ...` – alter_igel Sep 29 '18 at 21:27
  • If you have a box for stuff, the box itself doesn't care what's inside it. It could be another box. Simply, a for-loop inside a for-loop is, well, a for-loop inside a for-loop. What exactly is unclear to you about that? Replace your single `int n` with a for-loop that iterates `n` over the range of values you wish, and then inside it you have your existing code. What exactly is your question, regarding this? – Sam Varshavchik Sep 29 '18 at 21:28
  • It doesn't need to be hardcoded! I'm trying to run it so it isn't hardcoded. But when I went to replace "int n" with something like "for (int n = 20; n<40; n++)" the code spiralled out into extreme values. – J.M Sep 29 '18 at 21:32
  • So I had something like this but it's not working for me and I'm getting a useless output!! `for (int n=20;n<40;n++) { double h = (t1-t)/n; for (int i=0;i – J.M Sep 29 '18 at 21:35
  • I would suspect the spiraling out of control to be because of `x`, `t`, and `t1` being changed by previous loop runs. My advice: put those variables and the Euler's method logic into a function like `double eulersMethod(double t, double t1, int n)` – alter_igel Sep 29 '18 at 21:40

1 Answers1

0

If I understand correctly, you want to execute that first piece of code inside your main function for different values of n. Then your problem is with the variables x, t and t1, which are set once before the loop and never reset. You want them inside your outer loop:

#include <iostream>

using std::cout;

double f( double x, double t )
{
    return t * x * x - t;
}

int main()
{
    for ( int n = 20; n < 40; n++ )
    {
        double x = 0.0, t = 0.0, t1 = 2.0;
        double h = ( t1 - t ) / n;
        for ( int i = 0; i < n; i++ )
        {
            x += h * f( x, t );
            t += h;
        }
        cout << h << " " << x << "\n";
    }
}

Using a function for this, makes it clearer:

#include <iostream>

using std::cout;

double f( double x, double t )
{
    return t * x * x - t;
}

void eulers( const int n )
{
    double x = 0.0, t = 0.0, t1 = 2.0;
    double h = ( t1 - t ) / n;
    for ( int i = 0; i < n; i++ )
    {       
        x += h * f( x, t ); 
        t += h; 
    }       
    cout << h << " " << x << "\n";
}

int main()
{
    for ( int n = 20; n < 40; n++ )
    {
        eulers( n );
    }
}

Hope this helps.

Roger Rapid
  • 956
  • 1
  • 8
  • 28
  • That's exactly what I was trying to do! Thank you so much for the reply. Apologies if my question wasn't clear/concise, I haven't posted here much. – J.M Sep 30 '18 at 13:13
  • Glad to be of help! And welcome to Stack Overflow. If this is the answer you were looking for, could you please mark it as accepted? Thanks. – Roger Rapid Sep 30 '18 at 15:01