/* sample.c
*
* Sample C program for AME 437.
*
* This program numerically solves and graphs the
* solution of the differential equation
*
* x'' + 4x' + 2x = sin(2.45 t) with
* x(0) = 0, x'(0) = 1.11.
* for t = 0 to 50;
*
* It uses simple Euler integration.
*
* To compile it on a unix machine, type:
*
* gcc sample.c -o sample -lm
*
* To execute it, type:
*
* sample (or ./sample, if necessary).
*
* It should work in Windows as well, but you will
* have to remove the "system" functions and plot
* the data some other way.
*
* The plot is stored in a file called "plot.eps".
* It is overwritten each time the program is executed.
*/
#include
#include
main() {
double x[2],xold[2],t,dt;
FILE *fp;
/* make sure to double check that dt is small enough! */
dt = 0.001;
/* open the output file */
fp = fopen("data.d","w");
/* initial conditions */
x[0] = 0;
x[1] = 1.11;
/* main loop */
for(t=0;t<=50;t+=dt) {
fprintf(fp,"%f\t%f\t%f\n",t,x[0],x[1]);
xold[0] = x[0];
xold[1] = x[1];
x[0] += xold[1]*dt;
x[1] += (sin(2.45*t) - 4*xold[1] - 2*xold[0])*dt;
}
fclose(fp);
/* make the plot and display it */
system("gnuplot<