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

#1
October 31st, 2012, 09:58 PM
 Astrodude
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);
}```

#2
October 31st, 2012, 11:06 PM
 Lux Perpetua
Contributing User

Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,938
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);`

#3
November 1st, 2012, 01:45 PM
 Astrodude
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);
}```

#4
November 1st, 2012, 02:03 PM
 bdb
Contributing User

Join Date: Aug 2012
Posts: 156
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
...

#5
November 1st, 2012, 04:50 PM
 Astrodude
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 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
November 1st, 2012, 05:07 PM
 bdb
Contributing User

Join Date: Aug 2012
Posts: 156
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

#7
November 1st, 2012, 05:43 PM
 Astrodude
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.

#8
November 1st, 2012, 05:48 PM
 bdb
Contributing User

Join Date: Aug 2012
Posts: 156
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.

#9
November 1st, 2012, 06:48 PM
 Astrodude
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 ) ... abs() (declared in ) accepts and returns values of type int.

I switched to fabs and I got the same results.

#10
November 1st, 2012, 07:14 PM
 Lux Perpetua
Contributing User

Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,938
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.

#11
November 1st, 2012, 07:51 PM
 Astrodude
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.
Lux Perpetua agrees!

 Viewing: Dev Shed Forums > Programming Languages > C Programming > What am I doing wrong?