cosx sinx 泰勒展开 C++
常见的泰勒展开:
void tylor_sin(int n){double s=n;double i=1;double term=n;do{term=-term*n*n/((i+2)*(i+1));s+=term;i+=2;}while(fabs(term)>1e-6);cout<<"the value of sin"<<n<<" is :"<<s<<endl;}void tylor_cos(int n){double s=1;double term=1;int i=0;do{term=-term*n*n/((i+1)*(i+2));s+=term;i+=2;}while(fabs(term)>1e-6);cout<<"the value of cos"<<n<<" is :"<<s<<endl;}int main(){tylor_cos(10);tylor_sin(10);return 0;}
结果如下:
思考:
用迭代法累加得到,要注意正负号和分母的阶乘就很简单了。