Showing posts with label Taylor Series. Show all posts
Showing posts with label Taylor Series. Show all posts

Taylor Series - C Program

Program:
#include<stdio.h>
void sinx(float,int,int);
void cosx(float,int,int);
void epowerx(int,int);
void main()
{
//    clrscr();
    float x;
    int y,n;
    printf("enter limit: ");
    scanf("%d",&n);
    printf("enter the value in degree: ");
    scanf("%f",&x);
    y=x;
    x=(3.14/180)*x;
    sinx(x,n,y);
    cosx(x,n,y);
    epowerx(y,n);
}
void sinx(float x,int n,int y)
{
    float term=x,sum=x;
    int i;
    for(i=1;i<=n;i++)
    {
        term=((-term)*(x*x))/((2*i)*(2*i+1));
        sum=sum+term;
    }
    printf("sin %d=%f\n",y,sum);
}
void cosx(float x,int n,int y)
{
    float term=x,sum=1;
    int i;
    for(i=1;i<=n;i++)
    {
        term=((-term)*(x*x))/((2*i)*(2*i-1));
        sum=sum+term;
    }
    printf("cos %d=%f\n",y,sum);
}
void epowerx(int y,int n)
{
    int i;
    float term=1,sum=1;
    for(i=1;i<=n;i++)
    {
        term=((term)*y)/i;
        sum=sum+term;
    }
    printf("e^%d=%.0f\n",y,sum);
}

Output:
nn@linuxmint ~ $ gcc c25.c
nn@linuxmint ~ $ ./a.out
enter limit: 5
enter the value in degree: 30
sin 30=0.499770
cos 30=0.929956
e^30=241231
nn@linuxmint ~ $

Evaluation of eX Series - C Program

Program:
#include<stdio.h>
//#include<conio.h>
void ex(int y,int n)
{
    int i;
    float term=1,sum=1;
    for(i=1;i<=n;i++)
    {
        term=((term)*y)/(i);
        sum=sum+term;
    }
    printf("Sum of the ex series=%f\n",sum);
 }
void main()
{
     int n,y;
     float x;//clrscr();
     printf("Enter the no of terms:");
     scanf("%d",&n);
     printf("Enter the value:");
     scanf("%f",&x);
     ex(x,n);
    // getch();
}

Output:
nn@linuxmint ~ $ gcc c13.c
nn@linuxmint ~ $ ./a.out
Enter the no of terms:10
Enter the value:30
Sum of the ex series=238829632.000000
nn@linuxmint ~ $

Evaluation of CosX Series - C Program

Program:
#include<stdio.h>
//#include<conio.h>
void cosx(float x,int n)
{
    int i;
    float term=1,sum=1;
    for(i=1;i<=n;i++)
    {
        term=((-term)*(x*x))/((2*i)*(2*i-1));
        sum=sum+term;
    }
    printf("Sum of the cos series=%f\n",sum);
}
void main()
{
    int n,y;
    float x;//clrscr();
    printf("Enter the no of terms:");
    scanf("%d",&n);
    printf("enter the value in degree:");
    scanf("%f",&x);
    y=x;
    x=x*(3.14/180);
    cosx(x,n);
//      getch();
}

Output:
nn@linuxmint ~ $ gcc c12.c
nn@linuxmint ~ $ ./a.out
Enter the no of terms:10
enter the value in degree:0
Sum of the cos series=1.000000
nn@linuxmint ~ $ ./a.out
Enter the no of terms:0
enter the value in degree:0
Sum of the cos series=1.000000
nn@linuxmint ~ $ ./a.out
Enter the no of terms:10
enter the value in degree:60
Sum of the cos series=0.500460
nn@linuxmint ~ $

Evaluation of SinX Series - C Program

Program:
#include<stdio.h>
//#include<conio.h>
void sinx(float x,int n)
{
int i;
float term=x,sum=x;
for(i=1;i<=n;i++)
{
term=((-term)*(x*x))/((2*i)*(2*i+1));
sum=sum+term;
}
printf("Sum of the sin series=%f\n",sum);
}

void main()
{
int n,y;
float x;//clrscr();
printf("enter the limit:");
scanf("%d",&n);
printf("enter the value in degree:");
scanf("%f",&x);
y=x;
x=x*(3.14/180);
sinx(x,n);
//getch();
}

Output:
nn@linuxmint ~ $ gcc c11.c
nn@linuxmint ~ $ ./a.out
enter the limit:10
enter the value in degree:90
Sum of the sin series=1.000000
nn@linuxmint ~ $ ./a.out
enter the limit:10
enter the value in degree:0
Sum of the sin series=0.000000
nn@linuxmint ~ $ ./a.out
enter the limit:10
enter the value in degree:30
Sum of the sin series=0.499770
nn@linuxmint ~ $
Related Posts Plugin for WordPress, Blogger...