The Taylor series for the sine function is: f(x) = x - x^3/3! + x^5/5! + Write a
ID: 3883335 • Letter: T
Question
The Taylor series for the sine function is: f(x) = x - x^3/3! + x^5/5! + Write a MATLAB function called my_sine that uses the Taylor series expansion to evaluate sin(x) at a user-provided x-value, to a user-defined accuracy tol, which specifies the maximum relative true error (i.e., the relative error compared against the built-in function sin(x)). Inputs: x The x-value to evaluate. tol The tolerance to which the sin(x) function should be evaluated. Outputs: f The estimate of sin(x) n The number of terms in the series required to achieve the desired accuracy. Prepare a figure that plots the number of terms required vs. log_10tol for x = 1, x = 5, x = 10, and x = 20. Use a logspace-generated list of tolerances ranging from tol=l (100% error) to tol = lessthanorequalto - 9. Your plot should include proper labels, a title, and a legend.Explanation / Answer
This is the sin function.
function [y n] = sine(x, tol)
i = 1;
sign = 1;
y = x;
while abs(y - sin(x)) > tol
i += 2;
sign *= -1;
y = y + sign*(x^i)/factorial(i);
end
n = (i + 1)/ 2;
end
Driver
figure();
for x = [1 5 10 20]
count = [];
tols = logspace(0,-9);
for tol = tols
[y, n] = sine(x, tol);
count = [count n];
end
plot(tols, count);
hold
end
xlabel('0 < tolerance < 1') % x-axis label
ylabel('iterations') % y-axis label
Here you go champ. I have included 2 codes for you. I hope they will help you. If you have any doubt please feel free to comment below. I shall be glad to help you till the problem gets resolved.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.