When you write the Taylor series of a function about 0, also it is called Maclau
ID: 3112006 • Letter: W
Question
When you write the Taylor series of a function about 0, also it is called Maclaurin Series. Here we have the Maclaurin series of sin(x). sin(x) = sigma^k = infinity_N = 0 (-1)^N/(2N + 1)! x^2N + 1 Please write a MATLAB function to calculate the Maclaurin Series of sin(x) with the number of the series terms (k) and the vector array x as the input arrays. By using the function in question 3, with the k = 1, please plot the exact and approximate value of sin(x) for [-pi lessthanorequalto x lessthanorequalto pi]. In addition, plot the error bar figure. Use the step value "pi/8".Explanation / Answer
function smp = maclaurin_sin(x, n)
% x = a vector with values around 0
% n = number of terms in the series
% Start the series with 0
smp = 0;
% Consider all the possible derivatives
deriv = [0 1 0 -1]';
% Iterate n times (from 0 to n-1)
for i = 0 : n-1
% Implement the Maclaurin expansion
t(i+1, :) = deriv(1) * x.^(i) / factorial(i);
% Find the derivative for the next term
deriv = circshift(deriv, -1);
end
% Add-up the calculated terms
smp = sum(t);
clear, clc, close all
% Work with two full periods
x = -2*pi : .1 : 2*pi;
y = sin(x);
% Plot the goal
plot(x, y, 'g', 'Linewidth', 3)
title('Study of Maclaurin series')
xlabel('x')
ylabel('sin(x) with different number of terms')
axis([-2*pi 2*pi -3 3])
grid on
hold on
% Consider 4 terms
smp = maclaurin_sin(x, 4);
plot(x, smp, 'ro')
% Consider 6 terms
smp = maclaurin_sin(x, 6);
plot(x, smp, 'b-.')
% Consider 10 terms
smp = maclaurin_sin(x, 10);
plot(x, smp, 'k', 'linewidth', 3)
% Label the calculated lines
legend('sin(x)', '4-term series', ...
'6 terms', '10 terms')
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.