I try to apply Taylor's series for sin(x) + cos(x)
24 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Phuri Nittiwat
el 21 de En. de 2022
Comentada: Phuri Nittiwat
el 9 de Feb. de 2022
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 comentarios
Jon
el 21 de En. de 2022
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
el 21 de En. de 2022
Editada: John D'Errico
el 21 de En. de 2022
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))
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)]
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)
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)]
sum(T)
cos(3*pi)
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.
Respuesta aceptada
Nadia Shaik
el 1 de Feb. de 2022
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!
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!