<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/249728</link>
    <title>MATLAB Central Newsreader - Script for series expansion of sine</title>
    <description>Feed for thread: Script for series expansion of sine</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Thu, 23 Apr 2009 01:56:01 -0400</pubDate>
      <title>Script for series expansion of sine</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/249728#644797</link>
      <author>Sean </author>
      <description>Hello folks,&lt;br&gt;
&lt;br&gt;
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.&lt;br&gt;
&lt;br&gt;
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:&lt;br&gt;
&lt;br&gt;
% SINESERIES:  computes sin(x) from series expansion.&lt;br&gt;
% A script to evaluate the series expansion of sine with the formula:&lt;br&gt;
% sin(x) = x-(x^3/3!)+(x^5/5!)-... &lt;br&gt;
x = input('Enter the argument x:  ');&lt;br&gt;
n = input('Enter the interval n:  ');&lt;br&gt;
N = 1:1:n;     % Creates row vector with n elements, 1 at a time.&lt;br&gt;
k = 2*N-1;    % For convenience with prod() function&lt;br&gt;
j = N-1;        % For cleanup.&lt;br&gt;
sinseries = ((-1).^(j)).*((x.^(k))./(prod(k)))&lt;br&gt;
sum(sinseries)&lt;br&gt;
&lt;br&gt;
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.</description>
    </item>
    <item>
      <pubDate>Thu, 23 Apr 2009 02:33:15 -0400</pubDate>
      <title>Re: Script for series expansion of sine</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/249728#644799</link>
      <author>Roger Stafford</author>
      <description>&quot;Sean &quot; &amp;lt;el_sean@yahoo.com&amp;gt; wrote in message &amp;lt;gsohrh$27i$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hello folks,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 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.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 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:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % SINESERIES:  computes sin(x) from series expansion.&lt;br&gt;
&amp;gt; % A script to evaluate the series expansion of sine with the formula:&lt;br&gt;
&amp;gt; % sin(x) = x-(x^3/3!)+(x^5/5!)-... &lt;br&gt;
&amp;gt; x = input('Enter the argument x:  ');&lt;br&gt;
&amp;gt; n = input('Enter the interval n:  ');&lt;br&gt;
&amp;gt; N = 1:1:n;     % Creates row vector with n elements, 1 at a time.&lt;br&gt;
&amp;gt; k = 2*N-1;    % For convenience with prod() function&lt;br&gt;
&amp;gt; j = N-1;        % For cleanup.&lt;br&gt;
&amp;gt; sinseries = ((-1).^(j)).*((x.^(k))./(prod(k)))&lt;br&gt;
&amp;gt; sum(sinseries)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 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.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;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.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;You can see this if you follow each calculation step by step and compare it with how you would do it by  hand.&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
    <item>
      <pubDate>Thu, 23 Apr 2009 02:57:01 -0400</pubDate>
      <title>Re: Script for series expansion of sine</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/249728#644802</link>
      <author>Roger Stafford</author>
      <description>&quot;Roger Stafford&quot; &amp;lt;ellieandrogerxyzzy@mindspring.com.invalid&amp;gt; wrote in message &amp;lt;gsok1b$ruu$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;Sean &quot; &amp;lt;el_sean@yahoo.com&amp;gt; wrote in message &amp;lt;gsohrh$27i$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; 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:&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; % SINESERIES:  computes sin(x) from series expansion.&lt;br&gt;
