# C/++/# Runge-Kutta 4th order program copying array element

Tags:
1. Aug 9, 2016

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 (Text):
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;

}

}

}

Help me please, i have no more ideas !!

2. Aug 9, 2016

### Pepper Mint

You may debug the program and observe how each variable gets changed in every step.

I think this line
Code (Text):
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. Aug 14, 2016