Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************
- Online C++ Compiler.
- Code, Compile, Run and Debug C++ program online.
- Write your code in this editor and press "Run" button to compile and execute it.
- *******************************************************************************/
- #include <iostream>
- #include <cmath>
- using namespace std;
- //fact
- double fact(int n) {
- if (n == 0) return 1;
- return n * fact(n - 1);
- }
- //taylor series for sin(x)
- double myfunc(double x) {
- double sum = 0;
- double previous_result;
- for (int i = 0; i < 10; i++) {
- double result = pow(-1, i) * pow(x, 2 * i + 1) / fact(2 * i + 1);
- if(abs(result - previous_result) < 1e-15) {
- break;
- }
- previous_result = pow(-1, i) * pow(x, 2 * i + 1) / fact(2 * i + 1);
- sum += previous_result;
- }
- return sum;
- }
- int main()
- {
- for(double i=0;i<6.14;i+=0.1){
- cout<<i<<" "<< myfunc(i)<<" "<<sin(i)<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement