The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
What am I doing wrong?
Discuss What am I doing wrong? in the C Programming forum on Dev Shed. What am I doing wrong? C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 31st, 2012, 09:58 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 22
  
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);
}
|

October 31st, 2012, 11:06 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: San Francisco Bay
|
|
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);
|

November 1st, 2012, 01:45 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 22
  
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);
}
|

November 1st, 2012, 02:03 PM
|
|
|
When you convert an angle to radians
and then, that value to int
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
...
|

November 1st, 2012, 04:50 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 22
  
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
and then, that value to int
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.
|

November 1st, 2012, 05:07 PM
|
|
|
|
term is either positive or negative.
When it is negative your while loop terminates
|

November 1st, 2012, 05:43 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 22
  
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.
|

November 1st, 2012, 05:48 PM
|
|
|
I think maybe you want fabs() (declared in <math.h>) ... abs() (declared in <stdlib.h>) accepts and returns values of type int.
|

November 1st, 2012, 06:48 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 22
  
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.
|

November 1st, 2012, 07:14 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: San Francisco Bay
|
|
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.
|

November 1st, 2012, 07:51 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 22
  
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|