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

Problem - 1: The series expansion for cos, and sin, functions and trigonometric

ID: 3873751 • Letter: P

Question

Problem - 1: The series expansion for cos, and sin, functions and trigonometric identities are as follows (x in radians): 20 Pointscos x = 1 x2 2! + x4 4! x6 6! + x8 8! ...... sinx = x x3 3! + x5 5! x7 7! + x9 9! ...... sin(2x) = 2sinx cosx cos(2x) = cos2 xsin2 x a. Write a C++ program that calculates the value of the series sinx and cosx, sin2x or cos2x where the user enters the value of x (in degrees) and n the number of terms in the series. For example, if n = 5, the program should calculate the sum of 5 terms in the series for a given values of x. The program should use switch statements to determine the choice sinx and cosx, sin2x or cos2x. (Note: you may have to convert degrees to radians for sin and cos functions). 7 Points
b. Draw a detailed ow chart on how this program is designed, indicating the necessary inputs, outputs and the algorithm you have used. The ow chart should be included in the document le along with your program listing (source code). 4 Points
c. Now carry out a convergence study for n = 1...7 for a given value of x and plot the convergence graph (you can use M.S. Excel or MATLAB to plot ). 4 Points

Explanation / Answer

Here is the code:

#include<iostream>
#include<cmath>
#define PI 3.14159265359
using namespace std;

long fact(int n) {
   int p = 1;
   for (int i = 2; i <= n; i++)
       p *= i;
   return p;
}

double sin(double x, int n) {
   int count = 1;
   double sum = x;
   int sign = -1;
   while (count < n) {
       double term = pow(x, 2*count + 1)/fact(2*count + 1) * sign;
       sign *= -1;
       count ++;
       sum += term;
   }
   return sum;
}

double cos(double x, int n) {
   int count = 1;
   double sum = 1;
   int sign = -1;
   while (count < n) {
       double term = pow(x, 2*count)/fact(2*count) * sign;
       sign *= -1;
       count ++;
       sum += term;
   }
   return sum;
}

double sin2(double x, int n) {
   return 2 * sin(x,n) * cos(x, n);
}

double cos2(double x, int n) {
   return pow(cos(x,n), 2) - pow(sin(x, n), 2);
}

int main() {
   int ch, x, n;
   cout << "1. cos x" << endl;
   cout << "2. sin x" << endl;
   cout << "3. cos 2x" << endl;
   cout << "4. sin 2x" << endl;
   cout << "Enter choice: ";

   cin >> ch;

   cout << "Enter x(in integral degrees): ";
   cin >> x;
  
   cout << "Enter n: ";
   cin >> n;

   x = x%360;
   double rad = PI/180*x;

   double result = 0;

   switch (ch) {
       case 1:
           result = cos(rad, n);
           break;
       case 2:
           result = sin(rad, n);
           break;
       case 3:
           result = cos2(rad, n);
           break;
       case 4:
           result = sin2(rad, n);
           break;
       default:
           cout << "Invalid choice";
           return 0;
   }
   cout << "Answer is: " << result;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote