我有一个程序是模拟2个耦合振子的运动方程,哪位大虾帮小弟把它改成N个耦合振子的,N能由用户输入,原程序如下:
#include <stdio.h>
#include <conio.h>
#define N 10
void main()
{
int i,n=4,j=0;
double u[N]={0,0.5,0,0},v[N],a[N],t=0,dt=1e-4,tmax=20,k=1,M=1;
FILE *fp;
fp=fopen("zhendong.txt","w");
fprintf(fp,"t");
for(i=1;i<n-1;i++)fprintf(fp,"\tu%d",i);
fprintf(fp,"\n");
printf("Calculating...\n");
while(t<=tmax){
if(j%100==0){
fprintf(fp,"%lf",t);
for(i=1;i<n-1;i++)fprintf(fp,"\t%lf",u[i]);
fprintf(fp,"\n");
}
for(i=1;i<n-1;i++){
a[i]=(u[i-1]+u[i+1]-2*u[i])*k/M;
v[i]+=a[i]*dt;
u[i]+=v[i]*dt;
}
t+=dt;
j++;
}
fclose(fp);
printf("Finished!\n");
getch();
}