Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 10-26-2005, 06:02 PM   #1 (permalink)
josh2two
Registered User
 
Join Date: Sep 2005
Posts: 22
josh2two is on a distinguished road
Help with Sin in Taylor series!!!

I can't figure out how to do the sin of a function using taylor series in c++.

I figured out the cos

for( i = 2; i <= 2 * N; i+= 2)
{
term = -1 * term * x* x / (i* (i-1));
sum = sum + term;
}


PLEASE help me find the sin formula.
josh2two is offline   Reply With Quote
Old 10-26-2005, 11:48 PM   #2 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
These guys are always good for math quandaries. The def for sin is in the second equation block. http://mathworld.wolfram.com/TaylorSeries.html
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 10-27-2005, 07:27 AM   #3 (permalink)
josh2two
Registered User
 
Join Date: Sep 2005
Posts: 22
josh2two is on a distinguished road
I know the equation for the sin in taylor series. My problem is putting it into my c++ program like i did with the cos.

this is the cos
for( i = 2; i <= 2 * N; i+= 2)
{
term = -1 * term * x* x / (i* (i-1));
sum = sum + term;
}


PLease help with the sin code of the taylor.
josh2two is offline   Reply With Quote
Old 10-27-2005, 08:28 AM   #4 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
Okay... Sin doesn't seem any trickier than Cosine but here goes,
Code:
//Equation for Sin(x) 
//Sigma(0, infinity) { -1^n * (x^(2n+1))/(2n+1)! }  - Makes me wish vBulletin supported TeX

//Best to do things properly or not at all
int factorial (int n) {
     int i;
     int result = 1;
     if( n > 0 ) {
	  for( i = 1; i <= n; i++ ) {
	       result = result * i;
	  }
     }

     return result;
}

int taylor_sin (int pos, int x) {
     int i;
     int term;
     int sum = 0;
     
     for( i = 0; i < pos; i++ ) {
	  term = pow(-1, i) * pow(x,(2 * i + 1)) / factorial(2 * i + 1);
	  sum = sum + term;
     }
}
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 10-27-2005, 11:41 AM   #5 (permalink)
josh2two
Registered User
 
Join Date: Sep 2005
Posts: 22
josh2two is on a distinguished road
im a begineer.

when i put that code into dev c++, i can't get it to work. It says something about the pow being the error.


thanks for your help
josh
josh2two is offline   Reply With Quote
Old 10-27-2005, 01:04 PM   #6 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
It's from cmath
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 10-27-2005, 08:16 PM   #7 (permalink)
josh2two
Registered User
 
Join Date: Sep 2005
Posts: 22
josh2two is on a distinguished road
i have the #include<cmath> already in the program.

please help
josh2two is offline   Reply With Quote
Old 10-27-2005, 08:46 PM   #8 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
Quit being lazy. If you can't get the library function to work, write your own pow() function. Programming is about problem solving. Ideally, we want to avoid duplication but, the function isn't that hard to write.
Code:
int pow ( int base, int exp ) {
     if ( exp <= 0 ) {
	  //not mathematically correct but negative exponents won't return an int anyway
	  return 1; 
	       }
     else {
	  int i;
	  int result = 1;
	  
	  for( i = 0; i < exp; i++ ) {
	       result = result * base;
	  }

	  return result;
     }
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 10-27-2005, 08:48 PM   #9 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,709
redhead is on a distinguished road
Or link to the math library...
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 10-27-2005, 10:50 PM   #10 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
#include <cmath>
{...
std::pow()
...}
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 04:30 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting