#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#define ke 200
int main(void)
{
float ex[ke], hy[ke];
int n, k, kc, nsteps;
float t;
float to, spread, pulse;
FILE *fp;
kc = ke / 2;
to = 40.0;
spread = 12;
t = 0;
nsteps = 1;
for (k = 1; k < ke; k++)
{
ex[k] = 0;
hy[k] = 0;
}
kc = ke / 2;
to = 40.0;
spread = 12;
t = 0;
nsteps = 1;
while (nsteps > 0)
{
printf("nsteps-->");
scanf("%d", &nsteps);
printf("%d\n", nsteps);
n = 0;
for (n = 1; n <= nsteps; n++)
{
t = t + 1;
for (k = 1; k < ke; k++)
{
ex[k] = ex[k] + .5 * (hy[k - 1] - hy[k]);
}
pulse = exp(-.5 * (pow((to - t) / spread, 2)));
ex[kc] = pulse;
printf("%5.1f %6.2f\n", to - t, ex[kc]);
for (k = 0; k < ke - 1; k++)
{
hy[k] = hy[k] + .5 * (ex[k] - ex[k + 1]);
}
}
for (k = 1; k <= ke; k++)
{
printf("%3d %6.2f %6.2f\n", k, ex[k], hy[k]);
}
fp = fopen("ex", "w");
for (k = 1; k <= ke; k++)
{
fprintf(fp, " %6.2f \n", ex[k]);
}
fclose(fp);
fp = fopen("hy", "w");
for (k = 1; k <= ke; k++)
{
fprintf(fp, " %6.2f \n", hy[k]);
}
fclose(fp);
printf("t= %5f\n", t);
}
return 0;
}