### Thread: What am I doing wrong?

1. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2012
Posts
42
Rep Power
9

#### 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);
}```
2. No Profile Picture
Contributing User
Devshed Intermediate (1500 - 1999 posts)

Join Date
Feb 2004
Location
San Francisco Bay
Posts
1,939
Rep Power
1316
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);`
3. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2012
Posts
42
Rep Power
9
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);
}```
4. No Profile Picture
bdb
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2012
Posts
156
Rep Power
37
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
...
5. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2012
Posts
42
Rep Power
9
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.
6. No Profile Picture
bdb
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2012
Posts
156
Rep Power
37
term is either positive or negative.
When it is negative your while loop terminates
7. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2012
Posts
42
Rep Power
9
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.
8. No Profile Picture
bdb
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2012
Posts
156
Rep Power
37
I think maybe you want fabs() (declared in <math.h>) ... abs() (declared in <stdlib.h>) accepts and returns values of type int.
9. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2012
Posts
42
Rep Power
9
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.
10. No Profile Picture
Contributing User
Devshed Intermediate (1500 - 1999 posts)

Join Date
Feb 2004
Location
San Francisco Bay
Posts
1,939
Rep Power
1316
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.
11. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2012
Posts
42
Rep Power
9
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.