Complete program not part of it. Program must be written in C. Thanks! (Writing
ID: 3576108 • Letter: C
Question
Complete program not part of it.
Program must be written in C. Thanks!
(Writing to a file). A damped sine wave is a sinusoidal function whose amplitude approaches zero as time increases. Y(t)= e^-t (sin(2pit)) Write a program that will generate a data file containing 100 data points using the damped sine function above. Use the following formula for time: Where 1 k 100. Each record in your file will have two fields, time and the value of Y(t). Save your file with the name damped_sinusoidal.txt. You can make use of the following math library functions: sin(x) -> returns the sine of a radian angle x. exp(x) -> returns the value of e raised to the xth power.
Explanation / Answer
// C code
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#define PI 3.14159265358979323846
int main()
{
double y,t, angle, radians_angle;
FILE *fptr;
fptr = fopen("damped_sinusoidal.txt", "w");
if(fptr == NULL)
{
printf("Error!");
return 0;
}
int k;
for (k = 1; k <= 100; ++k)
{
t = (k-1)/10;
angle = 2*PI*t;
radians_angle = angle*PI/180;
y = exp((-1*t))*sin(radians_angle);
fprintf(fptr,"%0.9f ", y);
}
fclose(fptr);
return 0;
}
/*
compile: cc codename.c -lm
run: ./a.out
damped_sinusoidal.txt
0.000000000
0.000000000
0.000000000
0.000000000
0.000000000
0.000000000
0.000000000
0.000000000
0.000000000
0.000000000
0.040261685
0.040261685
0.040261685
0.040261685
0.040261685
0.040261685
0.040261685
0.040261685
0.040261685
0.040261685
0.029444951
0.029444951
0.029444951
0.029444951
0.029444951
0.029444951
0.029444951
0.029444951
0.029444951
0.029444951
0.016085422
0.016085422
0.016085422
0.016085422
0.016085422
0.016085422
0.016085422
0.016085422
0.016085422
0.016085422
0.007778960
0.007778960
0.007778960
0.007778960
0.007778960
0.007778960
0.007778960
0.007778960
0.007778960
0.007778960
0.003512134
0.003512134
0.003512134
0.003512134
0.003512134
0.003512134
0.003512134
0.003512134
0.003512134
0.003512134
0.001515794
0.001515794
0.001515794
0.001515794
0.001515794
0.001515794
0.001515794
0.001515794
0.001515794
0.001515794
0.000633244
0.000633244
0.000633244
0.000633244
0.000633244
0.000633244
0.000633244
0.000633244
0.000633244
0.000633244
0.000257976
0.000257976
0.000257976
0.000257976
0.000257976
0.000257976
0.000257976
0.000257976
0.000257976
0.000257976
0.000102968
0.000102968
0.000102968
0.000102968
0.000102968
0.000102968
0.000102968
0.000102968
0.000102968
0.000102968
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.