-
Notifications
You must be signed in to change notification settings - Fork 27
/
Taylor_Petrov.c
154 lines (145 loc) · 2.92 KB
/
Taylor_Petrov.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <stdio.h>
#include <math.h>
typedef double (First)(double);
typedef double (Next)(double, int);
//First
double First_exp(double x)
{
return 1;
}
double First_sin(double x)
{
return x;
}
double First_cos(double x)
{
return 1;
}
double First_cosh(double x)
{
return 1;
}
//Next
double Next_exp(double x, int n)
{
return x/n;
}
double Next_sin(double x, int n)
{
return (-(x * x))/((2*n)*(2*n+1));
}
double Next_cos(double x, int n)
{
return (-(x * x)) / ((2 * n) * (2 * n - 1));
}
double Next_cosh(double x, int n)
{
return x / (2 * n);
}
void function_list()
{
printf("1.Exp\n");
printf("2.Sin\n");
printf("3.Cos\n");
printf("4.Cosh\n");
}
double Taylor(double x, int N, double etalon, double tochhonst, First first, Next next)
{
double elem, elem_next, result;
double check = 1.0;
int i;
elem = first(x);
result = elem;
for (i = 1; (i < N) && !(check < tochhonst); i++)
{
elem_next = elem * next(x, i);
result += elem_next;
elem = elem_next;
check = fabs(result - etalon);
}
printf("----------\n");
printf("Result: %lf\n", result);
printf("Etalon is %lf\n", etalon);
printf("Raznica is %lf\n", fabs(etalon - result));
printf("Slgaaemo is %d\n", i);
printf("----------\n");
return result;
}
int main()
{
int mode = 0;
int function = 0;
double x = 0;
int N = 0;
double tochhonst;
int Nmax = 0;
int i;
while(1==1)
{
printf("Choose mode (1 - default mode 1; 2 - seria mode)(-1 to Exit.)\n");
scanf_s("%d", &mode);
if (mode == -1)
break;
printf("Choose function\n");
function_list();
scanf_s("%d", &function);
printf("Enter x\n");
scanf_s("%lf", &x);
printf("Enter tochhonst:\n");
scanf_s("%lf", &tochhonst);
if ((function == 2) || (function == 3)) //Приводит синус и косинус к интервалу от 0 до pi
{
while (fabs(x) > M_PI * 2)
{
if (x > 0)
x -= M_PI;
if (x < 0)
x += M_PI;
}
}
if (mode == 1)
{
printf("Enter N\n");
scanf_s("%d", &N);
switch (function)
{
case 1:
Taylor(x, N, exp(x), tochhonst, First_exp, Next_exp);
break;
case 2:
Taylor(x, N, sin(x), tochhonst, First_sin, Next_sin);
break;
case 3:
Taylor(x, N, cos(x), tochhonst, First_cos, Next_cos);
break;
case 4:
Taylor(x, N, cosh(x), tochhonst, First_cosh, Next_cosh);
break;
}
}
else
{
printf("Enter Nmax\n");
scanf_s("%d", &Nmax);
for (i = 1; i <= Nmax; i++)
{
switch (function)
{
case 1:
Taylor(x, i, exp(x), tochhonst, First_exp, Next_exp);
break;
case 2:
Taylor(x, i, sin(x), tochhonst, First_sin, Next_sin);
break;
case 3:
Taylor(x, i, cos(x), tochhonst, First_cos, Next_cos);
break;
case 4:
Taylor(x, i, cosh(x), tochhonst, First_cosh, Next_cosh);
break;
}
}
}
}
return 0;
}