我正在借助Numerical Recipes例程解决ODE问题。为了及时传播我的解决方案,我使用了for循环,但是这个循环不会随着时间的推移推进解决方案并且不会显示错误。可能是这种行为的原因我粘贴下面的代码:
//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
问题出在for循环中,“k”作为循环变量,由于未知原因,循环不会迭代多次。对于这个主题的任何帮助,我将不胜感激。
答案 0 :(得分:5)
从for语句中删除分号。
for(k = 0; k < 5; k++); // loops an empty statement
{
//doesn't loop
}