Saturday, 1 October 2011

C program to find the taylor series expansion of sin x,cos x and e^x


#include<stdio.h>
#include<math.h>
#define PI 3.1415

float exp_x( int, int );
double sin_x( int, int );
double cos_x( int, int );
int fact( int );

int main()
{
    int choice, x, n;

    do
        {
            printf( "\nMenu\n[1] e^x)\n[2] Sin(x)\n[3] Cos(x)\n[4] Exit\n" );
            scanf( "%d", &choice );

            switch ( choice )
                {

                case 1:  // to find exponential value
                    printf( "e^x\nEnter x and n:\t" );
                    scanf( "%d %d", &x, &n );
                    printf( "e^%d(%d) = %f\n", x, n, exp_x( x, n ) );
                    break;

                case 2:  // to find sinx
                    printf( "sin(x)\nEnter x and n:\t" );
                    scanf( "%d %d", &x, &n );
                    printf( "\ncos(%d)(%d) = %f\n", x, n, sin_x( x, n ) );
                    break;

                case 3:  // to find cosx
                    printf( "cos(x)\nEnter x and n:\t" );
                    scanf( "%d %d", &x, &n );
                    printf( "\ncos(%d)(%d) = %f\n", x, n, cos_x( x, n ) );
                    break;

                case 4:  // exit
                    break;

                default: // wrong choice
                    printf( "Wrong choice" );
                    break;
                }
}
    while ( choice != 4 );
}

float exp_x( int x, int n )
{
    int i = 1;
    float ex = 1;

    while ( i < n )
        {
            ex += ( float ) pow( x, i ) / fact( i );
            ++i;
        }

    return ex;
}

double sin_x( int ang_deg, int no_of_terms )
{
    int term, j;
    double value = 0.0, ang_rad = 0.0;
    ang_rad = ( double ) ang_deg * PI / 180;

    for ( term = 1, j = 2;term < no_of_terms*2;term += 2, j++ )
        {
            value += ( double ) pow( -1.0, j ) * pow( ang_rad, term ) / fact( term );
        }

    return value;
}

double cos_x( int ang_deg, int no_of_terms )
{
    int term, j;
    double value = 1.0, ang_rad = 0.0;
    ang_rad = ( double ) ang_deg * PI / 180;

    for ( term = 2, j = 1;term <= no_of_terms;term += 2, j++ )
        {
            value += ( double ) pow( -1.0, j ) * pow( ang_rad, term ) / fact( term );
        }

    return value;
}

int fact( int num )
{
    int f = 0;

    if ( num == 1 )
        return 1;
    else
f = num * fact( num - 1 );

    return f;
}



You Like It!? Then kindly share with your Friends.
Comments
1 Comments

1 comment:

abc said...

thanx alot for this program...

Post a Comment