October 31st, 2012, 09:58 PM
-
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
-
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
-
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
-
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
-
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
-
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
-
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
-
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