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$>
References: <gsohrh$27i$> <gsok1b$ruu$>
Reply-To: <HIDDEN>
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: 1240470842 3570 (23 Apr 2009 07:14:02 GMT)
NNTP-Posting-Date: Thu, 23 Apr 2009 07:14:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: comp.soft-sys.matlab:534869

"Roger Stafford" <> wrote in message <gsok1b$ruu$>...
>   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;
s = x*s;

Roger Stafford