Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Script for series expansion of sine
Date: Thu, 23 Apr 2009 07:14:02 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 21
Message-ID: <gsp4fq$3fi$1@fred.mathworks.com>
References: <gsohrh$27i$1@fred.mathworks.com> <gsok1b$ruu$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1240470842 3570 172.30.248.38 (23 Apr 2009 07:14:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 23 Apr 2009 07:14:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:534869

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gsok1b$ruu$1@fred.mathworks.com>...
>   At first glance I would say the culprit here is prod(k).  You are expecting its results to be a vector consisting of all the products from 1 up to a number that increases by 2 each time.  Instead you are getting a single scalar answer consisting all the products in k.  It would only work for n - 1.  What you need is cumprod instead.

  Sean, I erred in telling you that just cumprod(k) would correct the error in your code.  The prod(k) divisor is still certainly wrong, as I mentioned before.  However, what you would need to rescue that code is this:

N = 1:n;
k = 2*N-1;
j = N-1;
p = cumprod(1:2*n-1);
sinex = sum((-1).^j.*x.^k./p(k));

  To make up for that lapse I'll spell out in detail the for-loop method I mentioned.

x2 = x^2/2;
s = 1;
for k = n-1:-1:1
 s = 1-x2/((2*k+1)*k)*s;
end
s = x*s;

Roger Stafford