필터 지우기
필터 지우기

I try to apply Taylor's series for sin(x) + cos(x)

조회 수: 14 (최근 30일)
Phuri Nittiwat
Phuri Nittiwat 2022년 1월 21일
댓글: Phuri Nittiwat 2022년 2월 9일
f(x) = sin(x) + cos(x) = (-1)^k[ x^2k+1 / (2k+1!) + x^2k / (2k!)]
let
Tn(x) = (-1)^k[ x^2k+1 / (2k+1!) + x^2k / (2k!)]
1) plot f (x) = sin(x) + cos(x), T1(x), T2(x), T5(x) for x [-π,π]
I come up with
n = input(prompt)
x = linspace(-pi,pi);
SumTerms = (-1)*(x.^(1)./factorial(1)) + (-1)*(x.^(0)./factorial(0)); %initail
for i = 1:n
SumTerms = SumTerms + (-1)^factorial(i)*(x.^(2*i+1)./factorial(2*i+1)) + (-1)^factorial(i)*(x.^(2*i)./factorial(2*i));
end
f = SumTerms
figure,
plot(x,f);
title('tayler series of sinx + cosx')
xlabel('xi')
ylabel('y')
2) Plot Tn(3π) for n = 1, 2, . . . , 20. Note: In this case, your x-axis should be n.
n = input(prompt)
x = 3*pi;
SumTerms = (-1)*(x.^(1)./factorial(1)) + (-1)*(x.^(0)./factorial(0)); %initail
for i = 1:n
SumTerms = SumTerms + (-1)^factorial(i)*(x.^(2*i+1)./factorial(2*i+1)) + (-1)^factorial(i)*(x.^(2*i)./factorial(2*i));
end
f = SumTerms
figure,
plot(n,f);
title('tayler series of sinx + cosx')
xlabel('x*pi')
ylabel('sin(x) + cos(x)')
I try to plot x as 3*pi and y as taylor series of sin(x) + cos(x) but its show nothing on the graph
  댓글 수: 2
Jon
Jon 2022년 1월 21일
What is your question? Are you getting errors? If so please copy and paste them here. Are you unsure how to do a certain step, if so please explain what you are trying to do and where you are stuck
John D'Errico
John D'Errico 2022년 1월 21일
편집: John D'Errico 2022년 1월 21일
To add to the good points made by Jon, I need to add some points, but now from John. Sadly, I guess we are a dime a dozen. :)
First, note that the sum of sin(x) + cos(x) is still jjust a sine wave. The amplitude and phase angle change.
syms x
simplify((sin(x) + cos(x)) - sqrt(2)*sin(x+pi/4))
ans = 
0
So you should still see essentially a sine wave. If you don't, then what are you seeing? Why do you think there is a problem?
Next, you need to consider if this series is convergent. Well, theoretically, it is globally convergent, if you compute with an infinite number of digits. But the reason this assignment was given to you was surely to see if you can sum a series, but also so you should see that a series need not be convergent without taking a huge number of terms, and when you do that, you end up seeing massive subtractive cancellation. Effectively, don't expect that series to converge at all, ever in double precision for x at all large in magnitude. How large must x be to see that problem? I'd not be surprsed if things start going to hell for abs(x) as large as pi. At least there you will start to see some numerical junk happening. At x=3*pi? You really are going to see junk happen there.
Next, you might notice that at x=3*pi, cos(x) is technically -1. But what hapens is you have numerical problems that prevent this from happening. For example, at x = pi/3, so a relatively small number. we have this in the cosine series:
format long g
x = pi/3;
N = 16;
n = (0:2:N)';
T = (-1).^(n/2).*x.^n./factorial(n);
[T,cumsum(T)]
ans = 9×2
1 1 -0.548311355616075 0.451688644383925 0.0501075571162564 0.501796201500181 -0.0018316361712683 0.499964565328913 3.58681040022699e-05 0.500000433432915 -4.37041971752511e-07 0.499999996390943 3.63083448473851e-09 0.500000000021778 -2.18772283334568e-11 0.4999999999999 9.99627727053343e-14 0.5
What happens is you are starting to get large numbers, there, but of opposite signs. Now form a cumulative sum, as we see it is converging to 1/2, as expected, since
cos(pi/3)
ans =
0.5
is 1/2.
So all is good there. Now see what happens at x = 3*pi. I'll even go out a few more terms.
x = 3*pi;
N = 32;
n = (0:2:N)';
T = (-1).^(n/2).*x.^n./factorial(n);
[T,cumsum(T)]
ans = 17×2
1.0e+00 * 1 1 -44.4132198049021 -43.4132198049021 328.755682239758 285.342462434856 -973.406558494995 -688.064096060139 1544.0042657847 855.940169724559 -1523.87112968894 -667.930959964381 1025.45490056393 357.523940599549 -500.480812074284 -142.956871474735 185.233035956592 42.2761644818571 -53.7699054971861 -11.493741015329
sum(T)
ans =
-0.999995780428758
cos(3*pi)
ans =
-1
That is actually a little better than I thought would happen, but we are starting to see contamination from floating point errors and massive subtractive cancellation.
Similar stuff happens for the sine series for large arguments. And 10 is starting to grow large in this context.

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

채택된 답변

Nadia Shaik
Nadia Shaik 2022년 2월 1일
Hi,
From my understanding you can plot the figure when x is in the range of -pi to pi. But you are not able to plot the figure when x=3*pi.
The graph is being plotted, but as it is a single point it is not visible. When you try to plot a marker and colour it will be visible
The following changes have been done to the code
prompt='enter a value '
n = input(prompt)
x = 3*pi;
SumTerms = (-1)*(x.^(1)./factorial(1)) + (-1)*(x.^(0)./factorial(0)); %initail
for i = 1:n
SumTerms = SumTerms + (-1)^factorial(i)*(x.^(2*i+1)./factorial(2*i+1)) + (-1)^factorial(i)*(x.^(2*i)./factorial(2*i));
end
f = SumTerms
figure,
plot(x,f,"ko");
title('tayler series of sinx + cosx')
xlabel('xi')
ylabel('y')
Results for the graph
Hope it helps!
  댓글 수: 1
Phuri Nittiwat
Phuri Nittiwat 2022년 2월 9일
I Think for the case ploting x = 3*pi with the series, by themself is a sinusoidal pattern.
As a funtion its self is a vector so that I create 3*pi as a vector quantity that this case I think is the solution for this problem
clear
clc
clear all
%get input from user
prompt = 'initail value start from k=1: ';
k = 1 %start from k = 1
prompt = 'Enter last value: ';
n = input(prompt) %last number of the summation
x = linspace(0,3*pi,n); % X is 3*pi which in range of (0,3*pi) circle which n is the point we consider
t = SumT(k,n,x)
r = 0:1:n-1 %counting number
%ploting n in x axis vs y of taylor's series of 3*pi
figure,
plot(r,t);
title('tayler series of sinx + cosx')
xlabel('n')
ylabel('Summation of 3*pi')

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by