The Taylor series expansion for sin(x) is sin(x) =x-33 + where is in radians Wri
ID: 3168293 • Letter: T
Question
The Taylor series expansion for sin(x) is sin(x) =x-33 + where is in radians Write a MATLAB program that determines sin G n-0 (2n+1)! program that determines sin(x) using the Taylor series +...-000 ( 1)_x2n+1 using the Taylor series expansion. The program expansion. Ihe program asks the user to type a value for an angle in degrees. Then the program uses a while loop for adding the terms of the Taylor series. If an n is the nth term in the series, then the sum Sney of the n terms is S ew-Sold + In each pass calculate the estimated error E given by E = new Sold Sold he lde a stop adding terms when S1E8The program displays the value of sin(x). Use the program for calculating sin(45") and sin (195") Show the results as: The sin of is providing an error equal toUse scientific notation with 2 decimal plaes Problem 6 Write a MAILAB program for problem 5 using a for loop. Run the program for 5, 10, and 15 steps. Display the errorat the end of the calculations. Show the results as: The sin ofisafer iterations, proniding an error equal toUse zrentific otation with 2 decimal places.Explanation / Answer
code.m
angle = 45;
[result, E] = taylor_sine(angle);
sprintf('The sin of %f is %f, providing an error equal to %2.0f.', result, E);
angle = 195;
[result, E] = taylor_sine(angle);
sprintf('The sin of %f is %f, providing an error equal to %2.0f.', result, E);
angle = input('Enter x (in degrees):');
[result, E] = taylor_sine(angle);
sprintf('The sin of %f is %f, providing an error equal to %2.0f.', result, E);
angle = input('Enter x (in degrees):');
taylor_sine_for(angle);
taylor_sine.m
function [result, E] = taylor_sine(angle)
angle = angle*pi/180;
prev_result = 0;
n = 0;
while true
result = prev_result + (((-1)^n)/(factorial(2*n+1)))*angle^(2*n+1);
E = abs((result - prev_result)/prev_result);
if E <= 1e-8
break
end
end
end
talylor_sine_for:
function [] = taylor_sine_for(angle)
angle = angle*pi/180;
prev_result = 0;
n = 0;
for i=1:15
result = prev_result + (((-1)^n)/(factorial(2*n+1)))*angle^(2*n+1);
E = abs((result - prev_result)/prev_result);
if rem(i,5) == 0
sprintf('The sin of %f if %f after %d iterations, providing an error equal to %2.0f.', angle*180/pi, result, i, E);
end
if i == 15
break;
end
end
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.