Runge-Kutta 4th order program copying array element

AI Thread Summary
The discussion revolves around debugging a code snippet designed to solve a physical EDO (Ordinary Differential Equation) system. The main issue identified is that the element k[1][3] is being incorrectly copied to k[2][3], which suggests a problem in how values are assigned within nested loops. The user points out that the line k[j][l]= f[j] might be the source of the problem, particularly when j=1, l=3 and j=2, l=3, as both seem to reference the same value from px. Additionally, there are concerns about undefined variables in the code, specifically the variable 'i', which has not been defined in the provided context. The user seeks assistance in debugging the program to understand how variable values change at each step.
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?
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top