Question

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

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

PLEASE REFER BELOW INSTRUCTIONS

1) Create Approx_Cos_yourEmailAccount.m and paste below code

function [approx_value,real_per_error,iterations] = Approx_Cos_yourEmailAccount(x,desired_per_error)
    approx_value = 0.0;
    real_per_error = 0.0;
    iterations = 0;
    sign = 1;
    orig_cos_val = cos(x);
    power = 0;
    while true
        approx_value = approx_value + ((sign * x^power) / factorial(power));
        iterations = iterations + 1;
        real_per_error = abs(approx_value-orig_cos_val) * 100;
        sign = -1 * sign;
        power = power + 2;
      
        if( real_per_error <= desired_per_error)
            break;
        end
    end
end

2) create test.m and paste below code

close all
clear all
clc

x = pi / 12;
desired_per_error = 1;

fileID = fopen('EX1_11.dat','w');
[approx_value,real_per_error,iterations] = Approx_Cos_yourEmailAccount(x,desired_per_error)
test=[x desired_per_error];
fprintf(fileID,'%f %f\n',test.');
test = [approx_value real_per_error iterations];
fprintf(fileID,'%f %f %f\n',test.');
fclose(fileID);

x = pi / 12;
desired_per_error =0.1;

fileID = fopen('EX1_12.dat','w');
[approx_value,real_per_error,iterations] = Approx_Cos_yourEmailAccount(x,desired_per_error)
test=[x desired_per_error];
fprintf(fileID,'%f %f\n',test.');
test = [approx_value real_per_error iterations];
fprintf(fileID,'%f %f %f\n',test.');
fclose(fileID);

x = pi / 6;
desired_per_error = 1;

fileID = fopen('EX1_13.dat','w');
[approx_value,real_per_error,iterations] = Approx_Cos_yourEmailAccount(x,desired_per_error)
test=[x desired_per_error];
fprintf(fileID,'%f %f\n',test.');
test = [approx_value real_per_error iterations];
fprintf(fileID,'%f %f %f\n',test.');
fclose(fileID);

x = pi / 6;
desired_per_error = 0.1;

fileID = fopen('EX1_14.dat','w');
[approx_value,real_per_error,iterations] = Approx_Cos_yourEmailAccount(x,desired_per_error)
test=[x desired_per_error];
fprintf(fileID,'%f %f\n',test.');
test = [approx_value real_per_error iterations];
fprintf(fileID,'%f %f %f\n',test.');
fclose(fileID);

x = pi / 6;
desired_per_error = 0.01;

fileID = fopen('EX1_15.dat','w');
[approx_value,real_per_error,iterations] = Approx_Cos_yourEmailAccount(x,desired_per_error)
test=[x desired_per_error];
fprintf(fileID,'%f %f\n',test.');
test = [approx_value real_per_error iterations];
fprintf(fileID,'%f %f %f\n',test.');
fclose(fileID);

output will be .dat files and below


approx_value =

    0.9657


real_per_error =

    0.0195


iterations =

     2


approx_value =

    0.9657


real_per_error =

    0.0195


iterations =

     2


approx_value =

    0.8629


real_per_error =

    0.3103


iterations =

     2


approx_value =

    0.8661


real_per_error =

    0.0028


iterations =

     3


approx_value =

    0.8661


real_per_error =

    0.0028


iterations =

     3

>>

Add a comment
Know the answer?
Add Answer to:
3. Cosine Approximation Write a function m-file to calculate the approximated cosine function which can be...
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
  • The cosine function can be evaluated by the following infinite series as (where the angle x...

    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...

  • 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...

  • 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...

  • The cosine function is analytically defined as follows: m 1. x² + x6 (-1)" x2m COS...

    The cosine function is analytically defined as follows: m 1. x² + x6 (-1)" x2m COS X = (-1)" x2n (2n)! 2!*4!- 6 + ... + (2m)! Write a complete C program that has three functions: main, cosine, and factorial. The cosine function receives a real number x and returns a real number representing the cosine of x. The cosine function also receives another integer m that determines the number of terms that will be used in computing the cosine...

  • You are given the following function () 6a +11 6.1 You are to find the roots of this function using the secant method with z 2.5-and zi = 3.5 1.(6 points) Develop a function m-file that calculate...

    You are given the following function () 6a +11 6.1 You are to find the roots of this function using the secant method with z 2.5-and zi = 3.5 1.(6 points) Develop a function m-file that calculates the root of the above function using the secant method. The function should have the following five inputs only. 1. (2 points) The equation or function whose roots need to be found 2.(2 points) Initial guess -1 3. (2 points) Initial guess A4.(2...

  • Create a function m-file for the following expression which has two inputs X and Y. Call...

    Create a function m-file for the following expression which has two inputs X and Y. Call it “zoom”. Z = eVX2+Y2 /(X + Y) This function (zoom) can be in a separate script or as the last cell in your main m-file (which includes all of your solutions). Use Zoom to generate values for Z for X and Y values varying from 1 to 4 with an increment of 0.2. Also generate a table that includes all values for X...

  • 1. Write a short MATLAB script or function (m-file) that generates the functions (t)eu(t) for a =...

    Only calculate manually 1. Write a short MATLAB script or function (m-file) that generates the functions (t)eu(t) for a = 0.1. Plot the value of x(t). Since the e-af almost attenuates to 0 after 5T, where T1. So make your time vector from 0 to 5T would be enough to capture the signal.) 2. Using the definition of the FT (1), manually calculate the FT of the given functions in previous exercise 0O x(t)e 3. Plot the absolute values and...

  • PLEASE HELP, MATLAB, answers are very appreciated need to study pl 2. Write two m-file functions...

    PLEASE HELP, MATLAB, answers are very appreciated need to study pl 2. Write two m-file functions that will allow you to compare mortgages cakculations for the monthly payment and compare mortgage with different loan values. The monthly payment M, is calculated using 1-2)1 Where Pis the principal loan amount, r is the interest rate (in decimal form not percent form), and y is the number of years of the loan. Create an m-file function called "mortgage_a.m" that will serve as...

  • Problem 12.12 Pls show the m-file Develop your own M-file function for the Gauss-Seidel method without...

    Problem 12.12 Pls show the m-file Develop your own M-file function for the Gauss-Seidel method without relaxation based on Fig. 12.2, but change the first line so that it returns the approximate error and the number of iterations: function [x, ea, iter] = ... GaussSeidel (, b, es, maxit) Test it by duplicating Example 12.1 and then use it to solve Prob. 12.2a. Develop your own M-file function for Gauss-Seidel with relaxation. Here is the function's first line: function [x,...

  • (a) (4 points) Fill in the blanks in the following MATLAB function M file trap so...

    (a) (4 points) Fill in the blanks in the following MATLAB function M file trap so that it implements the composilu trapezidul rulo, using a soquence of w -1,2, 4, 8, 16,... trapezoids, to approximatel d . This M file uses the MATLAB built-in function trapz. If 401) and y=() f(!)..... fr. 1)). 3= ($1, then the execution of z = trapz(x, y) approximates using the composite trapezoidal rule (with trapezoids). (Note that the entries of are labeled starting at...

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
ADVERTISEMENT