1. Write a MATLAB script, by hand, on this page, that calculates the nth degree Taylor expansion of cos x and then compares the plot of this expansion to the original function. In one of your first lines, you may fix n (e.g., n = 6) but the remainder of the code should refer to n.
MATLAB Script:
close all
clear
clc
n = 6;
syms x
f = cos(x);
Tn = taylor(f, x, 'Order', n + 1);
disp('Taylor Expansion:'), disp(Tn)
xx = 0:0.01:2*pi;
yy_exact = cos(xx);
yy_taylor = subs(Tn, xx);
plot(xx, yy_exact, xx, yy_taylor)
legend('cos(x)', 'Taylor Expansion', 'Location', 'southwest')
xlabel('x'), ylabel('y'), title('Taylor Expansion of cos(x)')
Output:
Taylor Expansion:
- x^6/720 + x^4/24 - x^2/2 + 1
Plot:
Get Answers For Free
Most questions answered within 1 hours.