Thread: runge kutta 4th order-error during iterations

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    29

    runge kutta 4th order-error during iterations

    Code:
    # include <stdlib.h>
    # include <stdio.h>
    # include <math.h>
    # include <time.h>
    
    
    
    int main ( );
    void test01 ( );
    double test01_f ( double t, double u );
    double rk4 ( double t0, double u0, double dt, double f ( double t, double u ) );
    void timestamp ( void );
    
    /******************************************************************************/
    
    int main ( ) 
    
    /******************************************************************************/
    
    {
      timestamp ( );
      printf ( "\n" );
      printf ( "RK4_PRB:\n" );
      printf ( "  C version\n" );
      printf ( "  Test the RK4 library.\n" );
    
      test01 ( );
      
    /*
      Terminate.
    */
      printf ( "\n" );
      printf ( "RK4_PRB:\n" );
      printf ( "  Normal end of execution.\n" );
      printf ( "\n" );
     timestamp ( );
     system ("pause");
      return 0;
    }
    /******************************************************************************/
    
    void test01 ( ) 
    
    /******************************************************************************/
    
    {
      double dt = 0.1;
      double pi = 3.14159265;
      double t0 = 0.0;
      double t1;
      double tmax = 12.0 * pi;
      double u0 = 0.5;
      double u1;
      //double rk4 ( double t0, double u0, double dt, double f ( double t, double u ) );
    
    
      printf ( "\n" );
      printf ( "TEST01\n" );
      printf ( "  RK4 takes a Runge-Kutta step for a scalar ODE.\n" );
    
      printf ( "\n" );
      printf ( "          T          U(T)\n" );
      printf ( "\n" );
    
      while ( 1 )
      {
    /*
      Print (T0,U0).
    */
        printf ( "  %g  %g\n", t0, u0 );
    /*
      Stop if we've exceeded TMAX.
    */
        if ( tmax <= t0 )
        {
          break;
        }
    /*
      Otherwise, advance to time T1, and have RK4 estimate 
      the solution U1 there.
    */
        t1 = t0 + dt;
        u1 = rk4 ( t0, u0, dt, test01_f );
    /*
      Shift the data to prepare for another step.
    */
        t0 = t1;
        u0 = u1;
      }
      return;
    }
    /******************************************************************************/
    
    double test01_f ( double t, double u )
    
    /******************************************************************************/
    
    {
      double dudt;
      
      dudt = u * cos ( t );
      
      return dudt;
    }
    /******************************************************************************/
    double rk4 ( double t0, double u0, double dt, double f ( double t, double u ) )
    {
      double f0;
      double f1;
      double f2;
      double f3;
      double t1;
      double t2;
      double t3;
      double u;
      double u1;
      double u2;
      double u3;
    /*
      Get four sample values of the derivative.
    */
      f0 = f ( t0, u0 );
    
      t1 = t0 + dt / 2.0;
      u1 = u0 + dt * f0 / 2.0;
      f1 = f ( t1, u1 );
    
      t2 = t0 + dt / 2.0;
      u2 = u0 + dt * f1 / 2.0;
      f2 = f ( t2, u2 );
    
      t3 = t0 + dt;
      u3 = u0 + dt * f2;
      f3 = f ( t3, u3 );
    /*
      Combine them to estimate the solution.
    */
      u = u0 + dt * ( f0 + 2.0 * f1 + 2.0 * f2 + f3 ) / 6.0;
    
      return u;
    }
    
    
    void timestamp ( void )
    
    
    {
    # define TIME_SIZE 40
    
      static char time_buffer[TIME_SIZE];
      const struct tm *tm;
      size_t len;
      time_t now;
    
      now = time ( NULL );
      tm = localtime ( &now );
    
      len = strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm );
    
      fprintf ( stdout, "%s\n", time_buffer );
    
      return;
    # undef TIME_SIZE
    }
    the output..why does it iterates starting from 8.4 and not 0.0? why the introductions," RK4_PRB:
    C version
    Test the RK4 library.", not appear in the output?

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    The output I get is pasted below. Can you point out what's unexpected or surprising in the output and what you expected in its place?

    Code:
    $ ./a.out 
    19 December 2013 01:40:26 PM
    
    RK4_PRB:
      C version
      Test the RK4 library.
    
    TEST01
      RK4 takes a Runge-Kutta step for a scalar ODE.
    
              T          U(T)
    
      0  0.5
      0.1  0.552493
      0.2  0.609889
      0.3  0.671912
      0.4  0.738061
      0.5  0.807573
      0.6  0.879409
      0.7  0.952248
      0.8  1.0245
      0.9  1.09437
      1  1.15989
      1.1  1.21904
      1.2  1.26984
      1.3  1.3105
      1.4  1.33951
      1.5  1.35574
      1.6  1.35856
      1.7  1.34786
      1.8  1.32406
      1.9  1.28808
      2  1.24129
      2.1  1.18538
      2.2  1.12226
      2.3  1.05396
      2.4  0.982471
      2.5  0.909668
      2.6  0.837239
      2.7  0.766617
      2.8  0.698962
      2.9  0.635147
      3  0.575781
      3.1  0.521229
      3.2  0.471648
      3.3  0.427034
      3.4  0.387249
      3.5  0.352068
      3.6  0.321208
      3.7  0.294351
      3.8  0.271171
      3.9  0.251349
      4  0.234582
      4.1  0.220596
      4.2  0.209146
      4.3  0.200025
      4.4  0.193061
      4.5  0.18812
      4.6  0.185104
      4.7  0.183954
      4.8  0.184647
      4.9  0.187196
      5  0.191653
      5.1  0.198104
      5.2  0.206676
      5.3  0.217531
      5.4  0.230868
      5.5  0.246921
      5.6  0.265959
      5.7  0.288277
      5.8  0.314193
      5.9  0.344031
      6  0.378113
      6.1  0.416733
      6.2  0.460134
      6.3  0.508478
      6.4  0.561806
      6.5  0.620005
      6.6  0.682764
      6.7  0.749538
      6.8  0.819522
      6.9  0.891627
      7  0.964485
      7.1  1.03647
      7.2  1.10575
      7.3  1.17033
      7.4  1.22821
      7.5  1.27743
      7.6  1.31623
      7.7  1.34315
      7.8  1.35716
      7.9  1.3577
      8  1.34475
      8.1  1.31883
      8.2  1.28093
      8.3  1.23247
      8.4  1.17522
      8.5  1.11109
      8.6  1.04211
      8.7  0.970272
      8.8  0.897421
      8.9  0.825202
      9  0.755006
      9.1  0.687943
      9.2  0.624841
      9.3  0.566265
      9.4  0.512542
      9.5  0.463802
      9.6  0.420011
      9.7  0.381018
      9.8  0.346585
      9.9  0.316421
      10  0.290205
      10.1  0.267612
      10.2  0.248324
      10.3  0.232043
      10.4  0.218499
      10.5  0.207455
      10.6  0.198707
      10.7  0.192092
      10.8  0.18748
      10.9  0.184781
      11  0.183942
      11.1  0.184945
      11.2  0.18781
      11.3  0.192595
      11.4  0.199393
      11.5  0.208337
      11.6  0.219594
      11.7  0.23337
      11.8  0.249906
      11.9  0.269474
      12  0.292374
      12.1  0.318927
      12.2  0.349456
      12.3  0.384282
      12.4  0.423691
      12.5  0.467915
      12.6  0.517097
      12.7  0.571257
      12.8  0.630251
      12.9  0.69373
      13  0.761108
      13.1  0.831531
      13.2  0.903866
      13.3  0.976698
      13.4  1.04836
      13.5  1.11699
      13.6  1.18059
      13.7  1.23715
      13.8  1.28473
      13.9  1.32162
      14  1.34643
      14.1  1.3582
      14.2  1.35646
      14.3  1.34128
      14.4  1.31326
      14.5  1.27347
      14.6  1.22341
      14.7  1.16486
      14.8  1.09977
      14.9  1.03018
      15  0.958046
      15.1  0.885192
      15.2  0.813224
      15.3  0.743485
      15.4  0.677037
      15.5  0.614664
      15.6  0.556887
      15.7  0.503997
      15.8  0.456095
      15.9  0.413125
      16  0.374917
      16.1  0.341223
      16.2  0.311745
      16.3  0.286161
      16.4  0.264146
      16.5  0.245384
      16.6  0.229581
      16.7  0.216473
      16.8  0.205828
      16.9  0.19745
      17  0.191179
      17.1  0.186894
      17.2  0.184511
      17.3  0.183981
      17.4  0.185295
      17.5  0.188478
      17.6  0.193594
      17.7  0.200743
      17.8  0.210063
      17.9  0.221728
      18  0.23595
      18.1  0.252976
      18.2  0.273083
      18.3  0.296575
      18.4  0.323773
      18.5  0.355003
      18.6  0.390581
      18.7  0.430785
      18.8  0.475836
      18.9  0.525858
      19  0.580846
      19.1  0.640624
      19.2  0.704806
      19.3  0.772763
      19.4  0.843596
      19.5  0.91612
      19.6  0.988878
      19.7  1.06017
      19.8  1.12809
      19.9  1.19065
      20  1.24582
      20.1  1.29172
      20.2  1.32667
      20.3  1.34934
      20.4  1.35886
      20.5  1.35484
      20.6  1.33744
      20.7  1.30735
      20.8  1.26573
      20.9  1.21411
      21  1.15432
      21.1  1.08833
      21.2  1.01818
      21.3  0.945799
      21.4  0.872989
      21.5  0.801309
      21.6  0.732057
      21.7  0.666247
      21.8  0.604617
      21.9  0.547647
      22  0.495594
      22.1  0.448528
      22.2  0.406373
      22.3  0.368944
      22.4  0.33598
      22.5  0.30718
      22.6  0.282219
      22.7  0.260772
      22.8  0.242527
      22.9  0.227195
      23  0.214517
      23.1  0.204266
      23.2  0.196252
      23.3  0.190323
      23.4  0.186362
      23.5  0.184293
      23.6  0.184073
      23.7  0.185698
      23.8  0.189201
      23.9  0.194651
      24  0.202154
      24.1  0.211855
      24.2  0.223934
      24.3  0.23861
      24.4  0.256133
      24.5  0.276788
      24.6  0.30088
      24.7  0.328733
      24.8  0.360673
      24.9  0.39701
      25  0.438016
      25.1  0.483897
      25.2  0.534759
      25.3  0.59057
      25.4  0.651121
      25.5  0.71599
      25.6  0.784501
      25.7  0.855708
      25.8  0.928381
      25.9  1.00102
      26  1.07188
      26.1  1.13904
      26.2  1.20049
      26.3  1.25424
      26.4  1.29841
      26.5  1.33137
      26.6  1.35188
      26.7  1.35913
      26.8  1.35284
      26.9  1.33324
      27  1.30112
      27.1  1.2577
      27.2  1.20457
      27.3  1.1436
      27.4  1.07677
      27.5  1.00611
      27.6  0.933541
      27.7  0.860818
      27.8  0.789463
      27.9  0.720727
      28  0.655575
      28.1  0.594702
      28.2  0.538546
      28.3  0.487331
      28.4  0.4411
      28.5  0.399755
      28.6  0.363096
      28.7  0.330855
      28.8  0.302723
      28.9  0.278375
      29  0.257488
      29.1  0.239753
      29.2  0.224884
      29.3  0.212629
      29.4  0.202766
      29.5  0.195113
      29.6  0.189522
      29.7  0.185884
      29.8  0.184127
      29.9  0.184217
      30  0.186154
      30.1  0.189979
      30.2  0.195765
      30.3  0.203627
      30.4  0.213714
      30.5  0.226214
      30.6  0.24135
      30.7  0.259379
      30.8  0.28059
      30.9  0.305292
      31  0.333809
      31.1  0.366468
      31.2  0.403572
      31.3  0.445386
      31.4  0.4921
      31.5  0.5438
      31.6  0.600428
      31.7  0.661742
      31.8  0.727277
      31.9  0.796316
      32  0.867863
      32.1  0.940642
      32.2  1.01311
      32.3  1.08348
      32.4  1.14983
      32.5  1.21012
      32.6  1.26238
      32.7  1.30477
      32.8  1.33572
      32.9  1.35404
      33  1.35902
      33.1  1.35046
      33.2  1.32869
      33.3  1.29457
      33.4  1.2494
      33.5  1.19482
      33.6  1.13272
      33.7  1.06511
      33.8  0.993991
      33.9  0.921279
      34  0.848687
      34.1  0.777693
      34.2  0.709499
      34.3  0.645026
      34.4  0.584921
      34.5  0.529586
      34.6  0.47921
      34.7  0.433811
      34.8  0.39327
      34.9  0.357374
      35  0.325846
      35.1  0.298373
      35.2  0.27463
      35.3  0.254294
      35.4  0.23706
      35.5  0.222647
      35.6  0.210809
      35.7  0.201329
      35.8  0.194032
      35.9  0.188776
      36  0.185458
      36.1  0.184013
      36.2  0.184413
      36.3  0.186664
      36.4  0.190812
      36.5  0.196938
      36.6  0.205163
      36.7  0.215642
      36.8  0.228568
      36.9  0.244172
      37  0.262715
      37.1  0.28449
      37.2  0.309811
      37.3  0.339003
      37.4  0.372388
      37.5  0.410267
      37.6  0.452894
      37.7  0.500444
    
    RK4_PRB:
      Normal end of execution.
    
    19 December 2013 01:40:27 PM
    sh: pause: command not found

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    I'm not sure what you're expecting. The function can be integrated:

    du/dt = u cos(t)
    du/u = cos(t)dt

    ln(u) = sin(t) + C
    u = e^(sin(t) + C)

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    29
    Hodor.this is the output that i get

    Code:
      8.4  1.17522
      8.5  1.11109
      8.6  1.04211
      8.7  0.970272
      8.8  0.897421
      8.9  0.825202
      9  0.755006
      9.1  0.687943
      9.2  0.624841
      9.3  0.566265
      9.4  0.512542
      9.5  0.463802
      9.6  0.420011
      9.7  0.381018
      9.8  0.346585
      9.9  0.316421
      10  0.290205
      10.1  0.267612
      10.2  0.248324
      10.3  0.232043
      10.4  0.218499
      10.5  0.207455
      10.6  0.198707
      10.7  0.192092
      10.8  0.18748
      10.9  0.184781
      11  0.183942
      11.1  0.184945
      11.2  0.18781
      11.3  0.192595
      11.4  0.199393
      11.5  0.208337
      11.6  0.219594
      11.7  0.23337
      11.8  0.249906
      11.9  0.269474
      12  0.292374
      12.1  0.318927
      12.2  0.349456
      12.3  0.384282
      12.4  0.423691
      12.5  0.467915
      12.6  0.517097
      12.7  0.571257
      12.8  0.630251
      12.9  0.69373
      13  0.761108
      13.1  0.831531
      13.2  0.903866
      13.3  0.976698
      13.4  1.04836
      13.5  1.11699
      13.6  1.18059
      13.7  1.23715
      13.8  1.28473
      13.9  1.32162
      14  1.34643
      14.1  1.3582
      14.2  1.35646
      14.3  1.34128
      14.4  1.31326
      14.5  1.27347
      14.6  1.22341
      14.7  1.16486
      14.8  1.09977
      14.9  1.03018
      15  0.958046
      15.1  0.885192
      15.2  0.813224
      15.3  0.743485
      15.4  0.677037
      15.5  0.614664
      15.6  0.556887
      15.7  0.503997
      15.8  0.456095
      15.9  0.413125
      16  0.374917
      16.1  0.341223
      16.2  0.311745
      16.3  0.286161
      16.4  0.264146
      16.5  0.245384
      16.6  0.229581
      16.7  0.216473
      16.8  0.205828
      16.9  0.19745
      17  0.191179
      17.1  0.186894
      17.2  0.184511
      17.3  0.183981
      17.4  0.185295
      17.5  0.188478
      17.6  0.193594
      17.7  0.200743
      17.8  0.210063
      17.9  0.221728
      18  0.23595
      18.1  0.252976
      18.2  0.273083
      18.3  0.296575
      18.4  0.323773
      18.5  0.355003
      18.6  0.390581
      18.7  0.430785
      18.8  0.475836
      18.9  0.525858
      19  0.580846
      19.1  0.640624
      19.2  0.704806
      19.3  0.772763
      19.4  0.843596
      19.5  0.91612
      19.6  0.988878
      19.7  1.06017
      19.8  1.12809
      19.9  1.19065
      20  1.24582
      20.1  1.29172
      20.2  1.32667
      20.3  1.34934
      20.4  1.35886
      20.5  1.35484
      20.6  1.33744
      20.7  1.30735
      20.8  1.26573
      20.9  1.21411
      21  1.15432
      21.1  1.08833
      21.2  1.01818
      21.3  0.945799
      21.4  0.872989
      21.5  0.801309
      21.6  0.732057
      21.7  0.666247
      21.8  0.604617
      21.9  0.547647
      22  0.495594
      22.1  0.448528
      22.2  0.406373
      22.3  0.368944
      22.4  0.33598
      22.5  0.30718
      22.6  0.282219
      22.7  0.260772
      22.8  0.242527
      22.9  0.227195
      23  0.214517
      23.1  0.204266
      23.2  0.196252
      23.3  0.190323
      23.4  0.186362
      23.5  0.184293
      23.6  0.184073
      23.7  0.185698
      23.8  0.189201
      23.9  0.194651
      24  0.202154
      24.1  0.211855
      24.2  0.223934
      24.3  0.23861
      24.4  0.256133
      24.5  0.276788
      24.6  0.30088
      24.7  0.328733
      24.8  0.360673
      24.9  0.39701
      25  0.438016
      25.1  0.483897
      25.2  0.534759
      25.3  0.59057
      25.4  0.651121
      25.5  0.71599
      25.6  0.784501
      25.7  0.855708
      25.8  0.928381
      25.9  1.00102
      26  1.07188
      26.1  1.13904
      26.2  1.20049
      26.3  1.25424
      26.4  1.29841
      26.5  1.33137
      26.6  1.35188
      26.7  1.35913
      26.8  1.35284
      26.9  1.33324
      27  1.30112
      27.1  1.2577
      27.2  1.20457
      27.3  1.1436
      27.4  1.07677
      27.5  1.00611
      27.6  0.933541
      27.7  0.860818
      27.8  0.789463
      27.9  0.720727
      28  0.655575
      28.1  0.594702
      28.2  0.538546
      28.3  0.487331
      28.4  0.4411
      28.5  0.399755
      28.6  0.363096
      28.7  0.330855
      28.8  0.302723
      28.9  0.278375
      29  0.257488
      29.1  0.239753
      29.2  0.224884
      29.3  0.212629
      29.4  0.202766
      29.5  0.195113
      29.6  0.189522
      29.7  0.185884
      29.8  0.184127
      29.9  0.184217
      30  0.186154
      30.1  0.189979
      30.2  0.195765
      30.3  0.203627
      30.4  0.213714
      30.5  0.226214
      30.6  0.24135
      30.7  0.259379
      30.8  0.28059
      30.9  0.305292
      31  0.333809
      31.1  0.366468
      31.2  0.403572
      31.3  0.445386
      31.4  0.4921
      31.5  0.5438
      31.6  0.600428
      31.7  0.661742
      31.8  0.727277
      31.9  0.796316
      32  0.867863
      32.1  0.940642
      32.2  1.01311
      32.3  1.08348
      32.4  1.14983
      32.5  1.21012
      32.6  1.26238
      32.7  1.30477
      32.8  1.33572
      32.9  1.35404
      33  1.35902
      33.1  1.35046
      33.2  1.32869
      33.3  1.29457
      33.4  1.2494
      33.5  1.19482
      33.6  1.13272
      33.7  1.06511
      33.8  0.993991
      33.9  0.921279
      34  0.848687
      34.1  0.777693
      34.2  0.709499
      34.3  0.645026
      34.4  0.584921
      34.5  0.529586
      34.6  0.47921
      34.7  0.433811
      34.8  0.39327
      34.9  0.357374
      35  0.325846
      35.1  0.298373
      35.2  0.27463
      35.3  0.254294
      35.4  0.23706
      35.5  0.222647
      35.6  0.210809
      35.7  0.201329
      35.8  0.194032
      35.9  0.188776
      36  0.185458
      36.1  0.184013
      36.2  0.184413
      36.3  0.186664
      36.4  0.190812
      36.5  0.196938
      36.6  0.205163
      36.7  0.215642
      36.8  0.228568
      36.9  0.244172
      37  0.262715
      37.1  0.28449
      37.2  0.309811
      37.3  0.339003
      37.4  0.372388
      37.5  0.410267
      37.6  0.452894
      37.7  0.500444
    
    RK4_PRB:
      Normal end of execution.
    
    19 December 2013 02:17:27 PM
    Press any key to continue . . .
    is there something wrong with my compiler?

  5. #5
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Try changing

    Code:
    double rk4 ( double t0, double u0, double dt, double f ( double t, double u ) )
    to

    Code:
    double rk4 ( double t0, double u0, double dt, double (*f) ( double t, double u ) )
    for both the function prototype and the definition.

    I have no idea why that wouldn't emit a warning (maybe there's nothing wrong with it??) but it seems wrong to me.

  6. #6
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Well, apparently your original syntax is valid which explains why there is no errors or warnings (I've never seen that syntax before... I'll note it down).

    What compiler and OS are you using?

  7. #7
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Wait; you're not running out of buffer room in the console are you?

    Try programname > output.txt

    And see what's in output.txt

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Hodor View Post
    Well, apparently your original syntax is valid which explains why there is no errors or warnings (I've never seen that syntax before... I'll note it down).
    C99:6.3.2.1p4 A function designator is an expression that has function type. Except when it is the operand of the sizeof operator or the unary & operator, a function designator with type ''function returning type'' is converted to an expression that has type ''pointer to function returning type''.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  9. #9
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by oogabooga View Post
    C99:6.3.2.1p4 A function designator is an expression that has function type. Except when it is the operand of the sizeof operator or the unary & operator, a function designator with type ''function returning type'' is converted to an expression that has type ''pointer to function returning type''.
    Also see 6.7.6.3p8: A declaration of a parameter as "function returning type'' shall be adjusted to "pointer to function returning type'', as in 6.3.2.1.

    I've still never seen the syntax before today =) But it's good to learn.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Runge Kutta IV project problem
    By achikha in forum C Programming
    Replies: 24
    Last Post: 06-16-2013, 10:09 PM
  2. Runge Kutta Projectile Problem
    By meadmead in forum C Programming
    Replies: 2
    Last Post: 11-25-2011, 02:25 PM
  3. Runge Kutta 4
    By rhenley in forum C Programming
    Replies: 20
    Last Post: 05-23-2010, 01:39 PM
  4. Runge-Kutta Problem
    By nickbrown05 in forum C Programming
    Replies: 10
    Last Post: 11-13-2008, 11:00 AM
  5. Runge Kutta for electron trajectories
    By QueenB in forum C Programming
    Replies: 1
    Last Post: 05-03-2007, 08:12 PM