The value of sin(x) can be approximated as: Sin(x) = x - x^3/3! + x^5/5! - x^7/7
ID: 3855457 • 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 expression becomes more accurate when more terms are added. At some point, however, the soulution is "close enough for engineering purposes."
Create a C++ program called my_sin, using a midpoint break loop to approximate the value of sin(x). Determine convergence by comparing successive values of the summation as you add additional terms. These successive sums should be within an absolute value of 0.001 of each other.
The convergence criterion, epsilon ( * symbol looks like a backwards 3) is met when:
|sin(x)n - sin(x)n - 1| <= epsilon
where "n" is the number of terms:
Test your function by evaluating the my_sin(2 radians) and comparing it to the built-in C++ sine.
Have the program output:
1. The value of sin(2) using your code
2. The value of sin(2) using the C++ sine function
3. The number of terms needed in your summation to reach the conclusion
Note: The factorial function (!) is not included in the C++ library of functions. You must write it yourself in a user-defined function, which may be a value-returning function or a void function.
Explanation / Answer
#include<iostream>
#include<cmath>
using namespace std;
int factorial(int n) {
int product = 1;
for (int i = 1; i <= n; i++)
product = product * i;
return product;
}
int my_sine(double x) {
int count = 0;
double item, sum = 0;
while (1) {
item = pow(x, count*2 + 1)/factorial(count*2 + 1) * pow(-1, count);
sum = sum + item;
count ++;
if (abs(item) < 0.001)
break;
}
cout << "Value from my_sine: " << sum << endl;
cout << "Value from c++ sine: " << sin(x) << endl;
cout << "Number of iterations: " << count << endl;
}
int main() {
my_sine(2);
}
I hope this helps. :) let me know if any modification is needed
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.