I am solving an ODE with the aid of Numerical Recipes routines. In order to propagate my solution in time I used a for loop but this loop does not advance the solution as the time progresses and no error is displayed. What may be the cause of this behaviour I am pasting my code below:
//Compilation and running
//gcc Q3.c RK4.c nrutil.c -lm -o Q3 && ./Q3
#include <stdio.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define N 2
#define H 10
#define h 0.01
void derivs(float t,float y[],float dydt[])
{
dydt[1]=y[2];
dydt[2] = -32 -y[1];
}
int main(void)
{
int k,j;
// The time is set to be zero when the mass is released
float t=0.0,*y,*dydt,*yout;
y=vector(1,N);
dydt=vector(1,N);
yout=vector(1,N);
y[1]= H;
y[2]= 0;
derivs(t,y,dydt);
printf("\n%16s %5s %12s %12s %12s\n",
"Function:","y","dydt","d2ydt2","t");
for(k = 0; k < 5; k++);
{
rk4(y,dydt,N,t,h,yout,derivs);
printf("\nfor a step size of: %6.2f\n",h);
printf("%12s","rk4:");
for (j=1;j<=N;j++) printf(" %12.6f",yout[j]);
t += h;
printf("%12.6f %12.6f \n", -32 - yout[2], t);
y[1] = yout[1];
y[2] = yout[2];
}
free_vector(yout,1,N);
free_vector(dydt,1,N);
free_vector(y,1,N);
return 0;
}
#undef NRANSI
The problem is in the for loop with "k" as the loop variable, the loop does not iterate more than once due to an unknown reason. I would be grateful for any assistance on this topic.
Remove the semicolons from your for statements.
for(k = 0; k < 5; k++); // loops an empty statement
{
//doesn't loop
}