&amp;gt; &amp;gt; % A script to evaluate the series expansion of sine with the formula:&lt;br&gt;
&amp;gt; &amp;gt; % sin(x) = x-(x^3/3!)+(x^5/5!)-... &lt;br&gt;
&amp;gt; &amp;gt; x = input('Enter the argument x:  ');&lt;br&gt;
&amp;gt; &amp;gt; n = input('Enter the interval n:  ');&lt;br&gt;
&amp;gt; &amp;gt; N = 1:1:n;     % Creates row vector with n elements, 1 at a time.&lt;br&gt;
&amp;gt; &amp;gt; k = 2*N-1;    % For convenience with prod() function&lt;br&gt;
&amp;gt; &amp;gt; j = N-1;        % For cleanup.&lt;br&gt;
&amp;gt; &amp;gt; sinseries = ((-1).^(j)).*((x.^(k))./(prod(k)))&lt;br&gt;
&amp;gt; &amp;gt; sum(sinseries)&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;I should have pointed out that there are more efficient ways of carrying out that computation.  Suppose n = 5.  You could do the computation this way:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;s = x*(1-x^2/(2*3)*(1-x^2/(4*5)*(1-x^2/(6*7)*(1-x^2/(8*9)))));&lt;br&gt;
&lt;br&gt;
Moreover you can easily devise a for-loop that accomplishes this calculation in the same sequence of operations for a general n.  You would have to work from the high end and go backwards.  It obviously involves far fewer multiplications and divisions and is more accurate numerically in the bargain.  In spite of the for-loop overhead, this method is bound to be faster for sufficiently large n, since it is an order n algorithm whereas your original one is order n^2.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;Another observation:  Don't try this with values of x substantially larger than 1 unless you have set n very high.  High values of x will cause the series to converge very slowly at first and only when the ratio x/k grows substantially smaller than 1 does it begin to converge at a decent rate.  In the actual computation of the sine function, things are done in a very different manner than this.&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
    <item>
      <pubDate>Thu, 23 Apr 2009 04:26:01 -0400</pubDate>
      <title>Re: Script for series expansion of sine</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/249728#644807</link>
      <author>Derek O'Connor</author>
      <description>&quot;Sean &quot; &amp;lt;el_sean@yahoo.com&amp;gt; wrote in message &amp;lt;gsohrh$27i$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hello folks,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 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.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 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:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % SINESERIES:  computes sin(x) from series expansion.&lt;br&gt;
&amp;gt; % A script to evaluate the series expansion of sine with the formula:&lt;br&gt;
&amp;gt; % sin(x) = x-(x^3/3!)+(x^5/5!)-... &lt;br&gt;
&amp;gt; x = input('Enter the argument x:  ');&lt;br&gt;
&amp;gt; n = input('Enter the interval n:  ');&lt;br&gt;
&amp;gt; N = 1:1:n;     % Creates row vector with n elements, 1 at a time.&lt;br&gt;
&amp;gt; k = 2*N-1;    % For convenience with prod() function&lt;br&gt;
&amp;gt; j = N-1;        % For cleanup.&lt;br&gt;
&amp;gt; sinseries = ((-1).^(j)).*((x.^(k))./(prod(k)))&lt;br&gt;
&amp;gt; sum(sinseries)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 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.&lt;br&gt;
&lt;br&gt;
Sean,&lt;br&gt;
&lt;br&gt;
The notes here &lt;a href=&quot;http://www.derekroconnor.net/NA/LE/LE-2006-2.pdf&quot;&gt;http://www.derekroconnor.net/NA/LE/LE-2006-2.pdf&lt;/a&gt;  explain why your code gives the wrong answers.&lt;br&gt;
&lt;br&gt;
I hope you read and digest all 14 pages of this homework solution.&lt;br&gt;
&lt;br&gt;
Regards,&lt;br&gt;
&lt;br&gt;
Derek O'Connor.</description>
    </item>
    <item>
      <pubDate>Thu, 23 Apr 2009 07:14:02 -0400</pubDate>
      <title>Re: Script for series expansion of sine</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/249728#644823</link>
      <author>Roger Stafford</author>
      <description>&quot;Roger Stafford&quot; &amp;lt;ellieandrogerxyzzy@mindspring.com.invalid&amp;gt; wrote in message &amp;lt;gsok1b$ruu$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt;   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.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;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:&lt;br&gt;
&lt;br&gt;
N = 1:n;&lt;br&gt;
k = 2*N-1;&lt;br&gt;
j = N-1;&lt;br&gt;
p = cumprod(1:2*n-1);&lt;br&gt;
sinex = sum((-1).^j.*x.^k./p(k));&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;To make up for that lapse I'll spell out in detail the for-loop method I mentioned.&lt;br&gt;
&lt;br&gt;
x2 = x^2/2;&lt;br&gt;
s = 1;&lt;br&gt;
for k = n-1:-1:1&lt;br&gt;
&amp;nbsp;s = 1-x2/((2*k+1)*k)*s;&lt;br&gt;
end&lt;br&gt;
s = x*s;&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
    <item>
      <pubDate>Sat, 25 Apr 2009 02:23:01 -0400</pubDate>
      <title>Re: Script for series expansion of sine</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/249728#645300</link>
      <author>Sean </author>
      <description>Derek &amp; Roger,&lt;br&gt;
&lt;br&gt;
Thanks for your informative replies!  I'm going to play around with this a bit, and get back to you.&lt;br&gt;
&lt;br&gt;
-Sean</description>
    </item>
  </channel>
</rss>

