sin x

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
41
42
43
44
45
46
float k,j,p=1,s=0;int i;
void evaluate(float);
main()
{
int n,j;float b;
clrscr();
printf("\n\nenter the value of x to be fed in the function sin(x) ");
scanf("%f",&b);
evaluate(b);
getch();
}
void evaluate(float b)
{
for(i=0;i<=100000;i++)
{
k=pow(-1,i);
for(j=1;j<=(2*i+1);j++)
{
p*=b/j;
}
if(i==1)
{
printf("%f",p);
}
if(i%2==0)
s+=p;
else
s+=-p;
}
printf("sin (%f)=%f ",b,s);
}
/*            0


float a[6],s;int i;
printf("enter coefficient 1*x^5 ");
scanf("%f",&a[0]);
printf("enter coefficient 2*x^4 ");
scanf("%f",&a[1]);
s=a[0]*b+a[1];
for(i=2;i<=5;i++)
{
printf("enter the coefficient %d*x^%d ",i+1,5-i);
scanf("%f",&a[i]);
s=s*b+a[i];
} */

after including header files the sin x va;lue int coming to the required level of accuracy.....
wats the error.......plz figure out.....
The first trick is to tidy the code.
1. k was defined and set, but never used. Let's just remove it
2. j is defined as a float universally and then as an int locally in main, remove that and only define locally. Since you only use it in the function, just define it there.
3. In c++ we ussually prefer to use cin or cout instead of printf and scanf.
4. void evaluate (float b) only outputs a printf, it would be much more useful if it returned our result. Make it a float function instead and return b.
5. You don't need the printf in line 23, so to make it readable lets remove it.
6. The last 14 lines of commented code is irrelevant, lets remove it.
7. Indents!

We are left with this: Much easier to read and therefore it will be easier for you to spot your problem:

1
2
3
4
5
6
7
8
9
10
11
12
13
float sine ( float x )
{
	float p=1, s=0;
	for (int i=0;i<=100000; i++)
	{
		for (int j = 1; j<=(2*i+1); j++)
			p *= x/(float)j;
		
		if (i%2==0)     s += p;
		else		s -= p;
	}
	return s;
}
Oh, I assume you are trying to calculate this using the Taylor Series:

sin(x) = (x^1)/1! - (x^3)/3! + (x^5)/5! - (x^7)/7! ...

Let me try and work out how I would do this.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
float sine ( float x )
{
	float sum = 0;
	for (int i=0;i<=100000; i++)
	{
		if (i%2 == 0) continue;          // Skip all even numbers
		
		float factoral = 1;              // Make this a float to avoid integer division
		for (float j = i ; j > 0 ; j--)  // Calculate the factoral
			factoral *= j;
		
		sum += (x^i)/factoral * ((i%4)-2)*-1) // The last term makes our component -ve if i%4==3 
	}                                         // so that terms 3, 7, ... are -ve and 1, 5, 9... are +ve
	return sum;
}
Last edited on
Ah ha, thought of another improvement.

for (int i = 1;i<100000;i++) seems rather excessive, how about we continue until the next term is within our required precision? You can hard-code this precision or pass it as an argument. In this case, I've hard coded to precision to 10 decimal places.

To technically answer your poorly spelled question "wats the error", the error is equal to the final value of term.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
double sine ( double x )
{
	double sum = 0., precision = 1.0e-10, term = 1.;
	for (int i=1;abs(term)>precision; i++)
	{
		if (i%2 == 0) continue;     
		
		double factorial = 1;            
		for (double j = i ; j > 0 ; j--) 
			factorial *= j;
		
		term = pow(x,i)/factoral * ((i%4)-2)*-1);
		sum += term;
	}
	return sum;
}
Last edited on
Topic archived. No new replies allowed.