Page 1 of 2

Calculating Trigonometric Functions

Posted: Sat Jan 07, 2012 11:43 pm
by amlansaha
Write a program to calculate any trigonometric functions(ie. $sin$,$cos$,$tan$ etc).
Please write the alogarithm or the logic. If u code in C/C++, skip using the header file math.h

Re: Calculating Trigonometric Functions

Posted: Sun Jan 08, 2012 12:17 am
by sm.joty
:idea: :idea: :idea: :idea: :idea: :idea: :idea: :idea: :idea: :idea: :idea: :idea: :idea: :idea: :idea: :idea:
আমি নিজে চেষ্টা করে দেখি পারি কিনা। তবে একটা আইডিয়া আসছে, "ম্যাকলরিনের ধারা।"
$sinx=x-\frac{x^3}{3!}+\frac{x^5}{5!}-...............................$
:D

Re: Calculating Trigonometric Functions

Posted: Sun Jan 08, 2012 1:12 am
by amlansaha
জোত্যিরে তোরে বিশাল ঝাপ্পি
:D :D :D


আমি আসলেই একটা গর্দভ। এইডা যে কেমনে ভুইলা গেলাম :'( :'( :'(

Re: Calculating Trigonometric Functions

Posted: Sun Jan 08, 2012 10:29 am
by Tahmid Hasan
দেখে যা মনে হচ্ছে for loop দিয়ে করা যায় বোধহয়।

Re: Calculating Trigonometric Functions

Posted: Sun Jan 08, 2012 3:24 pm
by Labib
math.h ছাড়া ম্যাক্লরিনের ধারা কেমনে লিখে??

Re: Calculating Trigonometric Functions

Posted: Sun Jan 08, 2012 3:33 pm
by sabbir.yousuf
Write something like -

Code: Select all

double sin(double x) {
    double res = 0;
    int sign = 1;
    double denom = 1;
    double num = x;
    for(i = 1; i <= 100; i+=2) {
        res += (sign * num) / denom;
        num *= (x*x);
        denom *= ((i+1)*(i+2));
        sign *= -1;
    }
    return res;
}

Re: Calculating Trigonometric Functions

Posted: Sun Jan 08, 2012 5:00 pm
by sm.joty
math.h ছাড়া ম্যাক্লরিনের ধারা কেমনে লিখে??
আগে একটা ফাংশন বের করতে হবে যেটায় variable পাওয়ার যা, সেটার ফ্যাক্টরিয়াল দিয়ে ভাগ করা হয়।
এবার loop ব্যবহার করে ধারার যোগফল বের করতে হবে। :D
দেখে যা মনে হচ্ছে for loop দিয়ে করা যায় বোধহয়।

আল্গারিদম লিখতে পারলে যেটা খুশি সেটা দিয়েই করা যায়। ;)
Write something like -

double sin(double x) {
double res = 0;
int sign = 1;
double denom = 1;
double num = x;
for(i = 1; i <= 100; i+=2) {
res += (sign * num) / denom;
num *= (x*x);
denom *= ((i+1)*(i+2));
sign *= -1;
}
return res;
}
এর আগা মাথা কিছু বুঝলাম না। কম্পাইল করতে গেলে কম্পাইলার অনেক রকম error দেখায়।
আর res += (sign * num) / denom; এখানে += কি বুঝলাম না ??? একই ভাবে *= কি :?:
আমি প্রোগ্রামিং জগতে নতুন সব ফাংশন জানি না । :( :(

Re: Calculating Trigonometric Functions

Posted: Sun Jan 08, 2012 9:27 pm
by sabbir.yousuf
Let me explain my code in a more detail

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;
}

Re: Calculating Trigonometric Functions

Posted: Sun Jan 08, 2012 9:47 pm
by Nadim Ul Abrar
মাথার উপর ছাদ । ছাদের উপর আমগাছ । আমগাছের উপর দিয়া যাইতাসে :? :( :cry:

Re: Calculating Trigonometric Functions

Posted: Sun Jan 08, 2012 10:27 pm
by sabbir.yousuf
Okay, here's a somewhat simpler and inefficient version -

Code: Select all


double factorial(int n) {
    double res = 1;
    for(int i = 1; i <=n; i++) {
        res = res * n;
    }
    return res;
}

double sin(double x) {
    double res = 0;
    int sign = 1;
    for(i = 1; i <= 100; i+=2) {
        double num = pow(x, i);
        double denom = factorial(i);
        res = res + ((sign * num) / denom);
        sign *= -1;
    }
    return res;
}