According to Taylor series expansion at x = 0, we have sin(x) = x -x^3/3! + x^5/
ID: 3775789 • Letter: A
Question
According to Taylor series expansion at x = 0, we have sin(x) = x -x^3/3! + x^5/5! - x^7/7! + x^9/9! - x^11/11! + ... = sigma_k=0^infinity (-1)^k x^2k+1/(2 k + 1)! and cos (x) = 1 - x^2/2! + x^4/4! - x^+/6! + x^8/8! - x^10/10! + ... = sigma_k=0^infinity (-1)^k x^2k/(2k)! where x is in radian. Using these two equations, we can approximately evaluate the values of sin and cos functions for a given argument x. Write two functions g(x) = sin(x) and h(x) = cos(x) using the series above to obtain accuracy to 5 decimal places. Write a C++ program that uses the functions above to calculate f(n) for integer n = 0, 1, 2, ..., 6, where f(n) = 5g(n) * h (4000 pi n + pi/3 = 5 sin(n) * cos (4000 pi n + pi/3). The program also display the result in the following table format: n 5 sin(n) cos(4000 pi n + pi/3) f(n)Explanation / Answer
#include<iostream>
#include<cmath>
#include <iomanip>
using namespace std;
double g(double x);
double h(double x);
long int factorial(int m);
double f(int n);
const double PIE= 3.14159;
int main()
{
//x is in radians
double x;
int n;
cout<<"Enter x in radians = ";
cin>>x;
cout << setprecision(5);
//find g(x) and h(x) by calling those functions
cout<<"Value of g(x) = "<<g(x) << " and h(x) = "<<h(x)<<endl;
//find f(n)
cout<<"Enter value of n = ";
cin>>n;
cout<<"Value of f(n)= "<< f(n)<<endl;
}
double g(double x)
{
int n;
double val=0;
//for approximation to 5 places
for (n=0;n<5;n++)
{
double p = pow((long double)-1,n);
double px = pow(x,2*n+1);
long fac = factorial(2*n+1);
val += p * px / fac;
}
return val;
}
double h(double x)
{
int n;
double val=0;
//for approximation to 5 places
for (n=0;n<5;n++)
{
double p = pow((long double)-1,n);
double px = pow(x,2*n);
long fac = factorial(2*n);
val += p * px / fac;
}
return val;
}
long int factorial(int m)
{
if (m==0 || m==1) return (1);
else return (m*factorial(m-1));
}
double f(int n)
{
double value;
//f(n) = 5sin(n) * cos(4000pien+pie/3
value = 5*g(n) * cos((4000*PIE)+PIE/3);
return value;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.