現在、関数 x^(1/2) のテイラー級数の合計を計算しようとしています (これは Wolfram でどのように見えるかです: http://www.wolframalpha.com/input/?i=taylor+series+ x%5E%281%2F2%29 ) そして、これが必要な式です: http://i.imgur.com/YvVOQIR.png
二項係数の計算に問題があります。これを 2 つの部分 (分子と分母の階乗) に分割しました。分子は常に 0 の値に評価されます (初期化によって 1 になるはずの 1 回を除く)。
編集: int を double に変更し (ここのコードも更新)、うまく機能し、コードを編集しました:
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
double x;
double e;
void taylor() // taylor series
{
double result = 0;
double previous = 1;
for(double n=0; abs(previous-result) > e;n++) //cycle for caltucalting to given precision
{
double binomial=1; // variable for binomial coef
double fact=1; //variable for factorial
double topbin=1; //variable for top part of binomial coef
previous=result;
for(double k=1;k<=n;k++)
{
fact *= k; //calcucating factorial
topbin *= ((1.0/2.0)-k+1); //calculating top part of binomial coef
}
binomial=double(topbin)/double(fact); //calculating whole binomial coef
result+=binomial*(pow((-1+x),n)); // calculating taylor series
cout<<"topbin "<<topbin<<endl;
cout<<"fact "<<fact<<endl;
cout<<"binomial "<<binomial<<endl;
cout<<"taylor sum on "<<n<<" iteration: "<<result<<endl;
cout<<endl;
}
cout<<"taylor sum:"<<result<<endl;
}
void main()
{
cout<<"Enter x"<<endl;
cin>>x;
cout<<"Enter e"<<endl;
cin>>e;
taylor();
double y=1/sqrt(pow(x,2)-1);
cout<<"y= "<<y<<endl;
}