home / homework help / questions and answers / engineering / computer science /
ID: 641943 • Letter: H
Question
home / homework help / questions and answers / engineering / computer science / compute cos(p/4) the last line of the derivation ...
Choose a Best Answer - it's good karma!
*Recognize the hard work of the person who helped you out.
*Increase your rating % to keep great answers coming your way.
Ask a new question?
Question
This is to be a c++ program
Compute cos(P/4)
The last line of the derivation of the Maclaurin series for cosine above shows how to compute cosine to full machine precision using a sum.
You may use a macro for PI:
# define PI 3.14159265358979323846 /* pi */
You will need to use a double values to hold your various sum and product variables. Output approximations using 4,6,8 and 10 terms of the series. Also display the absolute and relative errors in each approximation. Assuming a variable named approximation holds your approximation and a variable cosx hold the actual value, the computations would be: absolute_error = fabs(cosx-approximation) and relative_error = fabs(cosx-approximation)/cosx. Note that the abs() function in C uses an int argument and returns an int result, which is not sufficiently precise for our problem, so you must use fabs(). The correct value of cos(P/4) can be determined to full double precision using the circular function cos(P/4) defined in the #include <cmath> file
Explanation / Answer
#include <iostream>
#include<cmath>
#define PI 3.14159265358979323846
using namespace std;
double cosine(int k,double x)
{
double sum=0;
for(int i=0;i<k;i++)
{
sum+=pow(-1,i)*pow(x,2*i)/fact(2*i);
}
return sum;
}
double fact(int x)
{
double fact=1;
for(int i=1;i<=x;i++)
fact*=i;
return fact;
}
int main()
{
double approximation,absolute_error,relative_error;
double cosx=cos(PI/4);
approximation=cosine(4,PI/4);
absolute_error=fabs(cosx-approximation);
relative_error=fabs(cosx-approximation)/cosx;
cout<<"For 4 terms: "<<endl;
cout<<"Approximation: "<<approximation<<endl;
cout<<"Absolute Error: "<<absolute_error<<endl;
cout<<"Relative Error: "<<relative_error<<endl;
cout<<endl;
approximation=cosine(6,PI/4);
absolute_error=fabs(cosx-approximation);
relative_error=fabs(cosx-approximation)/cosx;
cout<<"For 6 terms: "<<endl;
cout<<"Approximation: "<<approximation<<endl;
cout<<"Absolute Error: "<<absolute_error<<endl;
cout<<"Relative Error: "<<relative_error<<endl;
cout<<endl;
approximation=cosine(8,PI/4);
absolute_error=fabs(cosx-approximation);
relative_error=fabs(cosx-approximation)/cosx;
cout<<"For 8 terms: "<<endl;
cout<<"Approximation: "<<approximation<<endl;
cout<<"Absolute Error: "<<absolute_error<<endl;
cout<<"Relative Error: "<<relative_error<<endl;
cout<<endl;
approximation=cosine(10,PI/4);
absolute_error=fabs(cosx-approximation);
relative_error=fabs(cosx-approximation)/cosx;
cout<<"For 10 terms: "<<endl;
cout<<"Approximation: "<<approximation<<endl;
cout<<"Absolute Error: "<<absolute_error<<endl;
cout<<"Relative Error: "<<relative_error<<endl;
cout<<endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.