GIDForums  

Go Back   GIDForums > Computer Programming Forums > Python Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Dec-2008, 11:01
osamie osamie is offline
New Member
 
Join Date: Nov 2008
Posts: 3
osamie is on a distinguished road

Computing infinite series on python


Please I have problem writing a function that returns the cosine of a number with the aid of an infinite series, cos x = 1 − x2⁄2! + x4⁄4! − x6⁄6! + …
The program is meant to collect two arguments; n, number of terms and x, the number.
writing the factorial function wasn't a problem for me. The infinite series one, is just slapping me Please I need some help
  #2  
Old 02-Dec-2008, 18:51
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,200
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Computing ininite series on python


Quote:
Originally Posted by osamie
The infinite series one, is just slapping me

I would not actually use the factorial function. My method would be build the terms as you go.

Note that only even-numbered terms of the Taylor series are non-zero

So, for example the sum of first few terms looks like
Code:
sum = 1 - x*x/(2*1) + x*x*x*x/(4*3*2*1) - x*x*x*x*x*x/(6*5*4*3*2*1) ^ ^ ^ ^ | | | | i=0 i=2 i=4 i=6

After the first term, here's the ticket:
  1. The numerator of each term is x*x*(old numerator)
  2. The denominator of the i'th term is i*(i-1)*(old denominator)
  3. The signs of the terms alternate

Here's an approach
Code:
Function that uses N terms to approximate cos(x) Let num = 1.0 Let denom = 1.0 Let sum = 1.0 Let sign = -1 Let i go from 2 to 2*N in steps of 2 Loop Let num = num * x * x Let denom = denom * i * (i - 1) Let sum = sum + sign * num / denom Let sign = -sign End Loop return sum

Typical result (for 0.5 radians):
Code:
Approximate value of cos(5.000000e-01) calculated as follows: 1 term : actual = 8.775826e-01, approx = 1.000000e+00, err = 1.224174e-01 2 terms: actual = 8.775826e-01, approx = 8.750000e-01, err = -2.582562e-03 3 terms: actual = 8.775826e-01, approx = 8.776042e-01, err = 2.160478e-05 4 terms: actual = 8.775826e-01, approx = 8.775825e-01, err = -9.661260e-08 5 terms: actual = 8.775826e-01, approx = 8.775826e-01, err = 2.686054e-10 6 terms: actual = 8.775826e-01, approx = 8.775826e-01, err = -5.090373e-13 7 terms: actual = 8.775826e-01, approx = 8.775826e-01, err = 6.661338e-16

Regards,

Dave
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Python - Need help with time card script/wage calculator sigkill9 Python Forum 5 12-Mar-2008 22:01
Neutral Python book - the Quest for the Impossible? Kimmo Python Forum 1 04-Aug-2007 13:56
Looking for opinions crystalattice Miscellaneous Programming Forum 6 27-Sep-2006 22:02
Re: Beginning Python Tutorial (Part 5) crystalattice Python Forum 0 04-Oct-2005 05:27
Re: Beginning Python Tutorial (Part 2) crystalattice Python Forum 2 31-Jul-2005 13:25

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:36.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.