Question

In: Computer Science

The cosine of an angle can be computed from the following infinite series: cosx=1-(x^2/2!)+(x^4/4!)-(x^6/6!)+...... Write a...

The cosine of an angle can be computed from the following infinite series: cosx=1-(x^2/2!)+(x^4/4!)-(x^6/6!)+......

Write a program in C++ that reads an angle x (in radians) from the keyboard. Then, in a function compute the cosine of the angle using first five terms of the series. Print the value computed along with the value of the cosine computed using the C++ library function.

Solutions

Expert Solution

#include <iostream>

#include <math.h>

using namespace std;

float calculatedCos(float x){

float accuracy = 0.0001;

//calculate to radians from degrees

x = x * (3.142 / 180.0);

float cosval = cos(x);

float x1,cosx,denominator;

x1 = 1;

cosx = x1;

int i = 1;

do

{

denominator = 2 * i * (2 * i - 1);

x1 = -x1 * x * x / denominator;

cosx = cosx + x1;

i = i + 1;

} while (accuracy <= fabs(cosval - cosx));

return cosx;

}

int main() {

float x;

cout<<"x: ";

cin>>x;

cout<<"\nCalculated cos("<<x<<") : "<<calculatedCos(60)<<endl;

float x60radians = 60*(3.14/180.0);

cout <<"\n Actual cos("<<x<<") : "<<cos(x60radians)<<endl;

}


Related Solutions

Recall that the following Taylor series is used to approximate Cosine: cos(x) = ∑ (−1) nx...
Recall that the following Taylor series is used to approximate Cosine: cos(x) = ∑ (−1) nx 2n (2n)! ∞ n=0 You have been tasked with developing an m-file that allows other engineers to quickly determine the minimum n needed to reduce the truncation error below an error threshold. The truncation error is the absolute error between the approximation and MATLAB’s cos() function. You may want to Google the Taylor series to better your understanding of it. Your code should perform...
1. a. Determine the angle (in radians) between the vectors 〈4, −5, 6〉 and 〈−2, 2,...
1. a. Determine the angle (in radians) between the vectors 〈4, −5, 6〉 and 〈−2, 2, 3〉. b. Find the vector projection of 〈1, 2, 4〉 onto 〈1, 1, 1〉. c. Compute the cross product 〈2, 3, 4〉 × 〈1, 0, −1〉.
Fill in the following function to sum the series of 1 + x/1 + x^2/2 +...
Fill in the following function to sum the series of 1 + x/1 + x^2/2 + x^3/3 + .. + x^n/n using Java Program
Expand in Fourier series: f(x) = x|x|, -L<x<L, L>0 f(x) = cosx(sinx)^2 , -pi<x<pi f(x) =...
Expand in Fourier series: f(x) = x|x|, -L<x<L, L>0 f(x) = cosx(sinx)^2 , -pi<x<pi f(x) = (sinx)^3, -pi<x<pi
Solve the ODE y"+3y'+2y=(cosx)+(x^2)+(e^-1)
Solve the ODE y"+3y'+2y=(cosx)+(x^2)+(e^-1)
2. Consider the following data: x= 1, 2, 3, 4, 5 y =3, 2, 4, 6,...
2. Consider the following data: x= 1, 2, 3, 4, 5 y =3, 2, 4, 6, 5 By hand, not using Matlab, and showing your work: (a) Compute the correlation coefficient. (b) Find the least-squares line. (c) Find the standard deviation around the least-squares line.
Problem 6-01 (Algorithmic) Consider the following time series data. Week 1 2 3 4 5 6...
Problem 6-01 (Algorithmic) Consider the following time series data. Week 1 2 3 4 5 6 Value 18 12 16 10 19 13 Using the naïve method (most recent value) as the forecast for the next week, compute the following measures of forecast accuracy. Mean absolute error. If required, round your answer to one decimal place. Mean squared error. If required, round your answer to one decimal place. Mean absolute percentage error. If required, round your intermediate calculations and final...
Solve STEP BY STEP using power series about x = -1, 4(x+1)^2 y'' - 2(x+1)(x+3) y'...
Solve STEP BY STEP using power series about x = -1, 4(x+1)^2 y'' - 2(x+1)(x+3) y' + (x+4) y = 0. note, solve by power series, variable transformation by letting: w= x+1 = 0
Consider the following time series data. Quarter Year 1 Year 2 Year 3 1 4 6...
Consider the following time series data. Quarter Year 1 Year 2 Year 3 1 4 6 7 2 0 1 4 3 3 5 6 4 5 7 8 (b) Use a multiple regression model with dummy variables as follows to develop an equation to account for seasonal effects in the data. Qtr1 = 1 if Quarter 1, 0 otherwise; Qtr2 = 1 if Quarter 2, 0 otherwise; Qtr3 = 1 if Quarter 3, 0 otherwise. If required, round your...
Consider the following time series data. Quarter Year 1 Year 2 Year 3 1 4 6...
Consider the following time series data. Quarter Year 1 Year 2 Year 3 1 4 6 7 2 2 3 6 3 3 5 6 4 5 7 8 Compute seasonal indexes and adjusted seasonal indexes for the four quarters (to 3 decimals). Quarter Seasonal Index Adjusted Seasonal Index 1 (___) (___) 2 (___) (___) 3 (___) (___) 4 (___) (___) Total (___) Consider the following time series data. Quarter Year 1 Year 2 Year 3 1 5 5 6...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT