Taylor
This commit is contained in:
commit
7750dbddb1
38
taylor.cpp
Normal file
38
taylor.cpp
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
#include<iostream>
|
||||
#include<math.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
long factorial(long N) {
|
||||
if (N < 2) return 1;
|
||||
for (int i = N - 1; i > 0; --i) {
|
||||
N = N * i;
|
||||
}
|
||||
return N;
|
||||
}
|
||||
|
||||
double sin_taylor(double x, int N) {
|
||||
double res = 0;
|
||||
for (int k = 0; k < N; ++k) {
|
||||
res += pow(-1, k) * pow(x, 1 + 2*k) / (double)factorial(1 + 2*k);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
void find_interval(int N) {
|
||||
for (double x = 0; x < 3.14; x += 0.00000001) {
|
||||
double real = sin(x);
|
||||
double taylor = sin_taylor(x, N);
|
||||
//cout << "Real: " << real << " Taylor: " << taylor << endl;
|
||||
if (abs(real - taylor) > 0.001) {
|
||||
cout << "Interval: [0, " << x - 0.01 << "]" << endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
find_interval(1);
|
||||
find_interval(2);
|
||||
find_interval(3);
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user