The equation to compute sin(x)using infinite series is given below: sin(x) = sig
ID: 3935271 • Letter: T
Question
The equation to compute sin(x)using infinite series is given below: sin(x) = sigma_n = 0^infinity T_n = sigma_n = 0^infinity (-1)^n times 2^n + 1/(2n + 1)! = x - x^3/3! + x^5/5!-... Note that the value of x is given in radians. Depending on the accuracy required, we can compute each term and keep adding it to the partial sum of infinite series until the next term is negligibly small value denoted by epsilon (eps), say less than 0.000001. Write a program to calculate the value of sin(x) using the above infinite series. The program prompts the user to enter the values of x and eps. It then calculates the value of sin(x) and prints it out on the screen. Test your program for the following values (assume eps = 0.000001): x = 0.785398, x = 6.083185, x = 4.112389, x = 7.225665Explanation / Answer
#include<iostream>
#include<cmath>
using namespace std;
long long int fact(long long int n){
if(n==1)
return 1;
else
return n*fact(n-1);
}
int main(){
double e=0.000001,sum=0.0,small=0.16,x;
cin>>x;
long long int n=1,i=0;
while(abs(small)>e){
if(i%2==0)
sum+=pow(x,n)/fact(n);
else sum-=pow(x,n)/fact(n);
i++;
small=pow(-1,i%2)*pow(x,n)/fact(n);
n=n+2;
}
cout<<sum;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.