• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest Sep 15th, 2019 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <stdio.h>
2. #include <math.h>
3.
4. double f1 (double);
5. double df1 (double);
6. double d2f1 (double);
7. double f2 (double);
8. double df2 (double);
9. double d2f2 (double);
10. double f3 (double);
11. double df3 (double);
12. double d2f3 (double);
13. double get_masheps ();
14. double get_h (int, int, double);
15. double get_dpm (double, double (*f) (double), double);
16. double get_dp (double, double (*f) (double), double);
17. double get_dpdm (double, double (*f) (double), double);
18. double get_diff (double, double (*df) (double), double);
19. void calculate (double (*f) (double), double (*df) (double), double (*d2f) (double), double, double);
20.
21. double f1 (double x)
22. {
23.   return sin (x);
24. }
25.
26. double df1 (double x)
27. {
28.   return cos (x);
29. }
30.
31. double d2f1 (double x)
32. {
33.   return (-sin (x));
34. }
35.
36. double f2 (double x)
37. {
38.   return 100 * 100 * 10 * sin (x);
39. }
40.
41. double df2 (double x)
42. {
43.   return 100 * 100 * 10 * cos (x);
44. }
45.
46. double d2f2 (double x)
47. {
48.   return -100 * 100 * 10 * sin (x);
49. }
50.
51. double f3 (double x)
52. {
53.   return tan (x);
54. }
55.
56. double df3 (double x)
57. {
58.   return 1. / (cos (x) * cos (x));
59. }
60.
61. double d2f3 (double x)
62. {
63.   return 2. * sin (x) / (cos (x) * cos (x) * cos (x));
64. }
65.
66. double get_masheps ()
67. {
68.   double tmp = 1.;
69.   while (tmp + 1. > 1.)
70.     tmp /= 2.;
71.   return tmp * 2.;
72. }
73.
74. double get_h (int k, int m, double eps)
75. {
76.   double res = 0.;
77.   res = pow (10, k) * pow (eps, 1. / m);
78.   return res;
79. }
80.
81. double get_dpm (double h, double (*f) (double), double x)
82. {
83.   double tmp = 0.;
84.   tmp = (f (x + h) - f (x - h)) / (2. * h);
85.   return tmp;
86. }
87.
88. double get_dp (double h, double (*f) (double), double x)
89. {
90.   double tmp = 0.;
91.   tmp = (f (x + h) - f (x)) / h;
92.   return tmp;
93. }
94.
95. double get_dpdm (double h, double (*f) (double), double x)
96. {
97.   double tmp = 0.;
98.   tmp = (f (x + h) - 2 * f (x) + f (x - h)) / (h * h);
99.   return tmp;
100. }
101.
102. double get_diff (double my_df, double (*df) (double), double x)
103. {
104.   double res = 0.;
105.   res = fabs (my_df - df (x));
106.   return res;
107. }
108.
109. void calculate (double (*f) (double), double (*df) (double), double (*d2f) (double), double x, double eps)
110. {
111.   double h = 0., dpm = 0., dp = 0., dpdm = 0., res = 0.;
112.   int m = 0;
113.
114.   m = 3;
115.   for (int k = -2; k < 3; k++)
116.     {
117.       h = get_h (k, m, eps);
118.       dpm = get_dpm (h, f, x);
119.       res = get_diff (dpm, df, x);
120.       printf ("h = %.2e,  dpm = %.10e, diff = %e\n", h, dpm, res);
121.     }
122.   printf ("\n");
123.
124.   m = 2;
125.   for (int k = -2; k < 3; k++)
126.     {
127.       h = get_h (k, m, eps);
128.       dp = get_dp (h, f, x);
129.       res = get_diff (dp, df, x);
130.       printf ("h = %.2e,   dp = %.10e, diff = %e\n", h, dp, res);
131.     }
132.   printf ("\n");
133.
134.   m = 4;
135.   for (int k = -2; k < 3; k++)
136.     {
137.       h = get_h (k, m, eps);
138.       dpdm = get_dpdm (h, f, x);
139.       res = get_diff (dpdm, d2f, x);
140.       printf ("h = %.2e, dpdm = %.10e, diff = %e\n", h, dpdm, res);
141.     }
142.   printf ("\n");
143. }
144.
145. int main (void)
146. {
147.   double eps = 0.;
148.   double x = 1.;
149.   eps = get_masheps ();
150.   printf ("eps = %e\n", eps);
151.
152.   printf ("----------- sin(x) -----------\n");
153.   calculate (f1, df1, d2f1, x, eps);
154.
155.   printf ("----------- 10^5 * sin(x) -----------\n");
156.   calculate (f2, df2, d2f2, x, eps);
157.
158.   printf ("----------- tg(x) -----------\n");
159.   x = 1.59;
160.   calculate (f3, df3, d2f3, x, eps);
161.
162.   return 0;
163. }
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.

Top