A Tylor series expansion is When f(x) is expanded at a local point x0=0, it is a
ID: 2991311 • Letter: A
Question
A Tylor series expansion is When f(x) is expanded at a local point x0=0, it is also called the Maclaurin series expansion Write a MATLAB program to calculate cos(x) based on the Maclaurin series expansion when f(x) = cos(x) You MATLAB code submission to T-square should contain two files: (1) cos maclaurinan This file is a general function so that, for example, a functional call of cos maclaurin(pi/4, 6) should return the value of cos(x 4) from the expansion in with a polynomial degree or order of 6. (2) draw_ cos_ maclaurin.m This file is a script that draw four subplots. one is cos(x) by calling cos() directly in Matlab and the other three are approximated curves by calling the above function cos maclaurin(), with which the degree or order of the polynomial is 6, 8, 10 respectively. The range of x in the four subplots should be [0, 4x]. Compare the results between cos() and cos maclaurin().Explanation / Answer
Here is the cos maclaurian function :
function y=cos_maclaurian(x,n)
global x;
global n;
i=0;
sum=0;
% value(1)=((-1)^(1)*2*x)/factorial(2*1); % initializing 1st value using maclaurian series
for i=0:n
value=((-1)^(i)*x^(2*(i)))/factorial(2*(i));
% value(i)=value(i-1)+value(i);% summation according to maclaurian series formula
sum=sum+value;
end
y=sum;
end
Here is the script for plottig cos and cos maclaurian together:
clc
clear all
close all
t=0:0.1:4*pi;
n=6;
global x;
global n;
for l1=1:3;
i=1;
for x=0:0.1:4*pi
y(i)=cos_maclaurian(x,n);
w=cos(t);
i=i+1;
end
subplot(3,2,2*l1-1);
plot(t,w);
title(['cos function for order',num2str(n)]);
subplot(3,2,2*l1);
plot(t,y);
title(['cos maclaurian function for order',num2str(n)]);
n=n+2;
end
Here is the filefor evaluating cos_maclaaurian at (pi/4,6):
clc
clear all
global x;
global n;
disp('value of cos(x) at pi/4 upto 6 order')
n=6; % for iteration
x=4*pi; % value at which we want to find cos(x)
y=cos_maclaurian(x,n)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.