Preliminary C Program According to Taylor series expansion a

Preliminary C++ Program According to Taylor series expansion at x = 0, we have and sin(x) = x - x^3/3! + x^5/5! - x^7/7! + x^9/9! - x^11/11! + ... sigma^infinity_k=0 (-1)^k x^2k+1/(2k + 1)! and cos(x) = 1 - x^2/2! + x^4/4! - x^6/6! + x^8/8! - x^10/10! + ... sigma^infinity_k=0 (-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: Show well-documented and correct C++ program, and run output to verity that your program work as intended.

Solution

#include <iostream>
using namespace std;

#define pi 3.14159265359   //best value of pi I got
#define iter 10     //total terms of taylor series to be used

//calculates sin
double sin( double y ){
   double x = y;
   if( x < 0 ){ x*= -1; }
   x = x - 2*pi*((int)(x/(2*pi)));
   if( y < 0 ){ y = -x; }
   else{ y = x;}

   double result = x;
   double last = x;
   for(int i = 1; i < iter; i++ ){
       last*= -1;
       last*= (x*x);
       last/= (2*i)*(2*i + 1);
       result += last;
   }
   return result;
}

//calculates cos
double cos( double x ){
   if( x < 0 ){ x*= -1; }
   x = x - 2*pi*((int)(x/(2*pi)));
   double result = 1;
   double last = 1;
   for(int i = 1; i < iter; i++ ){
       last*= -1;
       last*= (x*x);
       last/= (2*i)*(2*i - 1);
       result += last;
   }
   return result;
}

void f( double x ){
   cout << \"n\\t\\t5sin(n)\\tcos(4000pi*n + pi/3)\\tf(n)\" << endl;
   cout << \"-----------------------------------------------\" << endl;
   double a= 5*sin(x);
   double b= cos( 4000*pi*x + pi/3 );
   cout << x << \"\\t\" << a << \"\\t\" << b << \"\\t\" << a+b << endl;
}


int main(){
   f( pi/6 );
}

 Preliminary C++ Program According to Taylor series expansion at x = 0, we have and sin(x) = x - x^3/3! + x^5/5! - x^7/7! + x^9/9! - x^11/11! + ... sigma^infini

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site