Complete program not part of it. Program must be written in C. Thanks! (Writing
ID: 3576322 • 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.
Please do all the instructions. Don't answer an incomplete program.
Explanation / Answer
#include<stdio.h>
#include<math.h> //math library for exp and sin
#define pi 3.14159 //pi value
void main()
{
int t,k;
double y; //y as y(t)
FILE *fptr; //file pointer
fptr = fopen("damped_sinusoidal.txt", "w"); //creating and opening a file in write mode
if(fptr == NULL) //if not possible to open file
{
printf("Error!");
exit(1); //exit program
}
fprintf(fptr,"time t value y(t) ");
for(k=0;k<100;k++) //repeating for 100 times
{
t=k;
y=exp(-t*(sin(2*pi*t))); //calculating y(t) for 100 instances 0 to 99
fprintf(fptr,"%d %d ",t, y); //printing to file
}
fclose(fptr); //close opened file
}
File output is;
time t value y(t)
0 0
1 -1868328342
2 1117382322
3 369480767
4 186739916
5 574488066
6 1539576521
7 -1204588046
8 941827090
9 -599690506
10 -1521226598
11 -1808308773
12 -1444937606
13 -413588823
14 1304792047
15 -564179489
16 -1703423208
17 -2089294881
18 -1696617722
19 -498681052
20 1532761123
21 132525661
22 -373102978
23 48741990
24 1432460945
25 -480969907
26 -1359084714
27 -1162850223
28 148329801
29 -1678377801
30 1990664117
31 -1684202345
32 228742878
33 -812067085
34 -461733909
35 1331252922
36 324988346
37 869091985
38 -1275194718
39 -1755098678
40 -511252660
41 -1777647081
42 -1196786703
43 1295478472
44 1469915053
45 -606133664
46 -568790410
47 1652494217
48 1834912184
49 52198357
50 674708287
51 -515528604
52 855089959
53 571820384
54 -1283440384
55 -332203682
56 -784258733
57 1742143070
58 -1254457003
59 -1093991319
60 -1979679075
61 476906612
62 2075913809
63 -1380816937
64 -1204911124
65 -1591160756
66 1857269744
67 653959854
68 -800854773
69 1894753135
70 259541759
71 -1301163468
72 1619743739
73 546201162
74 -111270492
75 -235316018
76 293151865
77 1594998250
78 -502175807
79 -1579003540
80 -1509328152
81 -165187147
82 -1711836527
83 -1722800625
84 -64722206
85 -897420202
86 211040993
87 -895510204
88 218524168
89 -599302386
90 1090206617
91 1138312728
92 -306935928
93 1199350372
94 1514009256
95 790736437
96 -814821556
97 1149702558
98 -1746155822
99 -751118064
Successfully compiled and executed in DevC++
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.