Calculating Trigonometric Functions

Discuss Computer Science and Programming related problems

Moderators: bristy1588, Labib

User avatar
sm.joty
Posts: 327
Joined: Thu Aug 18, 2011 12:42 am
Location: Dhaka

Re: Calculating Trigonometric Functions

Unread post by sm.joty » Sun Jan 08, 2012 10:37 pm

sabbir.yousuf wrote:Okay, here's a somewhat simpler and inefficient version -

Code: Select all

//we will write a function to calculate sine of a given value
//we will use MacLaurin Series for this purpose
//sinx = x - x^3 / 3! + x^5 / 5! - ......
double sin(double x) {
    double res = 0; //result variable
    int sign = 1; //sign of a particular term in the series, +/-
    double num = x; //num variable will denote the numerator of a particular term in the series
    double denom = 1; //denom variable will denote the denominator of a particular term in the series

    //it's not possible to calculate a infinite sum
    //it's not necessary either because later terms won't contribute much to the sum
    //so we will take first 50 odd terms
    for(int i = 1; i <= 100; i+=2) {
        res += (sign * num) / denom; // 'res += x' means 'res = res + x'
        num *= (x*x); //next numerator, note that each term is x^2 more than the previous one
        denom *= ((i+1)*(i+2)); //next denominator, note that i-th term should be (2*i+1)!
        sign *= -1; //sign alternates in successive terms
    }
    return res;
}
But bhaiya, C Compiler saying something like that,

In function 'sin':
error 'for' loop initial declaration are only allowed in C99 mod
note:use option -std=c99 or -std=gnu99 to compile your code
=== build finished: 1 errors, o warnings ===


i can't understand the first two line. :( :(
What can I do for compile it ???? :?:

(N.B: I used a main function also)
হার জিত চিরদিন থাকবেই
তবুও এগিয়ে যেতে হবে.........
বাধা-বিঘ্ন না পেরিয়ে
বড় হয়েছে কে কবে.........

sabbir.yousuf
Posts: 6
Joined: Sun Jan 08, 2012 11:43 am

Re: Calculating Trigonometric Functions

Unread post by sabbir.yousuf » Sun Jan 08, 2012 11:09 pm

Instead of declaring i in the for loop, you can declare i before. Like -

Code: Select all

int i;
for(i = 1; i <= 100; i+=2) {
}

User avatar
rakeen
Posts: 384
Joined: Thu Dec 09, 2010 5:21 pm
Location: Dhaka

Re: Calculating Trigonometric Functions

Unread post by rakeen » Mon Nov 12, 2012 1:38 pm

Code: Select all

#include <stdio.h>
int main()
{
   int i;
   double s=0,x,t=1;
   scanf("%lf", &x);
   for(i=1;i<=20;i++){
      s=s+t;
      t=t*((-x*x)/((i+1)*(i+2)));
   }
   printf("%lf",s);
   return 0;
   }
r@k€€/|/

User avatar
rakeen
Posts: 384
Joined: Thu Dec 09, 2010 5:21 pm
Location: Dhaka

Re: Calculating Trigonometric Functions

Unread post by rakeen » Mon Nov 12, 2012 1:49 pm

^ forgot to comment those...

x ia the value that we're gonna put in, and it must be in RADIAN.

As we won't be using math.h library thus we won't be using fact() function either. So, we are calculating the next term by multiplying a factor to the previous term, thus avoiding the factorial thing. You can also find the factorial of each denominator by using for loop. But it will show garbage if the value of $i$ gets a bit bigger.

In that program it will compute only upto 20 term. You can also write

Code: Select all

for(i=1;t<=0.000001;i++)
here the program will be out of the loop if the valure of $t$ gets too smaller(as it wont affect on the result of $sin(x)$).
r@k€€/|/

Post Reply