Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

the value of sin(x) can be approximated as: Sin(x)=x-x^3/3!+x^5/5!-x^7/7!+......

ID: 3855468 • Letter: T

Question

the value of sin(x) can be approximated as: Sin(x)=x-x^3/3!+x^5/5!-x^7/7!+......The expession becomes more accurate when more terms are added. At some point, however, the solution is"close enough for engineering purposes." create a c++ Program called my_sin, using midpoint break loop to approximate the value of sin(x). Determine convergence by comparing succesive values of the summation as you add additional terms. These succesive sums should be within an absolute value of 0.001 of each other. The convergence criterion,epson is met when: |sin(x).subscrpt(n)-sin(x).subscript(n+1)|<=epson, where "n" is the number of terms. Test your function by evaluating the my_sin(2radians) and comparing it to the built-in c++ sine. Have the program out put: 1) THE VALUE OF SIN(2) USING YOUR CODE. 2) THE VALUE OF SIN(2) USING C++ SINE FUNCTION. 3) THE NUMBER OF TERMS NEEDED IN YOUR SUMMATION TO REACH THE CONCLUTION. **NOTE: THE FACTORIAL FUNCTION IN SNOT INCLUDED IN THE C++ LIBRARY OF FUNCTIONS. YOU MUST WRITE IT YOURSELF IN A USER-DEFINED FUCTION, WHICH MAY BE A VALUE-RETURNING FUNCTION OR A VOID FUNCTUION

i wrote the funtion for factorials below

#include <iostream>

using namespace std;

void factorial(int n);

int main()

{

int n;

cout << "Input a number to Factorial :";

cin >> n;

factorial(n);

return 0;

}

void factorial(int n)

{

int fact=1;

for (int i=0; i<n;i++)

{

fact*=(n-i);

}

cout <<"The Factorial of " << n << " is " << fact << endl;

}

Explanation / Answer

// c++ code
#include <iostream>
#include <math.h>
#include <stdlib.h>
using namespace std;

int factorial(int n)
{
int fact=1;
for (int i=0; i<n;i++)
{
fact*=(n-i);
}
  
return fact;
}


int main()
{
double x;

cout << "sin(x): Input value of x: ";
cin >> x;

x=x*3.14159/180;
double t=x;
double seriesSum=x;
double previous = 0;
int k=2;
int count = 0;

while(true)
{
t=pow(-1,(k-1)) * pow(x,(k-1)*2+1) / factorial((k-1)*2+1);
seriesSum=seriesSum+t;
previous=previous+t;
k++;
if(abs(seriesSum - previous) <= 0.001)
break;
  
count++;
}
  
cout << "Approximate value of sin(" << x << "): " << seriesSum << endl;
cout << "Iterations: "<< count << endl;

cout << "Actual Value of sin(" << x << "): " << sin(x) << endl;
return 0;
}