Taylor series for cos x

조회 수: 19 (최근 30일)
Deleena Rebello
Deleena Rebello 2022년 1월 25일
댓글: Rik 2022년 1월 26일
We know from Calculus that the Taylor expansion of cos x about x = 0
is given by
cosx = 1-x^2/2!+x^4/4!............
Write a Function or Script in MATLAB that will take N, x1, and x2
(respectively, the number of terms in this expansion and the interval of
comparison [x1, x2 ]) as inputs and will compare the actual function value
with its approximation. Use the interval [0; 6pi] and compare the actual
function with its expansion of 6 terms, 15 terms, and 30 terms (3 separate
plots). Limit your viewing window on the vertical axis to [-1:5 1:5].
please help me with this question
  댓글 수: 2
KSSV
KSSV 2022년 1월 25일
What have you attempted for the question? If you google I am sure you will get the work around.
Deleena Rebello
Deleena Rebello 2022년 1월 25일
편집: Rik 2022년 1월 25일
function f=SineTaylorSeries(N,x1)
%This function takes two inputs N and x1. It compares the Taylor
%expansion of sine with (N+1) terms to the actual sine over
%the interval [-x1,x1].
x=-x1:0.1:x1;
SinePlot=zeros(size(x1));
for k=0:N
SinePlot = SinePlot + (-1)^k*x.^(2*k+1)/factorial(2*k+1);
end
plot(x,SinePlot,'b')
hold on
plot(x,sin(x),'r+')
hold off
end
this a sample of sinetaylor series with only 2 input N and x1.
I want program for cos and 3 inputs N,x1,x2

댓글을 달려면 로그인하십시오.

답변 (1개)

Rik
Rik 2022년 1월 25일
There are a few edits you should make to your function:
% If you want a third input, just add it like this:
% (don't forget to edit the documentation)
function f=SineTaylorSeries(N,x1,x2)
%This function takes two inputs N and x1. It compares the Taylor
%expansion of sine with (N+1) terms to the actual sine over
%the interval [-x1,x1].
x=-x1:0.1:x1;
% ^^^
% I would suggest implementing this with linspace.
% This is also where you can put x2
SinePlot=zeros(size(x1));
% ^^
% Are you sure this should be x1? It looks like you should use x instead
for k=0:N
SinePlot = SinePlot + (-1)^k*x.^(2*k+1)/factorial(2*k+1);
end
% The fact that you are creating a plot isn't documented.
% You should either remove it or document it
plot(x,SinePlot,'b')
hold on
plot(x,sin(x),'r+')
hold off
% You didn't define f anywhere. Is it supposed to be SinePlot? Or the
% handle to the figure with the plot? Something else still?
end
  댓글 수: 2
Deleena Rebello
Deleena Rebello 2022년 1월 26일
the interval is between x1 and x2. So please make a update on that too
Rik
Rik 2022년 1월 26일
I suggested that you use linspace. Did you read the documentation? How do you think you could make sure the interval is between x1 and x2?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by