Preliminary C++ Program. Given the McLaurin series sin(x) = x-x^3/(3!) + x^5/(5!
ID: 3688908 • Letter: P
Question
Preliminary C++ Program. Given the McLaurin series sin(x) = x-x^3/(3!) + x^5/(5!)-x^7/(7!) + x^9/(9!) +... where x is in radian. cos(x) = 1 - x^2/(2!) + x^4/(4!) - x^6/(6!) + x^8/(8!) +... where x is in radian. 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 to 6 where Post Lab: Well documented and correct C++ program., and run output Reference: sin(x) and cos(x) programs on the course website. as always, have the instructor verify that your program work as intended.Explanation / Answer
first:
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <cmath>
#include <iomanip>
using namespace std;
int degrees; //degrees variable
const double pi = 3.1415;
double factorialfunction (int f); //factorial function
double powerfunction(double x, int y); //power function
double sinefunction(double x); //sine series
double cosinefunction(double x); //cosine series
int main()
{
char p;
double x= 0.0;
const double pi = 3.1415;
int degrees=x*(180)/pi; //This is the conversion factor we will use now
cout << endl << " Degrees" << " Sine"<<" Cosine"<<endl;
cout << endl << " " << 0 << " "<< sine(0)<<" "<< cosine(0); //display sin and cos of 0
while (x < 0.7853) // This is the radian equivalent of 45 degrees
{
x = x + 0.0872664626; // This will be the radians equivalent of 5 degree increments
degrees=x*(180/pi);//conversion to degrees
cout << endl << " " << degrees << " "<< sinefunction(x)<<" "<< cosinefunction(x);
}
cout << endl;
cout << "Enter a character to end. ";
cin >> p;
}
//This is the factorial function:
double factorialfunction(int x)
{
double fact=1.0;
for (int i=1; i<=x; i++)
{
fact=fact*i;
}
return fact;
}
//This is the function for Power
double powerfunction (double x, int y)
{
double power1=1.0;
for (int i=1; i<=y; i++)
power1=power1*x;
return power1;
}
//This is the sinefunction to calculate the sin series.
double sinefunction (double x)
{
double sum_of_positives = 0.0;
double sum_of_negatives= 0.0;
double output = 0.0;
const double pi = 3.1415;
for (int i=1; i<=1000; i+=4)
{
sum_of_positives = sum_of_positives + (powerfunction (x,i) / factorialfunction (i));
}
for (int i=3; i<=1000; i+=4)
{
sum_of_negatives = sum_of_negatives + (powerfunction (x,i) / factorialfunction (i));
}
output = (sum_of_positives - sum_of_negatives);
return output;
return degrees=x*(180)/pi;
}
//This is the cosine function for Cosine Series
double cosinefunction (double x)
{
double sum_of_positives = 0.0;
double sum_of_negatives= 0.0;
double output=0.0;
const double pi = 3.1415;
for (int i=4; i<=1000; i+=4)
{
sum_of_positives = sum_of_positives + (powerfunction (x,i) / factorialfunction (i));
}
for (int i=2; i<=1000; i+=4)
{
sum_of_negatives = sum_of_negatives + (powerfunction (x,i) / factorialfunction (i));
}
output = (1 - (sum_of_negatives) + (sum_of_positives));
return output;
return degrees=x*(180)/pi;
}
second:
int main()
{int term1;
char p;
double x= 0.0;
const double pi = 3.1415;
int degrees=x*(180)/pi; //This is the conversion factor we will use now
cout << endl << " Degrees" << " Sine"<<" Cosine"<<endl;
for(int i=0;i<=6;i++)
{
term1=5* sinefunction(i)*cosinefunction(4000*pi*i+pi/3); calling both the functions
cout<< term1;
}