Runge-Kutta 4th order program copying array element

Click For Summary
SUMMARY

The discussion centers on debugging a Runge-Kutta 4th order implementation in C++. The user reports an issue where the array element k[1][3] is incorrectly copying the value from k[2][3]. The problem is traced to the assignment line k[j][l]= f[j], which may be causing unintended references due to the way the array px is populated. Additionally, the user is advised to check for undefined variables, specifically the variable 'i', which is crucial for the loop iterations.

PREREQUISITES
  • Understanding of C++ programming language
  • Familiarity with numerical methods, specifically the Runge-Kutta method
  • Knowledge of array manipulation and indexing in C++
  • Basic debugging techniques in programming
NEXT STEPS
  • Review C++ array indexing and memory management
  • Learn about debugging techniques in C++ using tools like GDB
  • Study the implementation of the Runge-Kutta method for solving ordinary differential equations
  • Investigate the use of undefined variables and their impact on program execution
USEFUL FOR

Developers working on numerical simulations, students learning numerical methods, and programmers debugging C++ code related to mathematical computations.

Leonardo Machado
Messages
56
Reaction score
2
Hello, thanks for your interest in may help me, i appreciate it, really.

My question is, I've wrote this code to solve an physical EDO system. But for some reason it copies the element k[1][3] at k[2][3].

After i call the initials elements the code becomes:

Code:
   for ( int n=1 ; n<=1; n++ ){
       
        pe= dpe*(n-1);
       
        cout << endl << endl << "pe= " << pe << endl ;
             
        file << endl << "pe= " << pe << "  ";
       
        for ( int l=1 ; l<=4 ; l++){
           
            for ( int j=1 ; j<=i ; j++){
               
                   
               
            if ( l==1){
                   
                px[j][l]= p[j];
                pex[j][l]= pe;
               
                }
           
            if ( l==2){
               
                px[j][l]= p[j]+ dpe*k[j][1]/2;
                pex[j][l]= pe+ dpe/2;
           
                }
               
            if ( l==3){
               
                px[j][l]= p[j]+ dpe*k[j][2]/2;
                pex[j][l]= pe+ dpe/2;
           
                }
           
            if ( l==4){               
                                   
                    px[j][l]= p[j]+ dpe*k[j][3];
                    pex[j][l]= pe+ dpe;
       
                }
               
                cout << "px" << j << "." << l << "= " << px[j][l] << "  " ; 
           
            }
           
       
            for ( int j=1; j<=i; j++){
           
               
                if ( j==1){
                       
                    f[j]=px[3][l];
                       
                    }
                   
                if ( j==2){
                       
                    f[j]= px[4][l];
                       
                    }   
   
                if (j==3){
                       
                        f[j]= (-GM) * px[1][l] / pow ( sqrt (  pow ( px[1][l], 2) + pow ( px[2][l], 2)), 3);
                    }
                   
                if (j==4){
                       
                        f[j]= (-GM) * px[2][l] / pow ( sqrt (  pow ( px[1][l], 2) + pow ( px[2][l], 2)), 3);
                    }   
               
                k[j][l]= f[j];
               
            }
             
            for ( int j=1; j<=i; j++){
               
                cout << "k" << j << "." << l << "= " << k[j][l] << "  " ;
               
            }
           
            cout << endl << endl;
           
        }
  
           
       
        for ( int j=1 ; j<=i ; j++){
          
                   
            file << p[j] << "  ";
            cout << "p." << j << "= " << p[j] << "  ";
            p[j]=p[j]+ dpe*(k[j][1]+k[j][4]+ 2*(k[j][2]+k[j][3]))/6;
           
           
        }
           
       
    }
       
    }

Erro copia de elementos.png
Help me please, i have no more ideas !
 
Technology news on Phys.org
You may debug the program and observe how each variable gets changed in every step.

I think this line
Code:
k[j][l]= f[j];
may start the problem.
E.g j=1, l=3 the k1,3=f1=px3,3
j=2, l=3 the k2,3=f2=px4,3
Looking up further above, you will see
px3,3 = px4,3

I may overlook something though.
 
Hey Leonardo Machado.

Your code has some undefined variables - where have you defined (as an example) the variable i?
 

Similar threads

Replies
12
Views
3K
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 20 ·
Replies
20
Views
2K
  • · Replies 97 ·
4
Replies
97
Views
10K