• Sign Up
• Login
• API
• FAQ
• Tools
• Archive
SHARE
TWEET # Untitled a guest Jul 19th, 2018 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <stdio.h>
2. #include <stdlib.h>
3. #include <math.h>
4. #include <plot.h>
5.
6. const double PI = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282;
7. const double F = 2;
8.
9. double arr;
10.
11. double wave(double t)
12. {
13.   double ans = 0, T = 1/F;
14.   int n;
15.   for(n = 0;n < 16;++n)
16.   {
17.     double tmp;
18.     if(t == n*T) tmp = 1;
19.     else tmp = sin(PI*(t-n*T)/T) / (PI*(t-n*T)/T);
20.     ans += arr[n]*tmp;
21.   }
22.   return ans;
23. }
24.
25. double original(double t)
26. {
27.   //return cos(5 + 1.5)*sin(0.2*PI*t) + 1.3*cos(0.4*PI*t) - 0.9*sin(0.5*PI*t) + 0.5*cos(0.6*PI*t);
28.   return cos(2*PI*t);
29. }
30.
31. void plot(double(*func)(double),double a,double b,double d)
32. {
33.   pl_fmove(a,func(a));
34.   double x;
35.   for(x = a+d;x <= b;x+=d)
36.     pl_fcont(x,func(x));
37. }
38.
39. int main()
40. {
41.   pl_parampl("PAGESIZE","letter");
42.
43.   int handle = pl_newpl("png",0,stdout,0);
44.   pl_selectpl(handle);
45.   pl_openpl();
46.   pl_fspace(0.0,-2.0,6.0,2.0);
47.   pl_flinewidth(0.05);
48.   pl_erase();
49.
50.   double dt = 0.01;
51.
52.   pl_pencolorname("red");
53.   plot(original,0,5,dt);
54.
55.   int i;
56.   for(i = 0;i < 16;++i)
57.     arr[i] = original(i/F);
58.
59.   pl_pencolorname("blue");
60.   plot(wave,0,5,dt);
61.
62.   double t, error = 0;
63.   for(t = 0;t < 8;t += dt)
64.   {
65.     double a = wave(t), b = original(t);
66.     //printf("a:%f, b:%f, error = %f\n",a,b,fabs(a-b));
67.     error += fabs(a - b);
68.   }
69.   fprintf(stderr,"avg error = %f\n",error/(8/dt));
70.
71.   pl_closepl();
72.   pl_selectpl(0);
73.   pl_deletepl(handle);
74.
75.   return 0;
76. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!

Top