Title: A program for cos x
Gustavo - October 12, 2003 10:04 PM (GMT)
Yes, I am having some trouble with a program I need to write for the cos x. This has to do with factorials and whatnot. The formula for cos x is: 1/0! - x^2/2! + x^4/4! - x^6/6! ... And so what I need to do is figure out how to put that into code. I came up with the code to find the factorial of a number, but can't figure out how to wite the cos x.
This is what I have so far:
| CODE |
#include <iostream>
using namespace std;
double factorial(double n); double cos(double n);
int main() { double num; double answer;
cout << "This program will return cos and factorial of a number.\n"; cout << "Enter a number: "; cin >> num;
answer = factorial(num);
cout << "Factorial of " << num << " is " << answer << '\n';
return 0; }
double factorial(double n) { double ans;
if(n==1) return 1; ans = factorial(n-1)*n; return ans; }
double cos(double n) { double cos_x;
do { 1/0(ans) - n * n / 2(ans), n+2, ans+2; }while (n != 0); }
... } |
I don't really know if that is right to get the cos x. I just maybe figured it would be from what I got for getting the factorial. Could I get some suggestions, ideas, help, anything along those lines? And if this helps out, the answer needs to be with 6 digit placement.
TheHawgMaster - October 13, 2003 11:58 PM (GMT)
Look into the functions prototyped in the standard C header "math.h" They include sin, cos, tan, hsin, hcos, htan, asin, acos, atan, atan2, etc...
Gustavo - October 17, 2003 12:50 AM (GMT)
Yes, that would work. But I have to write the program using those actual formulas... not just the math library. Sucks don't it. But I don't even know how to set those formulas up. Can I get help?
MonkeyMan - October 17, 2003 01:36 AM (GMT)
I cant help here... all i am learning in math right now is 5x+11=24x-12 :D
TheHawgMaster - October 17, 2003 02:59 AM (GMT)
I don't know the formula and you have a division by zero in the formula you put up. Could you please correct it so that I can make what you want?
Hmm MonkeyMan...
To
To
To
Yep. I remember algebra :D
Dragon - November 22, 2003 02:36 AM (GMT)
| QUOTE (MonkeyMan @ Oct 16 2003, 06:36 PM) |
| I cant help here... all i am learning in math right now is 5x+11=24x-12 :D |
You're not very good at math, are you? :lol:
CodeDesaster - November 27, 2003 02:52 PM (GMT)
Hi I read this Post today. Well its some time gone from your question. But here a hint maybe it helps.
I reduced the factorial to an integer, normally thats what you need to expand a function into a series like you would like with the cos(x). Factorials for real numbers are a little bit more complicated :-) Look here
http://mathworld.wolfram.com/Factorial.htmlI also includes the power function and cosine to be completely independant from the math library.
| CODE |
int factorial(int _n) { int result = 1; if(_n == 0) return 1; else { for(int i = 1; i<=_n; i++) { result *= i; } return result; } } double power(double _x, int _n) { if(_n == 0) return 1; else { double x = _x; // helps when x is negative for(int i=1; i < _n; i++) { _x *= x; } return _x; } }
double cosine(double _x, int _n) { double cosine = 0.; for(int i=0; i < _n; i++) { cosine += power(-1.,i)*power(_x, i*2)/factorial(i*2); } return cosine; }
|
FrozenKnight - December 25, 2003 08:50 AM (GMT)
LOL, ok now get your power function to do fractional powers like 1/2 (.5 or SquareRoot)
myork - January 21, 2004 09:54 PM (GMT)
Ok.
After tracking down the correct formula for cos(x)
| QUOTE |
| cos(x) = 1 - ((x^2)/2!) + ((x^4)/4!) - ((x^6)/6!) + ((x^8)/8!) - ..... |
The following should provide a good approximate:
| CODE |
#define ACCURACY 0.0000001
#include <math.h> #include <iostream>
/* * Factorial is easy. * But the size of the number gets very big very fast. * So I amusing a double to make sure we can hold it. */ double fac(double x) { return((x == 0)?1:x * fac(x-1)); }
/* * The formula for cos(x) is 1 - ((x^2)/2!) + ((x^4)/4!) - ((x^6)/6!) + ((x^8)/8!) .... */ double mycos(double angle) { double result = 1; double lastResult = result; bool finished = false; int loop = 2;
while(!finished) { // Now loop until we get to a point where the accuracy is good enough.
lastResult = result;
double tmp1 = pow(angle,loop)/fac(loop); double tmp2 = pow(angle,loop+2)/fac(loop+2); result = result - tmp1 + tmp2;
loop = loop + 4; finished = (fabs(result - lastResult) < ACCURACY); }
return(result); }
int main(int argc,char* argv[]) { double angle = 3;
std::cout << "Cos(" << angle << ") = " << mycos(angle) << " Should be (" << cos(angle) << ")\n"; }
|
FrozenKnight - January 22, 2004 01:53 PM (GMT)
Hmm i looked it up and i found a resource that using a little math i was able to translate
the formula for cos x is sqrt(4-((x*4)/90)))/2 or in c/c++
| CODE |
double cos (double x) { return pow(4-((x*4)/90), 0.5) >> 1; } |
however, this formula conflicts with the math.h version which seems to return incorrect values (or values for a level of math which is above what i am currently studying).

Sorry i didnt quire understand. i'm not fully versed in this arae af math yet :unsure: hope you find that usefull though.
TheHawgMaster - January 22, 2004 04:20 PM (GMT)
:huh: The math.h version works fine. I've used it in many programs.
myork - January 22, 2004 06:28 PM (GMT)
Hi FK,
Don't mean to be picky but your formula is a rough approximation.
Problem 1:
It will not work for angeles greater than 90 degrees.
Problem 2:
It wont work for negative numbers.
FrozenKnight - January 23, 2004 01:05 AM (GMT)
ya it was an aproximation i fomulated it just before i went to sleep last night. sorry
CodeDesaster - March 12, 2004 01:29 PM (GMT)
Didn't know this thread was moved. :)
@myork: what you did is an approximation too.
It is true only for small values of x around 0 (and multiples of 2*Pi).
And FrozenKnight, yes you are right, but if you read my previous post you will see it is not meant to caclulate fracts of floats, neither is the shorter recursive routine of myork.
myork - March 12, 2004 02:17 PM (GMT)
I though the mathamatical definition of cos(x) was
1 - ((x^2)/2!) + ((x^4)/4!) - ((x^6)/6!) + ((x^8)/8!) ....
NB. The .... means repeat forever.
But hey, its nearly 15 years since I did A Level maths.
KTC - March 12, 2004 08:43 PM (GMT)
| QUOTE |
I though the mathamatical definition of cos(x) was 1 - ((x^2)/2!) + ((x^4)/4!) - ((x^6)/6!) + ((x^8)/8!) .... |
Yeah well, that's the Taylor-MacLaurin expansion of cos(x). You can use it to define cos(x) but it'll rely on some different area of maths which would rely on some different area of maths which might end up relying on cos(x) etc. etc. :rolleyes: Trig was initially defined with triangles & then to make it work beyond 2*Pi, they defined it using unit circle....
Oh about fractorial of decimals, I recall seeing somewhere in one of my uni maths lecture note about how to work it out. Don't really understand (or remember for that matter) it tho :unsure: .....
CodeDesaster - March 15, 2004 12:37 PM (GMT)
Hmm seems that I have been wrong. The MacLaurin expansion is really working for every x but the convergence is very slow so the practical use for numerical problems is not very high. Of course u can use it for small values but for larger values it is more effective to use the periodicity of sin/cos.