The sine function can be evaluated via the following infinit

The sine function can be evaluated via the following infinite series: sin(x) = x - x^3/3! + x^5/5! - x^7/7! + ... Write a function approxSine that takes as input two parameters, x and threshold, to perform the following task. Starting with the initial approximation sin(x) = x, add terms one at a time to improve this estimate until Assume that the true value of sine is the one returned by the built-in sin function in MATLAB. Your function must return two output values: the approximate value of sin(x) and the number of terms that were needed to obtain this value subject to the desired error threshold. The following is an example of function behavior for x = pi/5. >> x = pi/5; >> true_value = sin(x) real = 0.5878 >> threshold = 0.001; >> [approx terms] = approxSine(x, threshold) approx = 0.5878 terms = 2 >> threshold = 0.00001; >> [approx terms] = approxSine (x, threshold) approx = 0.5878 terms = 3

Solution

Ans:

function result = approx_sine(xrad, threshold)
%sin(x) = x – (x^3/3!) + (x^5/5!) – (x^7/7!) + · · ·
    sinsum = xrad;
    i = 3
    terms = 1
    realval = sin(xrad)
    prevsign = 1
    approx = (realval - sinsum)/realval
    % if(approx < 0)
    %     approx = approx * -1
    % end
    while approx < threshold
        prevsign = prevsign * -1
        sinsum = sinsum + ( prevsign * (power(xrad, i) / factorial(i)))
        i = i + 2
        approx = (realval - sinsum)/realval
        % if(approx < 0)
        %     approx = approx * -1
        % end
      
    end
     terms = (i+1)/2
     result = [sinsum terms]
end

x = pi/5;

threshold = 0.001;

[approx terms] = approx_sine(x, threshold)

threshold = 0.00001;

[approx terms] = approx_sine(x, threshold)

 The sine function can be evaluated via the following infinite series: sin(x) = x - x^3/3! + x^5/5! - x^7/7! + ... Write a function approxSine that takes as inp

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site