The infinite Maclaurin series to calculate the sine of an angle in radians is: s
ID: 3566776 • Letter: T
Question
The infinite Maclaurin series to calculate the sine of an angle in radians is:
sin(x)=x-(x^3/3!)+(x^5/5!)-(x^7/7!)+(x^9/9!)-......
Write a function, named sine, to calculate the sine of an angle using the above Maclaurin series. Use your fact function from problem 1 in your sine function. Iterate the terms until the difference between the previous term and the current term is less than 10-5. Write a program that asks for an angle to be input, passes that angle to the sine function, and displays the result to five decimal places as:
The sine of ____ is ______.
If 0.3 is entered as the angle, the result would be:
The sine of 0.30000 is 0.29552.
Explanation / Answer
x=input('enter the angle (in rad)');
y=sine(x);
str=['the sine of ',num2str(x),' is ',num2str(y)];
disp(str)
function sine:
function [y]=sine(x)
err=1;
y1=x;
count=1;
while err>=1e-5
count=count+1;
y2=y1+(-1)^(count-1)*x^(2*count-1)/fact1(2*count-1);
err=abs(y2-y1);
y1=y2;
end
y=y1;
end
function [y]=fact1(x)
y=1;
for i=1:x
y=y*i;
end
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.