Cosine Series help


I think i am getting a wrong output. Can you please help me by checking this code and by verifying if it is valid. Thanks to all in advance. :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//c++ program to compute cosine series cos(x)= 1-(x^2/2!)+(x^3/3!)-........
#include<iostream>
#include<cmath>

using namespace std;

long fact(long);


int main()
{
	long a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,no;
	int sign=1;
	cout<<"Input the value of x"<<endl;
	cin>>a;
	
	cout<<"input the no. of terms"<<endl;
	cin>>n;
	
	a=a*(3.14)/180;
	b=2;
	c=1;
	s=1;
	for(i=2;i<=n;++i)
	{
		
		s=s+(pow(a,i)/fact(i))*sign;
		sign=sign*(-1);
		
	}
	cout<<"sum of the series is"<<s;
	return 0;
}
long fact(long i)
{
	int j,f=1;
	for(j=1;j<=i;j++)
	f=f*1;
	return(f);
}
cant you pass the same input value into cos() function temporarily and see if it returns the same number?
http://www.cplusplus.com/reference/cmath/cos/

and you should give your variables sensible names to help debugging.

some obvious things:
 
	f=f*1;

that's not doing anything.

for(i=2;i<=n;++i)
why these conditions?

I think you want to keep a running sum as you calculate each term. you don't need to keep flipping the sign then.
Last edited on
Look the cosine series is:
cos(x)=1-((x^2)/2!)+((x^3)/3!))-((x^4)/4!)..........

So i called a function to find the factorial
That's the use of
1
2
3
4
5
6
7
long fact(long i)
{
	int j,f=1;
	for(j=1;j<=i;j++)
	f=f*1;
	return(f);
}

for(i=2;i<=n;++i)

this is used for the factorials and the powers of the series.

i don't get my mistake.
Sorry for the weird variables. It is a bad habit that i shall have to mend.
Last edited on
I know what the cosine series expansion looks like.

You're loop is actually making sense to me now sorry.

But, i'll say this again:

f=f*1;
Do you know what happens if you multiply a number by 1? It does not change. Therefore the number you are passing in has no effect at all on the return value. The return value will ALWAYS be 1.

Effectively you are calculating this series:
cos(x)= 1-(x^2)+(x^3)...





Last edited on
Thank you, i got my problem. I did not notice it but the 1 was meant to be j. Here is the proper completed code. Sorry, but i know that 1*x=x just that it was a type error, thank you very much indeed, i am really grateful that you pointed it out.

I hope it is correct now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<iostream>
#include<cmath>

using namespace std;

long fact(float);


int main()
{
	float a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,no;
	int sign=1;
	cout<<"Input the value of x"<<endl;
	cin>>a;
	
	cout<<"input the no. of terms"<<endl;
	cin>>n;
	
	a=a*(3.14)/180;
	b=2;
	c=1;
	s=1;
	for(i=2;i<=n;++i)
	{
		
		s=s+(pow(a,i)/fact(i))*sign;
		sign=sign*(-1);
		
	}
	cout<<"sum of the series is"<<s;
	return 0;
}
long fact(float i)
{
	int j,f=1;
	for(j=1;j<=i;j++)
	f=f*j;
	return(f);
}

It always seems a mistaken approach to use the pow() function in the calculation of the series for cosine. Why? Because pow() internally will calculate the logarithm (by means of a series), multiply by the power, then calculate the antilogarithm (by means of another series). Thus what could be achieved by a single multiplication is replaced by a hugely complex process. I'd recommend not using the <cmath> library at all - except perhaps to verify the correctness of the results.
Topic archived. No new replies allowed.