0

I am currently trying to calculate the sum of the Taylor series for the function x^(1/2) (this is how it looks in Wolfram: http://www.wolframalpha.com/input/?i=taylor+series+x%5E%281%2F2%29) And this is the formula needed: https://i.stack.imgur.com/KpCiy.png

I am having trouble calculating the binomial coefficient. I divided it into 2 parts (numerator and factorial in the denominator). The numerator always evaluates to 0 value (except the one time when it should be 1 by initialization).

EDIT: Changed int to doubles(also updated code here) and it worked nicely And edited code:

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

}

0 Answers0