Please use C Langauge and include comment on the code
Problem #1 - Taylor Series Convergence (C Program) For this program, let's calculate the Taylor series expansion for the exponential function: e*. The equation we will implement is: 00 1 x X X3 X4 1 T 21 + + +... +4 +... i=0 This summation will converge to e' after some number of terms have been added to the whole. The number of terms will vary depending on the value of x. Note that every term in the summation is positive, so the summation will converge to the actual value from below. "x" is assumed to be > 0. In general, if we wish to test two floating point numbers for equality, we can calculate the absolute value of the difference between the numbers and then determine if that difference is less than some epsilon value. Let's apply that technique to this problem. We'll test the growing sum against the standard et value (as calculated by the exp (x) function) until the difference between the two becomes less than some epsilon (to be defined shortly). At that point we'll consider that the series has converged on the correct value. In addition, we'll track the number of terms that must be added to the sum to achieve convergence and report that value to the user. Functions Your program should contain at least two functions: main() a) standard main function unsigned int factorial (int n) a) This function will calculate n! and return the result as an unsigned int. Since we know that all factorials in this problem are positive, there is no need to return a signed value. Note that the function is to have a return data type of unsigned int. This is a requirement for both Windows and Mac systems. Calculating the factorial of a single number as an unsigned int is the only task for this function. It should not be used to print or perform any other task. If you want to use other functions as well, say, for example, the entire series is calculated in a separate function, that would be okay. Functionality Your program should do the following: Ask the user to enter an "x" value. This value should be a double that is >0. Have the program calculate ex by using the Taylor Series given above and print the following table: For x = 0.3: # Iter e^x Sum Diff - - - - - - - - von WNPIH 1.349859 1.349859 1.349859 1.349859 1.349859 1.349859 1.349859 1.000000 1.300000 1.345000 1.349500 1.349838 1.349858 1.349859 0.34985881 0.04985881 0.00485881 0.00035881 0.00002131 0.00000106 0.00000005 The number of iterations required for convergence is: 7 For x = 0.8: Sum Diff # Iter --- e^x -------- 2.225541 2.225541 2.225541 2.225541 2.225541 2.225541 2.225541 2.225541 1.000000 1.800000 2.120000 2.205333 2.222400 2.225131 2.225495 2.225536 1.22554093 0.42554093 0.10554093 0.02020760 0.00314093 0.00041026 0.00004617 0.00000456 CS1325 - Introduction to Programming page: 3 2.225541 2.225541 2.225541 2.225541 0.00000040 0.00000003 10 The number of iterations required for convergence is: 10 These are actual data and not dummy data. The "# Iter" is the current iteration in the growing sum in the series. The "e^x column is the value of exp (x). The Sum" column is the value of the summation during that particular iteration. And the Dift" column is the difference between ex and Sum. (When this quantity become less than FLT_EPSILON (see below), the iterations should stop.) We can use the built-in epsilon value that is included in
, FLT EPSILON, to test the difference between the standard value and the series value. This is a pre-defined symbolic constant in . (In VS2013, FLT_EPSILON == 1.192092896e-07F.) Test your program on x=0.5, x=1.0, and x=1.5 to see how it behaves. Supplemental: Note that the previous program runs just fine when 0 = 1.7 it fails to run properly. You can detect this in different ways. One of them is that the differential value becomes negative. This means that the growing sum has become larger than FLT_EPSILON. Since this particular Taylor Series converges from below, this should never happen. Please write a brief report that answers the following questions: 1) 2) 3) What's going on? Why does the program fail to run when x >= 1.7? How did you locate the problem? What steps did you take? How can it be fixed? Please include a second version of the program that fixes the problem and will run for larger values of x. You can name these two files as HW1 Exla.c and HW1_Exlb.c. How many terms are required for convergence when x = 2.0? x=5.0? x=10.0? Problem #1 - Taylor Series Convergence (C Program) For this program, let's calculate the Taylor series expansion for the exponential function: e*. The equation we will implement is: 00 1 x X X3 X4 1 T 21 + + +... +4 +... i=0 This summation will converge to e' after some number of terms have been added to the whole. The number of terms will vary depending on the value of x. Note that every term in the summation is positive, so the summation will converge to the actual value from below. "x" is assumed to be > 0. In general, if we wish to test two floating point numbers for equality, we can calculate the absolute value of the difference between the numbers and then determine if that difference is less than some epsilon value. Let's apply that technique to this problem. We'll test the growing sum against the standard et value (as calculated by the exp (x) function) until the difference between the two becomes less than some epsilon (to be defined shortly). At that point we'll consider that the series has converged on the correct value. In addition, we'll track the number of terms that must be added to the sum to achieve convergence and report that value to the user. Functions Your program should contain at least two functions: main() a) standard main function unsigned int factorial (int n) a) This function will calculate n! and return the result as an unsigned int. Since we know that all factorials in this problem are positive, there is no need to return a signed value. Note that the function is to have a return data type of unsigned int. This is a requirement for both Windows and Mac systems. Calculating the factorial of a single number as an unsigned int is the only task for this function. It should not be used to print or perform any other task. If you want to use other functions as well, say, for example, the entire series is calculated in a separate function, that would be okay. Functionality Your program should do the following: Ask the user to enter an "x" value. This value should be a double that is >0. Have the program calculate ex by using the Taylor Series given above and print the following table: For x = 0.3: # Iter e^x Sum Diff - - - - - - - - von WNPIH 1.349859 1.349859 1.349859 1.349859 1.349859 1.349859 1.349859 1.000000 1.300000 1.345000 1.349500 1.349838 1.349858 1.349859 0.34985881 0.04985881 0.00485881 0.00035881 0.00002131 0.00000106 0.00000005 The number of iterations required for convergence is: 7 For x = 0.8: Sum Diff # Iter --- e^x -------- 2.225541 2.225541 2.225541 2.225541 2.225541 2.225541 2.225541 2.225541 1.000000 1.800000 2.120000 2.205333 2.222400 2.225131 2.225495 2.225536 1.22554093 0.42554093 0.10554093 0.02020760 0.00314093 0.00041026 0.00004617 0.00000456 CS1325 - Introduction to Programming page: 3 2.225541 2.225541 2.225541 2.225541 0.00000040 0.00000003 10 The number of iterations required for convergence is: 10 These are actual data and not dummy data. The "# Iter" is the current iteration in the growing sum in the series. The "e^x column is the value of exp (x). The Sum" column is the value of the summation during that particular iteration. And the Dift" column is the difference between ex and Sum. (When this quantity become less than FLT_EPSILON (see below), the iterations should stop.) We can use the built-in epsilon value that is included in , FLT EPSILON, to test the difference between the standard value and the series value. This is a pre-defined symbolic constant in . (In VS2013, FLT_EPSILON == 1.192092896e-07F.) Test your program on x=0.5, x=1.0, and x=1.5 to see how it behaves. Supplemental: Note that the previous program runs just fine when 0 = 1.7 it fails to run properly. You can detect this in different ways. One of them is that the differential value becomes negative. This means that the growing sum has become larger than FLT_EPSILON. Since this particular Taylor Series converges from below, this should never happen. Please write a brief report that answers the following questions: 1) 2) 3) What's going on? Why does the program fail to run when x >= 1.7? How did you locate the problem? What steps did you take? How can it be fixed? Please include a second version of the program that fixes the problem and will run for larger values of x. You can name these two files as HW1 Exla.c and HW1_Exlb.c. How many terms are required for convergence when x = 2.0? x=5.0? x=10.0