C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 31st, 2012, 09:58 PM
Astrodude Astrodude is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 22 Astrodude User rank is Corporal (100 - 500 Reputation Level)Astrodude User rank is Corporal (100 - 500 Reputation Level)Astrodude User rank is Corporal (100 - 500 Reputation Level)Astrodude User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 h 57 m 34 sec
Reputation Power: 0
What am I doing wrong?

I'm trying to write a function that outputs that Taylor series sum of the sine of an angle and it's only working for 0 and 90 degrees and no other values.

Code:
int factorial(int n)
{
      int i; 
      int f;
       
      for(i=0; i<=n; i++)
      {
          if(i==0){
			  f=1;
		  }
		  else{

		  f = f * i;
		  }
      }
       
      return(f);  
}
double sind(double angle){
	double r;
	int n;
	int sum;
	int term;
	r=(PI/180)*angle;
	sum=r;
	term=r;
	n=3;
	while(term>=0.0001){
			term=(r*r*term)/factorial(n)*(-1);
			sum=sum+term;
			n+=2;
		}
	return(sum);
}

Reply With Quote
  #2  
Old October 31st, 2012, 11:06 PM
Lux Perpetua Lux Perpetua is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,938 Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 h 43 m 16 sec
Reputation Power: 1312
sin(r) = r - r^3/6 + r^5/120 - r^7/5040 + ...
= r + r*(-r*r/(2*3)) + r*(-r*r/(2*3))*(-r*r/(4*5)) + r*(-r*r/(2*3))*(-r*r/(4*5))*(-r*r/(6*7)) + ...

so this is wrong:
Code:
			term=(r*r*term)/factorial(n)*(-1);

Reply With Quote
  #3  
Old November 1st, 2012, 01:45 PM
Astrodude Astrodude is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 22 Astrodude User rank is Corporal (100 - 500 Reputation Level)Astrodude User rank is Corporal (100 - 500 Reputation Level)Astrodude User rank is Corporal (100 - 500 Reputation Level)Astrodude User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 h 57 m 34 sec
Reputation Power: 0
Quote:
Originally Posted by Lux Perpetua
sin(r) = r - r^3/6 + r^5/120 - r^7/5040 + ...
= r + r*(-r*r/(2*3)) + r*(-r*r/(2*3))*(-r*r/(4*5)) + r*(-r*r/(2*3))*(-r*r/(4*5))*(-r*r/(6*7)) + ...

so this is wrong:
Code:
			term=(r*r*term)/factorial(n)*(-1);


I can't find the mistake in my code. How is that term wrong when n increments 2?

I've updated my code.

Code:
int factorial(int n)
{
      int i; 
      int f;
       
      for(i=0; i<=n; i++)
      {
          if(i==0){
			  f=1;
		  }
		  else{

		  f = f * i;
		  }
      }
       
      return(f);  
}
double sind(double angle){
	double r;
	int n;
	int sum;
	int term;
	int Taylor;
	r=(PI/180)*angle;
	sum=r;
	term=r;
	Taylor=r;
	n=3;
	while(Taylor>=ALMOSTZERO){
			
			term=(r*r*term)*-1;
			Taylor=term/factorial(n);
			sum=sum+Taylor;
			n+=2;
		}
	return(sum);
}

Reply With Quote
  #4  
Old November 1st, 2012, 02:03 PM
bdb bdb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 156 bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 32
When you convert an angle to radians
Code:
r=(PI/180)*angle;

and then, that value to int
Code:
Taylor=r;

you will always get 0 for angles in the range 0 to 57.2957795...
1 for angles in the range 57.3 to 114.6
...

Reply With Quote
  #5  
Old November 1st, 2012, 04:50 PM
Astrodude Astrodude is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 22 Astrodude User rank is Corporal (100 - 500 Reputation Level)Astrodude User rank is Corporal (100 - 500 Reputation Level)Astrodude User rank is Corporal (100 - 500 Reputation Level)Astrodude User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 h 57 m 34 sec
Reputation Power: 0
Quote:
Originally Posted by bdb
When you convert an angle to radians
Code:
r=(PI/180)*angle;

