/* Sample C code for homework 3 * * to compile it, type * * cc hw3.c -o hw3 -lm * */ #include #include main() { float x[2],t,dt; /* declare the variables */ float tfinal; FILE *fp; tfinal = 50.0; dt = 0.001; /* initial conditions */ x[0] = 0.0; x[1] = 0.1; /* open data.d to be written */ fp = fopen("data.d","w"); for(t=0;t<=tfinal;t+=dt) { x[0] += x[1]*dt; x[1] += (-0.25*x[1] + x[0] - pow(x[0],3) + 0.3*cos(t))*dt; /* print data to data.d */ fprintf(fp,"%f \t %f \t %f \n",t,x[0],x[1]); } /* close data.d */ fclose(fp); }