Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

a) Write a Matlab script file to calculate sin(x) and cos(x), which are defined

ID: 3808268 • Letter: A

Question

a) Write a Matlab script file to calculate sin(x) and cos(x), which are defined as follows: sin (x) = x - x^3/3! + x^5/5! - x^7/7! + ... cos (x) = 1 - x^2/2! + x^4/4! - x^6/6! + ...Here n! is 'n factorial': n! identical to n middot (n - 1) middot (n - 2)... 3 middot 2 middot 1. Note that the argument x is in radians. NOT degrees! Your program should have the following features: Prompt user for x and the number of terms to include in the summation (input the values of x and n from command window) Do NOT calculate n! for each term. Recognize that for each successive tem, n! for that tem is n! of the previous term times (n + 1) (n + 2) Similarly do NOT calculate x^n for each term. Recognize that each successive term for n is x from the previous term multiplied by x^2.i.e., x^n + 2 = x^n x^2 Tips: Manually calculate the first several terms to verify your program is working correctly Learn to use the debugger in Matlab. It is an invaluable tool. Use the 'format long' command to express your results with many digits b) use your program to compute the following: sin (0.01) and cos (0.01) using 80 terms sin (0.5) and sin(0.5) using 80 terms sin(10) and cos(10)using 80 terms c)how many terms are required to computer each of the following to 10 digits of accuracy (tip: use the sin() and cos() functions in MATLAB to get the true values) sin (0.5) sin(5) sin(20) d) can you calculate sin(100)? can you calculate sin(0.01) using 200 terms? If not, what do you think is happening?

Explanation / Answer


%%%%%%%%%%%%%%%%%%%%% PART-A&B %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function result = findSine(x, n)
    result = 0.0;
    numerator = x;
    denominator = 1.0;
    for i=1:n
        if mod(i,2)==0
            result = result - (numerator/denominator);
        else
            result = result + (numerator/denominator);      
        end
        term = (2*i) - 1;
        numerator = numerator * (x*x);
        denominator = denominator * (term+1) * (term+2);
    end
end


function result = findCos(x, n)
    result = 0.0;
    numerator = 1.0;
    denominator = 1.0;
    for i=1:n
        if mod(i,2)==0
            result = result - (numerator/denominator);
        else
            result = result + (numerator/denominator);      
        end
        term = 2*(i-1);
        numerator = numerator * (x*x);
        denominator = denominator * (term+1) * (term+2);
    end
end


printf("sin(0.01) using 80 terms is %f ", findSine(0.01, 80));
printf("cos(0.01) using 80 terms is %f ", findCos(0.01, 80));
printf("sin(0.50) using 80 terms is %f ", findSine(0.5, 80));
printf("cos(0.50) using 80 terms is %f ", findCos(0.5, 80));
printf("sin(10.0) using 80 terms is %f ", findSine(10, 80));
printf("cos(10.0) using 80 terms is %f ", findCos(10, 80));


%%%%%%%%%%%%%%%%%%%%% PART-C %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


function trails = findTrailsForSine(x)
    actualSine = sin(x);
  
    numerator = x;
    denominator = 1.0;
    result = 0.0;
    trails=0;
  
    while (abs(actualSine-result) > 0.00000000001)
        trails = trails + 1;
        if mod(trails,2)==0
            result = result - (numerator/denominator);
        else
            result = result + (numerator/denominator);      
        end
        term = (2*trails) - 1;
        numerator = numerator * (x*x);
        denominator = denominator * (term+1) * (term+2);
    end
end


function trails = findTrailsForCos(x)
    actualCos = cos(x);
  
    numerator = 1.0;
    denominator = 1.0;
    result = 0.0;
    trails=0;
  
    while (abs(actualCos-result) > 0.00000000001)
        trails = trails + 1;
        if mod(trails,2)==0
            result = result - (numerator/denominator);
        else
            result = result + (numerator/denominator);
        end
        term = 2*(i-1);
        numerator = numerator * (x*x);
        denominator = denominator * (term+1) * (term+2);
    end
end

printf(" ");
printf("sin(0.5) uses %d trails for 10-digit accuracty ", findTrailsForSine(0.5));
printf("sin(5.0) uses %d trails for 10-digit accuracty ", findTrailsForSine(5));
printf("sin(20.0) uses %d trails for 10-digit accuracty ", findTrailsForSine(20));

%%%%PART D%%%%

% we can calculate sin(0.01) using 200 terms, but we can't calculate sin(100.0)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote