Question

In: Computer Science

Write an overloaded procedure using c++ for approximating the value of cos(x), called approx_cos. This procedure...

Write an overloaded procedure using c++ for approximating the value of cos(x), called approx_cos. This procedure should work as follows:

  • A call such as approx_cos(double x, long N) should produce the Nth order Taylor approximation of the cosine function at the point x. You of course need the derive this Taylor expansion in order to implement it.

  • A call such as approx_cos(double x) should approximate the value of the cosine function evaluated at the point x, to at least eight digits of accuracy. The Taylor Remainder Theorem helps us understand how to mathematically obtain such a result.

Of course you should write a main() procedure that checks the validity of this procedure and compare it to the true value of cosine at several points (not just x=0).

Solutions

Expert Solution

#include <iostream>
#include <cmath>

using namespace std;

double approx_cos(double x) {
        double lastResult = 0;
        double result = 1;
        double lastTerm = 1;
        int i=1;
        while(abs(result - lastResult) > pow(10, -6)) {
                lastResult = result;
                lastTerm = -1 * lastTerm * (x * x)/(2*i * (2*i - 1));
                result += lastTerm;
                i++;
        }

        return result;
}

double cosine(double x, long n) {

        double result = 1;
        double lastTerm = 1;
        for(int i=1; i<=n; i++) {
                lastTerm = -1 * lastTerm * (x * x)/(2*i * (2*i - 1));
                result += lastTerm;
        }

        return result;
}

int main() {

        cout << cos(M_PI/8.0) << endl;
        cout << cosine(M_PI/8.0, 100) << endl;
        cout << approx_cos(M_PI/8.0) << endl;

}
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

C++ Overloaded Hospital Write a program that computes and displays the charges for a patient’s hospital...
C++ Overloaded Hospital Write a program that computes and displays the charges for a patient’s hospital stay. First, the program should ask if the patient was admitted as an in-patient or an out-patient. Please keep asking the user until the user enters the valid choice. Please refer to the test cases for more details. If the patient was an in-patient the following data should be entered: • The number of days spent in the hospital • The daily rate •...
Write the Taylor series for f(x) = cos x about x = 0.
P1. Write the Taylor series for f(x) = cos x about x = 0. State the Taylor polynomials T2(x), T4(x), and T6(x) (note that T3(x) will be the same as T2(x), and T5(x) will be the same as T4(x)). Plot f(x), T2(x), T4(x), and T6(x), together on one graph, using demos or similar (cut-and-paste or reproduce below).
Using basic C++( without using <Rectangle.h> ) Write the definition for a class called Rectangle that...
Using basic C++( without using <Rectangle.h> ) Write the definition for a class called Rectangle that has floating point data members length and width. The class has the following member functions: void setlength(float) to set the length data member void setwidth(float) to set the width data member float perimeter() to calculate and return the perimeter of the rectangle float area() to calculate and return the area of the rectangle void show() to display the length and width of the rectangle...
Estimate the area under the graph of f(x)=25−x^2 from x=0 to x=5 using 5 approximating rectangles...
Estimate the area under the graph of f(x)=25−x^2 from x=0 to x=5 using 5 approximating rectangles and right endpoints. (B) Repeat part (A) using left endpoints. (C) Repeat part (A) using midpoints.
write a java merge sort called MERGE-SORT-A(), Using recursive calls and NO INSERTION-SORT() as a sub-procedure.
write a java merge sort called MERGE-SORT-A(), Using recursive calls and NO INSERTION-SORT() as a sub-procedure.
This code is to be written in Matlab. Write a function that will plot cos(x) for...
This code is to be written in Matlab. Write a function that will plot cos(x) for x values ranging from -pi to pi in steps of 0.1, using black *'s. It will do this three times across in one Figure Window, with varying line widths. If no arguments are passed to the function, the line widths will be 1, 2, and 3. If on the other hand, an argument is passed to the function, it is multiplier for these values....
Give the domains and ranges of the following functions. A) ln x, B) cos X, C)...
Give the domains and ranges of the following functions. A) ln x, B) cos X, C) 10^x D) f(x)= 1/ x^2-12x-45 E) g(x)= sqrt(82-x).
Find dy/dx for a & b a) sin x+cos y=1 b) cos x^2 = xe^y c)Let...
Find dy/dx for a & b a) sin x+cos y=1 b) cos x^2 = xe^y c)Let f(x) = 5 /2 x^2 − e^x . Find the value of x for which the second derivative f'' (x) equals zero. d) For what value of the constant c is the function f continuous on (−∞,∞)? f(x) = {cx^2 + 2x, x < 2 ,   2x + 4, x ≥ 2}
Given that Csc(x) = - 3 and Cos(x) < 0, find the exact value of each...
Given that Csc(x) = - 3 and Cos(x) < 0, find the exact value of each of the trigonometric function of x. Functions are: a. Sin(x) , b. Cos(x) , c. Tan(x) , d. Csc(x) , e. Sec(x) , f. Cot(x)
Estimate the area under the graph of f(x)=1/(x+4) over the interval [-1,2] using five approximating rectangles...
Estimate the area under the graph of f(x)=1/(x+4) over the interval [-1,2] using five approximating rectangles and right endpoints. Rn= Repeat the approximation using left endpoints. Ln=
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT