The Taylor series expansion for a is: nl Write a MATLAB program that determines
ID: 2249557 • Letter: T
Question
The Taylor series expansion for a is: nl Write a MATLAB program that determines a using the Taylor series expansion. The program asks the user to type a value for x. Use a loop for adding the terms of the Taylor series. If c, is the nth term in the series, then the sum Sn of the n terms is S,=s,-iton. In each pass calculate the esti- mated error E given btop te .Stop adding terms when E 0.000001 000 n-1 The program displays the value of a. Use the program to calculate: (a) 23.5 Compare the values with those obtained by using a calculator. (b) 6.31.7Explanation / Answer
clear, clc
% Let's see more decimals
format long
% We go from n = 0 to n = 2
n = 0 : 2;
% Center and point to evaluate
c = pi/2;
x = 1.6;
% These are the derivatives for each term
d = [0 -1 0];
% We form the sequence, following the Taylor formula
seq = d .* (x - c).^n ./(factorial(n))
% We add-up to get the Taylor approximation
approx = sum(seq)
% Let's compare with the official number
real_value = cos(x)
script to test and use the function above.
clear, clc, close all
% Work with values around center c
c = pi/2;
x = -4 : .1 : 6;
y = cos(x);
% Plot the goal
plot(x, y, 'g', 'Linewidth', 3)
title('Study of Taylor series')
xlabel('x')
ylabel('cos(x) with different number of terms')
axis([-4 6 -3 3])
grid on
hold on
% Consider 4 terms in the series
smp = taylor_cosine(c, x, 4);
plot(x, smp, 'ro')
% Consider 6 terms
smp = taylor_cosine(c, x, 6);
plot(x, smp, 'b-.')
% Consider 10 terms
smp = taylor_cosine(c, x, 10);
plot(x, smp, 'k', 'linewidth', 3)
% Label the calculated lines
legend('cos(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.