# Cos func.

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-15-2010
omGeeK
Cos func.
Okay so this is for homework and i am stuck, we have to write our own cos func. I understand how to set every thing up and i was given the formula that

cos(x)=(-1^n)*((x^2n)/(2n)!)

this is what i have set up so far. any help would be great.

Code:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>

float cosd(float);

void main() // shell to test the cosd() function
{            // This program is a poor example of programming.
// It is only for testing another function, in
// this case the cosd() function.

do
{
printf("Enter angle (in degrees): ");
scanf("%f",&angle);
} while (angle != -99.0); // -99 is sentinel value
}
// cosd() function goes here
float cosd(float d)
{
float r,term,sum,result;
r=(d)*(3.14159265/180);
term=1;
while(term > 0.0005)
{
sum=((r*r)/((term*2)*(2*term-1)))*(term-1);
}
return sum;
}

I mean i got the function to work by doing

Code:

float cosd(float d)
{
float r,sum;
r=(d)*(3.14159265/180);
sum= 1-((pow(r,2)/2))+(pow(r,4)/(4*3*2))-(pow(r,6)/(6*5*4*3*2))+(pow(r,8)/(8*7*6*5*4*3*2))-(pow(r,10)/(10*9*8*7*6*5*4*3*2))+(pow(r,12)/(12*11*10*9*8*7*6*5*4*3*2))-(pow(r,14)/(14*13*12*11*10*9*8*7*6*5*4*3*2));
return sum;
}

but my professor wants an algorithm that generates a term based on the previous term and i just don't see how to do it, yet.
• 10-15-2010
Char*Pntr
Quote:

Originally Posted by omGeeK
Okay so this is for homework and i am stuck, we have to write our own cos func. I understand how to set every thing up and i have the formula that

cos(x)=(-1^n)*((x^2n)/(2n)!)

this is what i have set up so far. any help would be great.

Code:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>

float cosd(float);

void main() // shell to test the cosd() function
{            // This program is a poor example of programming.
// It is only for testing another function, in
// this case the cosd() function.

do
{
printf("Enter angle (in degrees): ");
scanf("%f",&angle);
} while (angle != -99.0); // -99 is sentinel value
}
// cosd() function goes here
float cosd(float d)
{
float r,term,sum,result; <--- result is not being used
r=(d)*(3.14159265/180);
term=1;
while(term > 0.0005)
{
sum=((r*r)/((term*2)*(2*term-1)))*(term-1);
}
return sum;
}

For projects like this, I'd just go ahead and use doubles. In my case, my math function
expects doubles anyway.

In red above, I don't see the variable term being changed, hence it's locked in
a forever loop.

I haven't checked the rest of your code.
• 10-15-2010
omGeeK
yea i understand why i should be using doubles, but its what my professor wanted :). And i understand that its not changing its just that to be honest we were given some pointers and term was one of them and i dont really understand it. I mean i got the function to work by doing

Code:

float cosd(float d)
{
float r,sum;
r=(d)*(3.14159265/180);
sum= 1-((pow(r,2)/2))+(pow(r,4)/(4*3*2))-(pow(r,6)/(6*5*4*3*2))+(pow(r,8)/(8*7*6*5*4*3*2))-(pow(r,10)/(10*9*8*7*6*5*4*3*2))+(pow(r,12)/(12*11*10*9*8*7*6*5*4*3*2))-(pow(r,14)/(14*13*12*11*10*9*8*7*6*5*4*3*2));
return sum;
}

but my professor wants an algorithm that generates a term based on the previous term and i just don't see how to do it, yet.
• 10-15-2010
omGeeK
i think if i use a while loop and use the equation x^2/2! somehow it should work, any thoughts anyone?
• 10-16-2010

cos(x)=(-1^n)*((x^2n)/(2n)!)

Do you mean

cos(x)=[Summation from i = 0 to n] (-1^i)*((x^2i)/(2i)!)

Or is it a recurrence relation? Please state it a little more precisely.
• 10-16-2010
itsme86
Do you mean he wants you to write your own factorial function maybe? A simple one might be:
Code:

int factorial(int num)
{
int rv = 1;
while(num > 1)
rv *= num--;
return rv;
}

Then you could just do factorial(5) instead of (5*4*3*2).
• 10-16-2010
Quote:

Originally Posted by itsme86
Do you mean he wants you to write your own factorial function maybe?

I am pretty certain that the subject of this thread is the cosine function, not the factorial function. I could be wrong however.
• 10-16-2010
omGeeK
Yea i basically am rewriting the cos func with my own code.
• 10-16-2010
omGeeK
Quote:

cos(x)=(-1^n)*((x^2n)/(2n)!)

Do you mean

cos(x)=[Summation from i = 0 to n] (-1^i)*((x^2i)/(2i)!)

Or is it a recurrence relation? Please state it a little more precisely.

Well the cosine function is an infinite function so i would think it is recurring.
• 10-16-2010
Quote:

Originally Posted by omGeeK
Well the cosine function is an infinite function so i would think it is recurring.

Or an infinite summation. I am just wondering what it is you posted.
• 10-16-2010
omGeeK
well the cosine of an angle is 1-(x^2/2!)+(x^4/4!)-(x^6/6!)+(x^8/8!)................ going on forever but i only need the accuracy of 7 terms.
• 10-16-2010
ssharish2005
Quote:

I am pretty certain that the subject of this thread is the cosine function, not the factorial function. I could be wrong however.

The reason he might have pointed out is because the formula requires to find the factorial

Code:

cos(x)=[Summation from i = 0 to n] (-1^i)*((x^2i)/(2i)!)
ssharish
• 10-16-2010
Quote:

Originally Posted by omGeeK
well the cosine of an angle is 1-(x^2/2!)+(x^4/4!)-(x^6/6!)+(x^8/8!)................ going on forever but i only need the accuracy of 7 terms.

I know that, but you said

Quote:

Originally Posted by omGeeK
but my professor wants an algorithm that generates a term based on the previous term and i just don't see how to do it, yet.

...leading me to think your professor wants you to code up a recurrence relation. I know of no such relation.
• 10-16-2010
omGeeK
haha well the homework page states that we should generate a term from a previous term and it gives us the formula which i gave earlier
Quote:

1-(x^2/2!)+(x^4/4!)-(x^6/6!)+(x^8/8!)...
this is what i was given and i am having a hard time understanding it.
• 10-16-2010
tty0
Code:

sum= 1-((pow(r,2)/2))+(pow(r,4)/(4*3*2))-(pow(r,6)/(6*5*4*3*2))+(pow(r,8)/(8*7*6*5*4*3*2))-(pow(r,10)/(10*9*8*7*6*5*4*3*2))+(pow(r,12)/(12*11*10*9*8*7*6*5*4*3*2))-(pow(r,14)/(14*13*12*11*10*9*8*7*6*5*4*3*2));
since the above worked correctly, all you need to do is write a recursive function that computes the cos to the needed accuracy i.e. 7
along with that you need to write a factorial function.
Code:

int num=1;
int accuracy=7;//changing to even may mess up recursion
double sum=0;
while(accuracy!=-1)//need the extra loop to count the 1
{
sum+=pow(-1,(2*(accuracy%2)))*(pow(r,num)/fact(num));//fact is the factorial function
if(num==1){sum=1;}//another quick fix, too lazy to count :P
accuracy--;
num+=2;
if(num==3){num--;}//quick fix
}

this is untested code, and i myself am just a first year cs student, sooo... mistakes are very probable))
also to the purists out there: i know the if checks are bad coding style, but for demo purposes everything goes a'ight?)
P.S. first post :D
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last