Taylor Series Expansions for sin(x)

Given: sin(x)=(-1)^i*x^(2*i=1)/(2i+1)!...or... ...
Find: Code that calculates the Taylor series for sin(pi/3) using the equation above
Issue: It just seems like my code isn't approximating right, anything I am overlooking here?
My Solution: I also am unsure of how to compare the CORRECT summation results to realsin which happens to be 0.866
x=pi/3;
sum1=0;
N=10;
for i=0:N
a=(2*i+1);
sum1=(-1).^i.*(x.^(2i+1))./factorial(a);
fprintf('This is the estimate of sin(pi/3): %10e \n',sum1)
% diff=sum1-sin(pi/3);
end
% Could also use the input function to have the user enter a number>10, factorial is accurate up to N<=21
% summation here is always negative every other value?
% realsin=pi/3=0.8660
% Need a way to display how close my estimate was, difference between,
% howclose=diff(sum1, realsin)
% fprintf('Estimate was ... off of actual value: %0.10e', howclose)

 Accepted Answer

Voss
Voss on 22 Mar 2024
2i should be 2*i.
And you need to accumulate the sum; as it is now sum1 is only the current term and it's never added to anything.

11 Comments

Depending on the instructions in the assignment, storing each term in an element of an array inside the loop then calling sum on the array after the loop may be useful. If the textbook shows each term of the sum (to illustrate what happens if you use the series for larger values of x, like Cleve did in this old Cleve's Corner article) you could use that array to check that each term was computed correctly.
Good catch Voss. Am I supposed to do something like this, (as shown in the linked article)?
function s = sin(x)
s = 0;
t = x;
n = 1;
while s+t ~= s;
s = s + t;
t = -x.^2/((n+1)*(n+2)).*t;
n = n + 2;
end
I know I need to use the factorial function, which he did not... Also, how would I sum every iteration of sum1? Something like this?: (Note, he is using a while loop, and I am using a for loop):
x=pi/3;
sum1=0;
s=0;
N=10;
for i=0:N
a=(2*i+1);
sum1=(-1).^i.*(x.^(2*i+1))./factorial(a);
s=s+sum1; % This would work, yes?
fprintf('This is the estimate of sin(pi/3): %0.10e \n',s)
end
That should work, yeah.
x=pi/3;
% sum1=0; % commenting this out because it is no longer necessary
s=0;
N=10;
for i=0:N
a=(2*i+1);
sum1=(-1).^i.*(x.^(2*i+1))./factorial(a);
s=s+sum1; % This would work, yes?
fprintf('This is the estimate of sin(pi/3): %0.10e \n',s)
end
This is the estimate of sin(pi/3): 1.0471975512e+00 This is the estimate of sin(pi/3): 8.5580078157e-01 This is the estimate of sin(pi/3): 8.6629528379e-01 This is the estimate of sin(pi/3): 8.6602127166e-01 This is the estimate of sin(pi/3): 8.6602544510e-01 This is the estimate of sin(pi/3): 8.6602540349e-01 This is the estimate of sin(pi/3): 8.6602540379e-01 This is the estimate of sin(pi/3): 8.6602540378e-01 This is the estimate of sin(pi/3): 8.6602540378e-01 This is the estimate of sin(pi/3): 8.6602540378e-01 This is the estimate of sin(pi/3): 8.6602540378e-01
Is there a way to make it formatted such that it displays only the last value? (The one that is the closest approximation to realsin? Because then I should be able to take it a step further and subtract realsin from sum1 and display the result to see how close it approximated. Like so:
realsin=sin(pi/3)
sum1=s
acc=s-realsin
fprintf('This is the accuracy of the taylor series: \n',acc)
For some reason the above code doesn't actually give me a result of the difference between the 2 at every iteration.
"Is there a way to make it formatted such that it displays only the last value?"
Yes, move the fprintf command from inside the loop to after the loop:
x=pi/3;
s=0;
N=10;
for i=0:N
a=(2*i+1);
sum1=(-1).^i.*(x.^(2*i+1))./factorial(a);
s=s+sum1;
end
fprintf('This is the estimate of sin(pi/3): %0.10e \n',s)
This is the estimate of sin(pi/3): 8.6602540378e-01
If you also want the difference between the estimate and the actual value at every iteration, then that has to go inside the loop:
x=pi/3;
realsin=sin(x);
s=0;
N=10;
for i=0:N
a=(2*i+1);
sum1=(-1).^i.*(x.^(2*i+1))./factorial(a);
s=s+sum1;
acc = s-realsin;
fprintf('This is the accuracy of the taylor series: %0.10e\n',acc)
end
This is the accuracy of the taylor series: 1.8117214741e-01 This is the accuracy of the taylor series: -1.0224622219e-02 This is the accuracy of the taylor series: 2.6988000240e-04 This is the accuracy of the taylor series: -4.1321280660e-06 This is the accuracy of the taylor series: 4.1315342481e-08 This is the accuracy of the taylor series: -2.9095592602e-10 This is the accuracy of the taylor series: 1.5211165660e-12 This is the accuracy of the taylor series: -6.2172489379e-15 This is the accuracy of the taylor series: -1.1102230246e-16 This is the accuracy of the taylor series: -1.1102230246e-16 This is the accuracy of the taylor series: -1.1102230246e-16
fprintf('This is the estimate of sin(pi/3): %0.10e \n',s)
This is the estimate of sin(pi/3): 8.6602540378e-01
Genius. This makes so much more sense now. I appreciate your help on these matters. I learn by doing and also from examples I have found out.
This is MY final iteration of the code, cleaning up some variables and such:
x=pi/3;
sum1=0;
N=10;
for i=0:N
a=(2*i+1);
TSa=((-1).^i).*(x.^(2*i+1))./factorial(a);
sum1=TSa+sum1;
realsin=sin(pi/3);
acc=realsin-sum1;
fprintf('This is the accuracy of the Taylor Series iterations: %0.10e \n',acc)
end
fprintf('This is the closest estimate of sin(pi/3): %0.10e \n',sum1)
% Could also use the input function to have the user enter a number>10, factorial is accurate up to N<=21
Not to beat a dead horse, but just out of curiosity, how would I do this without a loop?
Looks like it should work.
I'll point out that you can move the definition of realsin to before the loop, since it doesn't change with each loop iteration. You can refer to how/where I defined it in my previous comment.
Thank you, I missed that part!
You're welcome!
You're the best, Voss!

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products

Release

R2023b

Tags

Asked:

on 22 Mar 2024

Commented:

on 8 Apr 2024

Community Treasure Hunt

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

Start Hunting!