and then, that value to int
Code:
Taylor=r;

you will always get 0 for angles in the range 0 to 57.2957795...
1 for angles in the range 57.3 to 114.6
...


Thanks for the help! The program works a lot better now that I've switched those terms to doubles!

It's still not giving accurate outputs though; frequently off after the first decimal place. I thought the Taylor term was redundant so I got rid of it.

Here's the code:

Code:
int factorial(int n)
{
      int i; 
      int f;
       
      for(i=0; i<=n; i++)
      {
          if(i==0){
			  f=1;
		  }
		  else{

		  f = f * i;
		  }
      }
       
      return(f);  
}
double sind(double angle){
	double r;
	int n;
	double sum;
	double term;
	r=(PI/180)*angle;
	sum=r;
	term=r;
	n=3;
	while(term>=0.00001){
			term=(r*r*term)/factorial(n)*(-1);
			sum+=term;
			n+=2;
		}
	return(sum);
}


The output is still slightly off.

Reply With Quote
  #6  
Old November 1st, 2012, 05:07 PM
bdb bdb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 156 bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 32
term is either positive or negative.
When it is negative your while loop terminates

Reply With Quote
  #7  
Old November 1st, 2012, 05:43 PM
Astrodude Astrodude is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 22 Astrodude User rank is Corporal (100 - 500 Reputation Level)Astrodude User rank is Corporal (100 - 500 Reputation Level)Astrodude User rank is Corporal (100 - 500 Reputation Level)Astrodude User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 h 57 m 34 sec
Reputation Power: 0
Quote:
Originally Posted by bdb
term is either positive or negative.
When it is negative your while loop terminates


I've actually changed it to be:
Code:
while(abs(term)>=ALMOSTZERO)){
			term=(r*r*term)/factorial(n)*(-1);
			sum+=term;
			n+=2;
		}

and I'm getting the same output. The sine of 30 degrees is 0.4997 according to my program.

Reply With Quote
  #8  
Old November 1st, 2012, 05:48 PM
bdb bdb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 156 bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 32
I think maybe you want fabs() (declared in <math.h>) ... abs() (declared in <stdlib.h>) accepts and returns values of type int.

Reply With Quote
  #9  
Old November 1st, 2012, 06:48 PM
Astrodude Astrodude is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 22 Astrodude User rank is Corporal (100 - 500 Reputation Level)Astrodude User rank is Corporal (100 - 500 Reputation Level)Astrodude User rank is Corporal (100 - 500 Reputation Level)Astrodude User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 h 57 m 34 sec
Reputation Power: 0
Quote:
Originally Posted by bdb
I think maybe you want fabs() (declared in <math.h>) ... abs() (declared in <stdlib.h>) accepts and returns values of type int.


I switched to fabs and I got the same results.

Reply With Quote
  #10  
Old November 1st, 2012, 07:14 PM
Lux Perpetua Lux Perpetua is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,938 Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 h 43 m 16 sec
Reputation Power: 1312
Quote:
Originally Posted by Astrodude
I can't find the mistake in my code. How is that term wrong when n increments 2?
Write out the first few terms in your version with actual numbers. Print the terms out in your program to verify that they agree with what you wrote out. You will see that your terms are not the same as my terms. Hint: your denominator is wrong.

Reply With Quote
  #11  
Old November 1st, 2012, 07:51 PM
Astrodude Astrodude is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 22 Astrodude User rank is Corporal (100 - 500 Reputation Level)Astrodude User rank is Corporal (100 - 500 Reputation Level)Astrodude User rank is Corporal (100 - 500 Reputation Level)Astrodude User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 h 57 m 34 sec
Reputation Power: 0
Quote:
Originally Posted by Lux Perpetua
Write out the first few terms in your version with actual numbers. Print the terms out in your program to verify that they agree with what you wrote out. You will see that your terms are not the same as my terms. Hint: your denominator is wrong.


Ah thanks! Now I get what you were saying. I was multiplying factorials together in my program before. I've fixed it and it works.
Comments on this post
Lux Perpetua agrees!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > What am I doing wrong?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap