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

The series expansion for the exponential, sine, and cosine functions are as foll

ID: 3797120 • Letter: T

Question

The series expansion for the exponential, sine, and cosine functions are as follows: e^x = 1 + x + x^2/2! + x^3/3! + x^4/4! + sin x = x - x^3/3! + x^5/5! - x^7/7! + x^9/9! cos x = 1 - x^2/2! + x^4/4! - x^6/6! + x^8/8! Write a C++ program that calculates the value of the series e^x, sin x and cos x, where the user enters the value of x and the n the number of terms in the series. For example, if n = 7, the program should calculate the sum of 7 terms in the series for a given values of x. The program should use switch statements to determine the choice of the function. Draw a detailed flow chart on how this program is designed, indicating the necessary inputs, outputs and the algorithm you have used. The flow chart should be included in the document file along with your program listing (source code). Now carry out a convergence study using different values of n for a given value of x and plot the convergence graph.

Explanation / Answer

#include<iostream>
#include<cmath>

using namespace std;

//factorial function
int fact(int n)
{
return (n == 1 || n == 0) ? 1 : fact(n - 1) * n;
}

//radian function
float rad(int x)
{
float pi = 22/7;
float radian = (x * pi)/180;
return radian;
}

//exponentian function
float expo(int x, int n)
{
float sum = 0;
for(int i = 0; i < n; i++)
{
if(i == 0)
sum += 1;
else
sum += pow(x,i)/fact(i);
}
return sum;
}
//sine function
float sine(int x, int n)
{
float sum = 0;
//int y = x; //in degrees
float y = rad(x); //in radian
for(int i = 0; i < n; i++)
{
if(i%2 == 0)
sum += pow(y,(2*i)+1)/fact((2*i)+1);
else
sum -= pow(y,(2*i)+1)/fact((2*i)+1);
}
return sum;
}
//cosine function
float cosine(int x, int n)
{
float sum = 0;
//int y = x; //in degrees
float y = rad(x);
for(int i = 0; i < n; i++)
{
if(i%2 == 0)
sum += pow(y,i*2)/fact(2*i);
else
sum -= pow(y,i*2)/fact(2*i);
}
return sum;
}

int main()
{
int x, n;
float ans;
char ch;
cout << "Enter value of x: " << endl;
cin >> x;
cout << "Enter value of n: " << endl;
cin >> n;
cout << "Select e for exponential, s for sine and c for cosine function. " << endl;
cin >> ch;
switch(ch)
{
case 'e':
ans = expo(x,n);
cout << "Exponential of x: " << ans << endl;
break;
case 's':
ans = sine(x,n);
cout << "Sine of x: " << ans << endl;
break;
case 'c':
ans = cosine(x,n);
cout << "Cos of x: " << ans << endl;
break;
default:
return 0;
}
return 0;
}

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