Calculating Trigonometric Functions
Moderators:Labib, bristy1588
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
Please write the alogarithm or the logic. If u code in C/C++, skip using the header file math.h
অম্লান সাহা
Re: Calculating Trigonometric Functions
আমি নিজে চেষ্টা করে দেখি পারি কিনা। তবে একটা আইডিয়া আসছে, "ম্যাকলরিনের ধারা।"
$sinx=x-\frac{x^3}{3!}+\frac{x^5}{5!}-...............................$
হার জিত চিরদিন থাকবেই
তবুও এগিয়ে যেতে হবে.........
বাধা-বিঘ্ন না পেরিয়ে
বড় হয়েছে কে কবে.........
তবুও এগিয়ে যেতে হবে.........
বাধা-বিঘ্ন না পেরিয়ে
বড় হয়েছে কে কবে.........
Re: Calculating Trigonometric Functions
জোত্যিরে তোরে বিশাল ঝাপ্পি
আমি আসলেই একটা গর্দভ। এইডা যে কেমনে ভুইলা গেলাম :'( :'( :'(
আমি আসলেই একটা গর্দভ। এইডা যে কেমনে ভুইলা গেলাম :'( :'( :'(
অম্লান সাহা
- Tahmid Hasan
- Posts:665
- Joined:Thu Dec 09, 2010 5:34 pm
- Location:Khulna,Bangladesh.
Re: Calculating Trigonometric Functions
দেখে যা মনে হচ্ছে for loop দিয়ে করা যায় বোধহয়।
বড় ভালবাসি তোমায়,মা
Re: Calculating Trigonometric Functions
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
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
-
- Posts:6
- Joined:Sun Jan 08, 2012 11:43 am
Re: Calculating Trigonometric Functions
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
আগে একটা ফাংশন বের করতে হবে যেটায় variable পাওয়ার যা, সেটার ফ্যাক্টরিয়াল দিয়ে ভাগ করা হয়।math.h ছাড়া ম্যাক্লরিনের ধারা কেমনে লিখে??
এবার loop ব্যবহার করে ধারার যোগফল বের করতে হবে।
দেখে যা মনে হচ্ছে for loop দিয়ে করা যায় বোধহয়।
আল্গারিদম লিখতে পারলে যেটা খুশি সেটা দিয়েই করা যায়।
এর আগা মাথা কিছু বুঝলাম না। কম্পাইল করতে গেলে কম্পাইলার অনেক রকম error দেখায়।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;
}
আর res += (sign * num) / denom; এখানে += কি বুঝলাম না ??? একই ভাবে *= কি
আমি প্রোগ্রামিং জগতে নতুন সব ফাংশন জানি না ।
হার জিত চিরদিন থাকবেই
তবুও এগিয়ে যেতে হবে.........
বাধা-বিঘ্ন না পেরিয়ে
বড় হয়েছে কে কবে.........
তবুও এগিয়ে যেতে হবে.........
বাধা-বিঘ্ন না পেরিয়ে
বড় হয়েছে কে কবে.........
-
- Posts:6
- Joined:Sun Jan 08, 2012 11:43 am
Re: Calculating Trigonometric Functions
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;
}
- Nadim Ul Abrar
- Posts:244
- Joined:Sat May 07, 2011 12:36 pm
- Location:B.A.R.D , kotbari , Comilla
Re: Calculating Trigonometric Functions
মাথার উপর ছাদ । ছাদের উপর আমগাছ । আমগাছের উপর দিয়া যাইতাসে
$\frac{1}{0}$
-
- Posts:6
- Joined:Sun Jan 08, 2012 11:43 am
Re: Calculating Trigonometric Functions
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;
}