Computer Science‎ > ‎

C Program: Computing exp(x), sin(x), cos(x), tan(x) using series expansions



C Program source code to solve, via series expansion, the functions exp(x),sin(x),cos(x),tan(x)


/*The following program demonstrates the values of series expansion of
exp(x),sine(x),cosine(x) and tan(x).*/
#include<stdio.h>
#include<math.h>
int factorial(int num)
{
    int ctr;
    int fact=1;//This function calculates and returns the factorial of number num
    for(ctr=1;ctr<=num;ctr++)
    fact=fact*ctr;
    return fact;
}
int main()
{
int terms;
int ctr;
printf("Enter the number of terms till which you want to calculate the value of expansion\n");
scanf("%d",&terms);//Enter the number of terms
double sum=0.0;
double x;
printf("Enter the value for x\n");
scanf("%lf",&x);
printf("1+x+x^2/2!+x^3/3!.......This series when expanded till infinity is expansion for exp(x)\n");
for(ctr=0;ctr<terms;ctr++)
sum+=pow(x,ctr)/factorial(ctr);//finding the value for sum appropriately
printf("The value from the expansion is %.14lf\n",sum);
sum=0.0;
printf("1-x^2/2!+x^4/4!.......This series when expanded till infinity is expansion for cosine(x)\n");
for(ctr=0;ctr/2<terms;ctr+=2)
sum=sum+pow(-1,ctr/2)*pow(x,ctr)/(factorial(ctr));
printf("The value from the expansion is %.14lf\n",sum);
sum=0.0;
printf("x-x^3/3!+x^5/5!.......This series when expanded till infinity is expansion for sine(x)\n");
for(ctr=1;(ctr-1)/2<terms;ctr+=2)
sum=sum+pow(-1,(ctr-1)/2)*pow(x,ctr)/(factorial(ctr));
printf("The value from the expansion is %.14lf\n",sum);
printf("x+x^3/3+2x^5/15.......This series when expanded till infinity is expansion for tan(x)\n");
sum=0.0;
double B,temp;
int Bn,k,r;
for(ctr=1;ctr<=terms;ctr+=1)
{//This loops here calculate Bernoulli number which is further used to get the coefficient in expansion of tan x
B=0;
Bn=2*ctr;
for(k=0;k<=Bn;k++)
{
    temp=0;
for(r=0;r<=k;r++)
{
    temp=temp+pow(-1,r)*factorial(k)*pow(r,Bn)/(factorial(r)*factorial(k-r));
}
B=B+temp/((double)(k+1));
}
sum=sum+pow(-4,ctr)*(1-pow(4,ctr))*B*pow(x,2*ctr-1)/factorial(2*ctr);
}
printf("The value from the expansion is %.14lf\n",sum);
getch();
}
/* A sample run of the program works as:-
Enter the number of terms till which you want to see
6
Enter the value for x
1
1+x+x^2/2!+x^3/3!.......This series when expanded till infinity is expansion for exp(x)
The value from the expansion is 2.71666666666667
1-x^2/2!+x^4/4!.......This series when expanded till infinity is expansion for cosine(x)
The value from the expansion is 0.54030230379189
x-x^3/3!+x^5/5!.......This series when expanded till infinity is expansion for sine(x)
The value from the expansion is 0.84147098464807
x+x^3/3+2x^5/15.......This series when expanded till infinity is expansion for tan(x)
The value from the expansion is 1.55137626113259
*/
Other Programs which you might find useful:


C Program to Reverse A String

C Program: Building an Expression Evaluator
C Program: Check for Armstrong Numbers
C Program: Check whether a string is a Palindrome or not
C Program: Common Operations on Sets - Union, Intersection, Difference, Cardinal Product
C Program: Computing exp(x), sin(x), cos(x), tan(x) using series expansions
C Program: Computing the Area of a Circle
C Program: Computing the Upper Triangular Matrix and Lower Triangular Matrix
C Program: Demonstrating File Handling Functions
C Program: Demonstrating Operations on Matrices - Addition, Subtraction, Multiplication, Inversion, Finding Determinants
C Program: Demonstrating the use of Bitwise Operators
C Program: Displaying a Histogram of word frequencies (unigram)
C Program: Distance Vector Routing Algorithm using Bellman Ford's Algorithm
C Program: Numerical Computing - The Gaussian Elimination Method
C Program: Numerical Computing - Implementing the Newton Raphson Method
C Program: Numerical Computing - the Bisection Method
C Program: Numerical Computing - The Gaussian Elimination Technique from Linear Algebra
C Program: Numerical Computing - the Jacobi Method
C Program: Printing the Pascal Triangle
C Program: Reversing the order of words in a sentence
C Program: Solving Simultaneous Equations in Two Variables
C Program: Source Code for computing the GCD(HFC) of two numbers
C Program: Source Code for Solving Quadratic Equations
C Program: Source code to solve the Josephus Problem
C Program: Sudoku Solver
C Program: The Usage of Command Line Arguments
C Program: Using the Sieve of Eratosthenes to print Prime Numbers


 







C Program to Reverse A String

C Program: Building an Expression Evaluator
C Program: Check for Armstrong Numbers
C Program: Check whether a string is a Palindrome or not
C Program: Common Operations on Sets - Union, Intersection, Difference, Cardinal Product
C Program: Computing exp(x), sin(x), cos(x), tan(x) using series expansions
C Program: Computing the Area of a Circle
C Program: Computing the Upper Triangular Matrix and Lower Triangular Matrix
C Program: Demonstrating File Handling Functions
C Program: Demonstrating Operations on Matrices - Addition, Subtraction, Multiplication, Inversion, Finding Determinants
C Program: Demonstrating the use of Bitwise Operators
C Program: Displaying a Histogram of word frequencies (unigram)
C Program: Distance Vector Routing Algorithm using Bellman Ford's Algorithm
C Program: Numerical Computing - The Gaussian Elimination Method
C Program: Numerical Computing - Implementing the Newton Raphson Method
C Program: Numerical Computing - the Bisection Method
C Program: Numerical Computing - The Gaussian Elimination Technique from Linear Algebra
C Program: Numerical Computing - the Jacobi Method
C Program: Printing the Pascal Triangle
C Program: Reversing the order of words in a sentence
C Program: Solving Simultaneous Equations in Two Variables
C Program: Source Code for computing the GCD(HFC) of two numbers
C Program: Source Code for Solving Quadratic Equations
C Program: Source code to solve the Josephus Problem
C Program: Sudoku Solver
C Program: The Usage of Command Line Arguments
C Program: Using the Sieve of Eratosthenes to print Prime Numbers