An alternative way for calculating sin(x) is to use its Taylor series as the fol
ID: 3879975 • Letter: A
Question
An alternative way for calculating sin(x) is to use its Taylor series as the following: sinx)x-+ Create a function named "sin_taylor" in MATLAB. This function takes two inputs. First input is the angle, and the second input determines the number of terms in Taylor series for approximation. Check the fidelity of your function by running sin-taylor( 7) and compare it with the exact value of it. Hint: “factorial" is a built-in function that you can use for calculating factorial of an integer number. Use MATLAB documentation to find more about it. 3! 5!7!Explanation / Answer
---------------------------------------------sin_taylor.m-----------------------------------------
function [ans] = sin_taylor(x, n)
ans = 0;
for i = 1 : n
% if the term is an even term
if mod(i, 2) == 0
ans = ans - ( x ^ ( 2 * i - 1 ) ) / factorial( 2 * i - 1 );
% if the term is an odd term
else
ans = ans + ( x ^ ( 2 * i - 1 ) ) / factorial( 2 * i - 1 );
end
end
end
-----------------------------------------main.m---------------------------------------------
sin_x = sin_taylor(pi / 6 , 7);
disp(sin_x);
Sample Output:
0.5000
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.