##### Sections
You are here: Home / ft.c

# ft.c

/* * Simple c code to calculate Fourier Series */ #include #include #include #define pi 3.1415926535897931 int main(){ double x, xmin, xmax, L, f, bn; int n, nmax, ix, ixmax; FILE *fp; /* get input data from the user*/ printf("\nFourier series calculator\n"); printf("Number of terms in series, nmax = ? "); scanf("%d",&nmax;); printf("Calculate function between xmin and xmax:\nxmin = ? "); scanf("%lf",&xmin;); printf("xmax = ? "); scanf("%lf",&xmax;); printf("Number of points at which to calculate series, ixmax = ? "); scanf("%d",&ixmax;); fp=fopen("ft.txt","w"); /* open data file (ft.txt) to store output data */ /* As usual the function is defined between 0 and L */ L=pi; /* Loop from x = xmin to x = xmax */ for (ix=0; ix<=nmax; n++) { /* Calculate the Fourier coefficient bn */ bn = 2.0*L/(pi*n)*pow(-1,n+1); /* sum the Fourier series */ f = f + bn * sin(n*pi*x/L); } fprintf(fp,"%e\t%e\n",x,f); /* print the result to the file ft.txt */ } fclose(fp); /* close data file */ }