Question

The cosine function can be evaluated by the following infinite series as (where the angle x is given in radians) cos x =1--+ + 2! 4! 6! Create a second function M-file mycos that takes the angle x (in radians), and returns cos(x) with an absolute iterative error less than 1.0e-18. Test your function to find cosine of ?/2 and 2? Display the Expansion order, and the actual error (absolute error, not the relative one) and iterative error using fprintf statement. The true or actual value can be calculated from MATLABs built-in function cos (). Apply a proper header to your display. A typical example is:

0 0
Add a comment Improve this question Transcribed image text
Answer #1

function result = My_Cos(x)
    err = 10^(-18);
    total = 1;
    sign = -1;
    i = 2;
    while true
        val = (sign * x^i / factorial(i));
        if abs(val) < err
            break;
        end
        total = total + val;
        i = i + 2;
        sign = sign * -1;
    end
  
    result = total;
end

octave:2 cos (pi/2) ans6.1232e-17 octave:3> cos (2*pi) ans1

Add a comment
Know the answer?
Add Answer to:
The cosine function can be evaluated by the following infinite series as (where the angle x...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please show exactly how it is in MATLAB The cosine function can be evaluated by the...

    Please show exactly how it is in MATLAB The cosine function can be evaluated by the following infinite series: cos ? = 1 − ?^2/ 2! + ?^4/ 4! − ?^6 /6! + ⋯ Create an M-file to compute cos(1.2345 rad) for up to and including eight terms, which is up to the term x^14/14!. a) Your program should compute and display the values of cos ? as each term in the series is added, e.g. cos ? = 1...

  • 7. (a) Use the well known Maclaurin series expansion for the cosine function: f (x )...

    7. (a) Use the well known Maclaurin series expansion for the cosine function: f (x ) = cos x = 1 x? 2! + 4! х 6! + (-1)" (2n)! . * 8! 0 and a substitution to obtain the Maclaurin series expansion for g(x) = cos (x²). Express your formula using sigma notation. (b) Use the Term-by-Term Integration Theorem to obtain an infinite series which converges to: cos(x) dx . y = cos(x²) (c) Use the remainder theorem associated...

  • 3. Cosine Approximation Write a function m-file to calculate the approximated cosine function which can be...

    3. Cosine Approximation Write a function m-file to calculate the approximated cosine function which can be evaluated by the following infinite series - equation (2): 1. x² x4 26 (-1)(n+1). 22(n-1) cos x = 1 - 3 + - 6! + + (2(n − 1))! Here are requirements for the function m-file. - Parameter lists (inputs): 2 value (in radian), and desired error percentagel. - Return value (outputs): approximated value, real error percentage, and number of iterations - Function name:...

  • Write a regular function (i.e. in a function .m file) to calculate the series expansion of...

    Write a regular function (i.e. in a function .m file) to calculate the series expansion of cosine(x). The number of terms calculated in the series should be specified in the input list of the function. Write a separate function that determines when the series expansion begins to deviate by at least 10% from the true value of cosine(x) based on the number of terms calculated in the series expansion, n. For n = 1, 2, … 10, determine the values...

  • below. Then find the corresponding Fourier cosine series t. Wite and ikcrch the even externsion ) for the function b f(x). 2 for 1s x<2 B. If fu) is approximated by the finse three terms of th...

    below. Then find the corresponding Fourier cosine series t. Wite and ikcrch the even externsion ) for the function b f(x). 2 for 1s x<2 B. If fu) is approximated by the finse three terms of the Fourier cosine espansion series, determine the error percent in C. Determine the frequency, magnitude and phase angle of the 3d harmonic of the expansion. below. Then find the corresponding Fourier cosine series t. Wite and ikcrch the even externsion ) for the function...

  • Convince yourself that the Maclaurin Series for cos(x) is: A. Write a function script called cos...

    Convince yourself that the Maclaurin Series for cos(x) is: A. Write a function script called cos_series that takes that takes as its inputs, x and N and has output given by the sum in the N-term Maclaurin Series approximation for Cos(x). Hint: try a “for loop” and set “format long” in your code. You may use the MATLAB built-in function factorial() B. Check your code by finding the 2-terms, 3-terms, 4-terms, 5-terms and 6-terms Maclaurin Series approximations every 30 degrees...

  • Question 4. Calculate the Fourier sine series and the Fourier cosine series of the function f(x)...

    Question 4. Calculate the Fourier sine series and the Fourier cosine series of the function f(x) = sin(x) on the interval [0, 1]. Hint: For the cosine series, it is easiest to use the complex exponential version of Fourier series. Question 5. Solve the following boundary value problem: Ut – 3Uzx = 0, u(0,t) = u(2,t) = 0, u(x,0) = –2? + 22 Question 6. Solve the following boundary value problem: Ut – Uxx = 0, Uz(-7,t) = uz (77,t)...

  • Programming Assignment 3 CSCI 251, Fall 2015 Infinite Series Trigonometric and math functions are...

    Programming Assignment 3 CSCI 251, Fall 2015 Infinite Series Trigonometric and math functions are usually calculated on computers using truncated Taylor series (an infinite series). An infinite series is an infinite set of terms whose sum is a particular function or expression. For example, the infinite series used to evaluate the natural log of a number is (x - 1)2 (x-1)3 (x-1)* (x-1)5 2 4 where x E (0, 2], or 0

  • In MATLAB The value of cos(x) can be approximated using a Maclaurin series + +... cos(x)=1-1...

    In MATLAB The value of cos(x) can be approximated using a Maclaurin series + +... cos(x)=1-1 2! 4! 6! Which can be expressed compactly as cos(x) = {(-1)+7 (2(k-1))! 00 2(k-1) k-1 Write Matlab code using a while loop that calculates cos(2) with 5 terms of Maclaurin series. Compare the value with the one calculated with a built-in function cos (2) in Matlab. The following is an expected output from your code: Using the Maclaurin series cos( 2.0) with 5...

  • Write in Python 3 Write a program that takes the value of an angle x in...

    Write in Python 3 Write a program that takes the value of an angle x in degrees, such that 0 s x <90, and calculates approximate value of sine of the given angel using the following series sin(x)-x-for all x The series given above has infinite number of terms. However, you can only include a finite number of terms in the series. The absolute value of each subsequent term is smaller than that of the term calculated before it. The...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT