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 02:33:15 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 25
Message-ID: <gsok1b$ruu$1@fred.mathworks.com>
References: <gsohrh$27i$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1240453995 28638 172.30.248.37 (23 Apr 2009 02:33:15 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 23 Apr 2009 02:33:15 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:534845


"Sean " <el_sean@yahoo.com> wrote in message <gsohrh$27i$1@fred.mathworks.com>...
> Hello folks,
> 
> To begin with, I browsed through the archives looking for relevant posts to my question and found some related topics.  However, the posts were ill-structured and, appropriately, weren't answered.  Hopefully I can be a little more specific.
> 
> I'm trying to write a script to calculate the series expansion of sine with the inputs x (argument) and n.  Here's what I've thrown together so far:
> 
> % SINESERIES:  computes sin(x) from series expansion.
> % A script to evaluate the series expansion of sine with the formula:
> % sin(x) = x-(x^3/3!)+(x^5/5!)-... 
> x = input('Enter the argument x:  ');
> n = input('Enter the interval n:  ');
> N = 1:1:n;     % Creates row vector with n elements, 1 at a time.
> k = 2*N-1;    % For convenience with prod() function
> j = N-1;        % For cleanup.
> sinseries = ((-1).^(j)).*((x.^(k))./(prod(k)))
> sum(sinseries)
> 
> My calculations aren't turning out at all correctly, short of x = 1, n = 1.  I've been toying with the equation to see if I translated it wrong, but I think the problem lies in how I'm expressing the series.  If anybody has some advice, I'd greatly appreciate it.  Thanks.

  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.

  You can see this if you follow each calculation step by step and compare it with how you would do it by  hand.

Roger Stafford