Advertisement
Joporezka1

Untitled

Jun 20th, 2022
676
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. /******************************************************************************
  2.  
  3.                               Online C++ Compiler.
  4.                Code, Compile, Run and Debug C++ program online.
  5. Write your code in this editor and press "Run" button to compile and execute it.
  6.  
  7. *******************************************************************************/
  8.  
  9. #include <iostream>
  10. #include <cmath>
  11.  
  12. using namespace std;
  13.  
  14. //fact
  15. double fact(int n) {
  16.     if (n == 0) return 1;
  17.     return n * fact(n - 1);
  18. }
  19.  
  20. //taylor series for sin(x)
  21. double myfunc(double x) {
  22.     double sum = 0;
  23.  
  24.     double previous_result;
  25.     for (int i = 0; i < 10; i++) {
  26.         double result = pow(-1, i) * pow(x, 2 * i + 1) / fact(2 * i + 1);
  27.         if(abs(result - previous_result) < 1e-15) {
  28.             break;
  29.         }
  30.         previous_result = pow(-1, i) * pow(x, 2 * i + 1) / fact(2 * i + 1);
  31.         sum += previous_result;
  32.     }
  33.     return sum;
  34. }
  35.  
  36. int main()
  37. {
  38.  
  39.     for(double i=0;i<6.14;i+=0.1){
  40.         cout<<i<<" "<< myfunc(i)<<" "<<sin(i)<<endl;
  41.     }
  42.  
  43.     return 0;
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement