/* ************************************************************************ * euler.c: 1st order Euler solution for an ordinary differential * equation ************************************************************************ */ #include #include #define MIN 0.0 /* minimum x */ #define MAX 1.0 /* maximum x */ #define PI 3.1415926 main() { double f(double x, double y); double x, y, dx, dy, yreal, yerror; FILE *output; /* save data in euler.dat */ output = fopen("euler.dat","w"); printf("Input: dx\n"); scanf("%lf", &dx); printf("dx=%g\n", dx); x = MIN; /* initial position */ y = 0.0; yreal = y; yerror = 0.0; fprintf(output, "%f\t%f\t%f\t%e\n", x, y, yreal, yerror); while(x <= MAX) { dy = dx*f(x, y); x += dx; y += dy; yreal=sin(2.0*PI*x); yerror=y-yreal; fprintf(output, "%f\t%f\t%f\t%e\n", x, y, yreal, yerror); } printf("data stored in euler.dat\n"); fclose(output); } /*-----------------------end of main program--------------------------*/ /* definition of equations */ double f(double x, double y) { return(2.0*PI*cos(2.0*PI*x)); /* RHS of first equation */ }