- #1
Leonardo Machado
- 57
- 2
I'm writing a program to compute an ODE solution of the Kepler's problem based on Runge-Kutta 4th order method and today I've past the whole day trying to made it work, but I've failed, maybe you could help me to kill the problem ?
The solutions is cartesian.
Look like when it takes n=1 there is some error that make the force in x and y equals 0, as you can see in this print screen, also it crashes on n=5 :c
I would be gratefull if you help me.
The solutions is cartesian.
Code:
int main(){
int n;
double tau, x[4][200], xk[4], f[4][200], GM, PI = 3.14159265;
double f1[4], f2[4], f3[4], f4[4] ;
GM = 4 * pow ( PI, 2);
x[2][1]= 0;
x[3][1]= 0;
cout << "Insert the inicial distance ( AU): " ;
cin >> x[1][1];
cout << "Choose tau ( Years): " ;
cin >> tau;
//Circular mov condition
x[4][1]= sqrt ( GM / sqrt ( pow ( x[1][1], 2) + pow ( x[2][1], 2)));
for ( n=1 ; n <= 199 ; n++) {
// Definitions of runge-kutta parametres F1, F2, F3 e F4
xk[1]=0;
xk[2]=0;
xk[3]=0;
xk[4]=0;
f[1][n]= x[3][n] + xk[3];
f[2][n]= x[4][n] + xk[4];
f[3][n]= -GM * (x[1][n] + xk[1]) / pow ( sqrt ( pow ( x[1][n] + xk[1], 2) + pow ( x[2][n] + xk[2], 2)), 3);
f[4][n]= -GM * (x[2][n] + xk[2]) / pow ( sqrt ( pow ( x[1][n] + xk[1], 2) + pow ( x[2][n] + xk[2], 2)), 3);
f1[1]= f[1][n];
f1[2]= f[2][n];
f1[3]= f[3][n];
f1[4]= f[4][n];
xk[1]=0.5*tau*f1[1];
xk[2]=0.5*tau*f1[2]/2;
xk[3]=0.5*f1[3];
xk[4]=0.5*tau*f1[4];
f2[1]= f[1][n];
f2[2]= f[2][n];
f2[3]= f[3][n];
f2[4]= f[4][n];
xk[1]=0.5*tau*f2[1];
xk[2]=0.5*tau*f2[2];
xk[3]=0.5*tau*f2[3];
xk[4]=0.5*tau*f2[4];
f3[1]= f[1][n];
f3[2]= f[2][n];
f3[3]= f[3][n];
f3[4]= f[4][n];
xk[1]=0.5*tau*f3[1];
xk[2]=0.5*tau*f3[2];
xk[3]=0.5*tau*f3[3];
xk[4]=0.5*tau*f3[4];
f4[1]= f[1][n];
f4[2]= f[2][n];
f4[3]= f[3][n];
f4[4]= f[4][n];
xk[1]=0;
xk[2]=0;
xk[3]=0;
xk[4]=0;
x[1][n+1]= x[1][n] + tau/6*(f1[1]+f4[1]+2*(f2[1]+f3[1]));
x[2][n+1]= x[2][n] + tau/6*(f1[2]+f4[2]+2*(f2[2]+f3[2]));
x[3][n+1]= x[3][n] + tau/6*(f1[3]+f4[3]+2*(f2[3]+f3[3]));
x[4][n+1]= x[4][n] + tau/6*(f1[4]+f4[4]+2*(f2[4]+f3[4]));
cout << endl
<< "t= " << setw (6) << tau*(n-1)
<< setw (10) << "x= " << setw (6) << x[1][n]
<< setw (10) << "y= " << setw (6) << x[2][n]
<< setw (10) << "vx= " << setw (6) << f[1][n]
<< setw (10) << "vy= " << setw (6) << f[2][n]
<< setw (10) << "fx= " << setw (6) << f[3][n]
<< setw (10) << "fy= " << setw (6) << f[4][n];
}
}
Look like when it takes n=1 there is some error that make the force in x and y equals 0, as you can see in this print screen, also it crashes on n=5 :c
I would be gratefull if you help me.
Last edited by a moderator: