hercool
جمعه 20 فروردین 1389, 14:04 عصر
سلام خدمت دوستان
من می خوام بسط تیلورم تا این اندازه جلو بره یعنی می خوام یک while بزارم تا اندازه 0.0001
حالا می خوام این شرط حلقه رو بزارم داخل این کد اما نمی تونم از بچه ها می خوام که راهنمایی کنن
ممنون
#include <iostream>
#include <cmath>
using namespace std;
//feel free to implement this w/ recursion
double factorial(int num){
double fact = 1;
for(int i = 1; i <= num; i++){
fact *= i;
}
return fact;
}
//Taylor series sin(x) = (-1)^n*x^(2n+1)/(2n+1)!
//any precision less then 19 and your values will start to skew
double taylorSine(double num, double precision = 19){
double value = 0;
for(int n = 0; n < precision; n++){
value += pow(-1.0, n) * pow(num, 2*n+1) / factorial(2*n + 1);
}
return value;
}
/*
What you'll see:
0.841471 0.841471
0.909297 0.909297
0.14112 0.14112
-0.756802 -0.756802
-0.958924 -0.958924
-0.279415 -0.279415
0.656987 0.656987
0.989358 0.989358
0.412118 0.412118
-0.544021 -0.544021
*/
int main(){
for(int i = 1; i <= 10; i++){
//math.h's sine my sine
cout << sin((double) i) << " \t\t\t" << taylorSine((double)i) << endl;
}
return 0;
}
من می خوام بسط تیلورم تا این اندازه جلو بره یعنی می خوام یک while بزارم تا اندازه 0.0001
حالا می خوام این شرط حلقه رو بزارم داخل این کد اما نمی تونم از بچه ها می خوام که راهنمایی کنن
ممنون
#include <iostream>
#include <cmath>
using namespace std;
//feel free to implement this w/ recursion
double factorial(int num){
double fact = 1;
for(int i = 1; i <= num; i++){
fact *= i;
}
return fact;
}
//Taylor series sin(x) = (-1)^n*x^(2n+1)/(2n+1)!
//any precision less then 19 and your values will start to skew
double taylorSine(double num, double precision = 19){
double value = 0;
for(int n = 0; n < precision; n++){
value += pow(-1.0, n) * pow(num, 2*n+1) / factorial(2*n + 1);
}
return value;
}
/*
What you'll see:
0.841471 0.841471
0.909297 0.909297
0.14112 0.14112
-0.756802 -0.756802
-0.958924 -0.958924
-0.279415 -0.279415
0.656987 0.656987
0.989358 0.989358
0.412118 0.412118
-0.544021 -0.544021
*/
int main(){
for(int i = 1; i <= 10; i++){
//math.h's sine my sine
cout << sin((double) i) << " \t\t\t" << taylorSine((double)i) << endl;
}
return 0;
}