I'm doing a homework assignment in which I have to model the motion of an aircraft with a 4th order RK approximation. However, for some reason I keep getting results that indicate that all of my K values are zero when I iterate.
my main loop is as follows:
for(i=0;i<duration;i++){
t[i]=(i)*delta_t;
rho[i]=1.225*(exp((-0.0001)*z[i]));
Kx1=f(i, V_x[i], V_z[i], rho[i], z[i], m, S, Cl);
Kz1=g(i, V_x[i], V_z[i], rho[i], z[i], m, S, Cl, G);
Kx2=f(i, (V_x[i]+((Kx1*h)/2)), (V_z[i]+((Kz1*h)/2)), rho[i], z[i], m, S, Cl);
Kz2=g(i, (V_x[i]+((Kx1*h)/2)), (V_z[i]+((Kz1*h)/2)), rho[i], z[i], m, S, Cl, G);
Kx3=f(i, (V_x[i]+((Kx2*h)/2)), (V_z[i]+((Kz2*h)/2)), rho[i], z[i], m, S, Cl);
Kz3=g(i, (V_x[i]+((Kx2*h)/2)), (V_z[i]+((Kz2*h)/2)), rho[i], z[i], m, S, Cl, G);
Kx4=f(i, (V_x[i]+((Kx3*h)/2)), (V_z[i]+((Kz3*h)/2)), rho[i], z[i], m, S, Cl);
Kz4=g(i, (V_x[i]+((Kx3*h)/2)), (V_z[i]+((Kz3*h)/2)), rho[i], z[i], m, S, Cl, G);
V_x[i+1]=V_x[i]+(delta_t/6)*(Kx1+2*Kx2+2*Kx3+Kx4);
x[i+1]=x[i]+(V_x[i+1])*delta_t;
V_z[i+1]=V_z[i]+(delta_t/6)*(Kz1+2*Kz2+2*Kz3+Kz4);
z[i+1]=z[i]+(V_z[i+1])*delta_t;
t[0]=0;
x[0]=0;
z[0]=4800;
V_z[0]=0;
V_x[0]=210;
}
with V_z, V_x, x, z, and rho all being 1xduration arrays (I have duration defined as 400). f and g are two functions that return acceleration in x and z directions respectively. I've tested both externally and by hand and they work. I reinforced the boundary conditions in the loop (I've written them outside as well) because I was getting weird results otherwise, but that's beside the point of what I'm having trouble with: my K values all seem to be ending up equal to zero because V_x stays at 210 for each iteration, V_z stays at 0, and obviously then x and z don't change.
I created a quick test code in a separate file to calculate the K values without iterating just to make sure the algorithm is sound (all the same BCs, constants, etc., just no loop):
Kx1=f(i, V_x[0], V_z[0], rho[0], z[0], m, S, Cl);
Kz1=g(i, V_x[0], V_z[0], rho[0], z[0], m, S, Cl, G);
Kx2=f(i, (V_x[0]+((Kx1*h)/2)), (V_z[0]+((Kz1*h)/2)), rho[0], z[0], m, S, Cl);
Kz2=g(i, (V_x[0]+((Kx1*h)/2)), (V_z[0]+((Kz1*h)/2)), rho[0], z[0], m, S, Cl, G);
Kx3=f(i, (V_x[0]+((Kx2*h)/2)), (V_z[0]+((Kz2*h)/2)), rho[0], z[0], m, S, Cl);
Kz3=g(i, (V_x[0]+((Kx2*h)/2)), (V_z[0]+((Kz2*h)/2)), rho[0], z[0], m, S, Cl, G);
Kx4=f(i, (V_x[0]+((Kx3*h)/2)), (V_z[0]+((Kz3*h)/2)), rho[0], z[0], m, S, Cl);
Kz4=g(i, (V_x[0]+((Kx3*h)/2)), (V_z[0]+((Kz3*h)/2)), rho[0], z[0], m, S, Cl, G);
This returns the proper K values, so I'm at a complete loss as to why my V_x[i] and V_x[i] aren't changing in the loop.