In MatLab The Taylor series for cosx is given by cosx1x22x4
In MatLab - The Taylor series for cos(x) is given by: cos(x)=1x22!+x44!x66!+x88!x1010!+... Plot the following figure, which shows, for 2x2 , the graph of the function cos(x) and graphs of the Taylor series expansion of cos(x) with two, four, and six terms. Label the axes and display a legend. Taylorcos
Solution
%% CODE
x=0:0.1:pi;
y=cos(x);
xlabel(\'x\');
ylabel(\'cosx\');
plot(x,y,\'linewidth\',2);
grid on;
% Taylor Series of Cos(x)
N=6;
yest=0;
for n=0:N
yest=yest+(((-1)^n)*(x.^(2*n))./factorial(2*n));
end
hold on;
xlabel(\'x\');
ylabel(\'cosx\');
plot(x,yest,\'r-\',\'linewidth\',2)
legend(\'Actual function\',\'Taylors series expansion\');
