Calculating Trigonometric Functions

Discuss Computer Science and Programming related problems

Moderators:Labib, bristy1588

User avatar
amlansaha
Posts:100
Joined:Tue Feb 08, 2011 1:11 pm
Location:Khulna, Bangladesh
Contact:
Calculating Trigonometric Functions

Unread post by amlansaha » Sat Jan 07, 2012 11:43 pm

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
অম্লান সাহা

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 12:17 am

: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
হার জিত চিরদিন থাকবেই
তবুও এগিয়ে যেতে হবে.........
বাধা-বিঘ্ন না পেরিয়ে
বড় হয়েছে কে কবে.........

User avatar
amlansaha
Posts:100
Joined:Tue Feb 08, 2011 1:11 pm
Location:Khulna, Bangladesh
Contact:

Re: Calculating Trigonometric Functions

Unread post by amlansaha » Sun Jan 08, 2012 1:12 am

জোত্যিরে তোরে বিশাল ঝাপ্পি
:D :D :D


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

User avatar
Tahmid Hasan
Posts:665
Joined:Thu Dec 09, 2010 5:34 pm
Location:Khulna,Bangladesh.

Re: Calculating Trigonometric Functions

Unread post by Tahmid Hasan » Sun Jan 08, 2012 10:29 am

দেখে যা মনে হচ্ছে for loop দিয়ে করা যায় বোধহয়।
বড় ভালবাসি তোমায়,মা

User avatar
Labib
Posts:411
Joined:Thu Dec 09, 2010 10:58 pm
Location:Dhaka, Bangladesh.

Re: Calculating Trigonometric Functions

Unread post by Labib » Sun Jan 08, 2012 3:24 pm

math.h ছাড়া ম্যাক্লরিনের ধারা কেমনে লিখে??
Please Install $L^AT_EX$ fonts in your PC for better looking equations,
Learn how to write equations, and don't forget to read Forum Guide and Rules.


"When you have eliminated the impossible, whatever remains, however improbable, must be the truth." - Sherlock Holmes

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 3:33 pm

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

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 5:00 pm

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; এখানে += কি বুঝলাম না ??? একই ভাবে *= কি :?:
আমি প্রোগ্রামিং জগতে নতুন সব ফাংশন জানি না । :( :(
হার জিত চিরদিন থাকবেই
তবুও এগিয়ে যেতে হবে.........
বাধা-বিঘ্ন না পেরিয়ে
বড় হয়েছে কে কবে.........

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 9:27 pm

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

User avatar
Nadim Ul Abrar
Posts:244
Joined:Sat May 07, 2011 12:36 pm
Location:B.A.R.D , kotbari , Comilla

Re: Calculating Trigonometric Functions

Unread post by Nadim Ul Abrar » Sun Jan 08, 2012 9:47 pm

মাথার উপর ছাদ । ছাদের উপর আমগাছ । আমগাছের উপর দিয়া যাইতাসে :? :( :cry:
$\frac{1}{0}$

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 10:27 pm

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

Post Reply