forked from Maria02179/mp1-2020-382003-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Teylor.c
110 lines (101 loc) · 2.27 KB
/
Teylor.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "stdio.h"
#include "math.h"
#include <locale.h>
double PI = 3.14159265358979323846;
double Arccos(double x, double eps, int*i)
{
int k = 1;
double x1 = x, x2, n = 1.0 * (k) / (k + 1), N = x * x * x, sum = x;
x2 = N * n / (k + 2);
while (fabs(x2 - x1) >= eps)
{
if ((k + 1) / 2 >= (*i)) {
break; }
sum += x2;
(k) += 2;
n *= 1.0 * (k) / (k + 1);
N *= x * x;
x1 = x2;
x2 = N * n / (k + 2);
}
if (((k + 1) / 2) <= (*i)) (*i) = (k + 1) / 2;
else (*i) -= (k + 1) / 2;
return PI*0.5 - sum;
}
double Csc(double x, double eps, int* count)
{
double sum = 0;
double z = 1;
int n = 0;
while (fabs(z) > eps)
{
sum += z;
if (n >= (*count))
break;
n++;
z *= -x * x / (2.0 * n - 1.0) / (2.0 * n);
}
if ((n != 1) && (n <= (*count)))
(*count) = n;
else
(*count) -= (n);
if (n == 1)
(*count) = n;
return sum;
}
void Input(double* x, int* y, double* eps) {
printf("\nÂâåäèòå çíà÷åíèå X:");
scanf_s("%lf", x);
printf("\nÂâåäèòå êîë-âî ÷ëåíîâ:");
scanf_s("%d", y);
printf("\nÂâåäèòå ïîãðåøíîñòü:");
scanf_s("%lf", eps);
}
void menu()
{
printf("Âûáåðèòå ôóíêöèþ äëÿ ðàçëîæåíèÿ ïî Òåéëîðó:\n");
printf("1. Arccos(x) \n");
printf("2. Csc(x)\n");
printf("0. Âûõîä\n\n");
}
void main()
{
setlocale(LC_CTYPE, "Russian");
double x = 0;
double eps = 0;
int t = 3;
while (t != 0)
{
menu();
scanf_s("%d", &t);
switch (t)
{
case 1:
{
double x;
double rez = 0;
double eps;
int count;
Input(&x, &count, &eps);
rez = Arccos(x, eps, &count);
printf("Ìî¸ çíà÷åíèå %10.8f Êîë-âî ÷ëåíîâ %d Arccos(x) %10.8f \n", rez, count, acos(x));
/*printf("%14.13f %5d %16.13f \n", rez, count, acos(x));*/
printf("\nÐàçíèöà: %10.8f\n", (rez - acos(x)));
break;
}
case 2:
{
double x;
double rez = 0;
double eps;
int count;
Input(&x, &count, &eps);
rez = Csc(x, eps, &count);
printf("Ìî¸ çíà÷åíèå %10.8f Êîë-âî ÷ëåíîâ %d Csc(x) %10.8f \n",1 / rez, count, 1 / cos(x));
/*printf("%10.8f %5d %14.8f \n", 1/rez, count, 1/cos(x));*/
printf("\nÐàçíèöà: %10.8f\n", (1 / rez - 1 / cos(x)));
break;
}
}
}
}