Runge-Kutta 4th order program copying array element

In summary: This could be an issue for your code copying elements incorrectly.In summary, the individual is asking for help with their code that copies elements incorrectly. They are unsure of where the issue lies and are seeking assistance with debugging.
  • #1
Leonardo Machado
57
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
  • #2
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.
 
  • #3
Hey Leonardo Machado.

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

Related to Runge-Kutta 4th order program copying array element

1. What is a Runge-Kutta 4th order program?

A Runge-Kutta 4th order program is a numerical method used to solve differential equations. It is a widely used algorithm in scientific computing and is known for its accuracy and stability.

2. How does a Runge-Kutta 4th order program work?

A Runge-Kutta 4th order program works by dividing a small time interval into smaller subintervals and using a weighted average of the slope at different points within each subinterval to approximate the solution of the differential equation at the next time step.

3. Why is it called "4th order"?

The "4th order" in Runge-Kutta 4th order refers to the degree of the error term in the algorithm. This means that the error in the approximation is proportional to the fourth power of the size of the time step, making it a highly accurate method.

4. What is the purpose of copying array elements in a Runge-Kutta 4th order program?

Copying array elements in a Runge-Kutta 4th order program is necessary for the algorithm to work correctly. The array elements hold the intermediate values of the solution at different time steps, and copying them ensures that the algorithm is using the correct values for each iteration.

5. What are the advantages of using a Runge-Kutta 4th order program over other methods?

Compared to other methods, a Runge-Kutta 4th order program is known for its high accuracy and stability. It also requires fewer function evaluations, making it more efficient. Additionally, it can handle a wide range of differential equations, making it a versatile and widely used method in scientific computing.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
681
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
2
Views
3K
Back
Top