Clear Filters
Clear Filters

Maclaurin series in M file with given tolerance level

2 views (last 30 days)
{x=(pi/9); sin(x)=x-((x^3)/(1:3))+((x^5)/(1:5))-((x^7)/(1:7));}
For my class we are to come up with a code for a Maclaurin series that will compute sin(pi/9) to a tolerance of 10^-5. The above code works, but I don't think the professor wants to me to just put in tons of terms until the series reaches the correct tolerance level. So that being said my questions are: 1. How do i determine what the tolerance level of my series is? 2. How would I use variables (and possibly a loop?) to add an endless number of terms without typing them all out. I'm not very good with this program, so any help would be much appreciated. Thank you
  1 Comment
John D'Errico
John D'Errico on 10 Feb 2016
Edited: John D'Errico on 10 Feb 2016
Actually, the code you wrote does NOT work. In fact, it will generate an error. For example, I'm not at all sure why you think you needed to divide by 1:3, 1:5 and 1:7 in your code. As I said, there is an error in the house. Perhaps you wanted to write the product of those terms. Or perhaps you thought that dividing a scalar by a vector would somehow divide the numerator by each of those terms in order, and return one result.
help prod
Or, you might just have used the factorial function, though perhaps that was disallowed by your instructor.

Sign in to comment.

Answers (1)

John D'Errico
John D'Errico on 10 Feb 2016
Ok, since you did give some code there, even though it would not work as you thought, here is an idea:
x = pi/9;
currentTerm = x;
tol = 1e-5;
sineApprox = currentTerm;
k = 1;
while abs(currentTerm) > tol
currentTerm = -currentTerm*x^2/(k+1)/(k+2);
k = k + 2;
sineApprox = sineApprox + currentTerm;
end
A simple, looped version of a series, with a tolerance. This assumes the terms are decreasing in magnitude, and that when the last term we see is less than the tolerance, then we can stop.

Community Treasure Hunt

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

Start Hunting!