Hi, I am new to c and I have come across a problem when adding other functions to a programme and printing the values. The question I am attempting to solve is :
Code:
The following function computes ex by summing the Taylor series expansion to
n terms. Write a program to print a table
of ex using both this function and the exp()
function from the math.h library, for x = 0 to 1 in steps of 0.1.
The program should ask the user what value of n to use.
double taylor(double x, int n)
{
int i;
double sum = 1.0;
double term = 1.0;
for (i=1; i<=n; i++)
{ /*Or we could have written: */
term = term * x / i; /* term *= x / i; */
sum = sum + term; /* sum += term; */
}
return sum;
}
My code is
Code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
/*Taylor series for e*/
double taylor(double x, int n)
{
int i;
double sum = 1.0;
double term = 1.0;
/* Value of n*/
{
printf(" Value of n :\n" );
scanf("%f",&n);
}
for (i=1; i<=n; i++)
{
term = term * x / i;
sum = sum + term;
printf(" Taylor series : %f\n", sum);
}
return sum;
}
int main ()
{
int i;
double n, x, terme;
/* e using math function*/
for(x=0; x<=1; x = x+0.1)
{
terme = exp(x);
printf(" The values of the exponential term : %f\n", terme);
}
taylor(x,n);
return(0);
}
The code prints out the values for exp, but it gets stuck in the Taylor function and I'm not sure how to solve it.
Thanks