微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 微波和射频技术 > 天线设计和射频技术 > 1 fdtd code error,pls help me

1 fdtd code error,pls help me

时间:04-11 整理:3721RD 点击:
hi i try to understand fdtd code,here 1 d code but it is error,any buddy help me
thanks regards
code
# include <math.h>
# include <stdlib.h>
# include <stdio.h>
#define ke 200
main()
{
kc = ke/2;
to = 40.0;
spread = 12;
t=0;
nsteps = 1;
float ex[ke],hy[ke];
int n,k,kc,ke,nsteps;
float t;
float to,spread,pulse;
FILE *ftp, *fopen();
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++)
{print ("%3d %6.2f %6.2\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)
print ( "t= %5f\n",t);
}
}

I don't know what that program is suppose to do, but it's full of C syntax errors.
I fixed the obvious errors, but I don't know if it the output is correct or not:

Code:
#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;
}

hi ech47,thanks ,that program in sullivan book,in first chapter,it is simple but after i run it,it has more error,that iignore it
regards

I grabbed the Sullivan book.

The program has array indexing bugs. Try changing the first loop from this:
for (k = 1; k < ke; k++)
to this:
for (k = 0; k < ke; k++)

Those final printf statements probably need to be ex[k-1] and hy[k-1] instead of ex[k] and hy[k].

There may be more similar bugs. Looks like Sullivan translated it (poorly) from FORTRAN or something like that.

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top