I'm trying to evaluate the perturbations magnitude between 2 body orbiting a central one in three dimensions. In order to do this I need to have an estimate of the error, which I did using Richardson extrapolation as described here. I'm using Runge-Kutta 4 and also velocity Verlet. Things seems good in the unperturbed problem, which means I obtain an error $O(10^{-7})$ and $O(10^{-12})$ for Verlet and Runge-Kutta respectively by using a time step of $0.001$. When I consider the perturbations, only velocity Verlet still give me the expected error, while Runge-Kutta give me an error of the same order of Verlet, using the same previous time step. The same happens even if I reduce or increase the time step. Actually for the perturbed problem I already expected something bad related to the error, since that's what I obtained for the Richardson's fraction for Runge-Kutta
As it can be seen, the level of the figure should be about $2^p = 16$, but it's $2$ actually. That sort of spikes are present also in the same plot for velocity Verlet, but for the latter their magnitudes are much less than that in the plot above and the $F_h$ values are about $4$ anyway, as expected for Verlet. I checked all the Runge-Kutta code and the ode system code but I didn't find any error. Moreover I tested both methods with a simpler function obtaining correct results, so I can't understand the reason for such wrong error estimates for Runge-Kutta.
Edit 1
The function code is the following
vector<double> F(double t, vector<double> x, vector<CB> objs, vector<double> s) {
//objs store the masses of the objects
//s store the positions of the objects in the order [x1,y1,z1,x2,y2,z2,...];
//x store the positions and velocity of the target (perturbed) object in the order [x,Vx,y,Vy,z,Vz]
vector<double> p(x.size(),0);
double m, d;
vector<double> y(x.size()/2,0);
for (int i = 0; i < objs.size(); i++) {
//temporary store the mass of the i-th object
m = objs[i].CB::getmass();
//temporary store the position of the i-th object from the s vector
y = {s[i*x.size()/2], s[i*x.size()/2 + 1], s[i*x.size()/2 + 2]};
d = sqrt(pow((x[0] - y[0]),2) + pow((x[2] - y[1]),2) + pow((x[4] - y[2]),2));
//x system
p[0] = x[1];
p[1] += -G*m*(x[0] - y[0])/pow(d,3);
//y system
p[2] = x[3];
p[3] += -G*m*(x[2] - y[1])/pow(d,3);
//z system
p[4] = x[5];
p[5] += -G*m*(x[4] - y[2])/pow(d,3);
}
return p;
}