CSC601 Exam. Section C ====================== Sample question. Use of Taylor series for math function calculations -------------------------------------------------------------------- Introduction ------------- Recall Taylor series for common mathematical functions, such as e^x, sin(x), ln(x) etc. E.g. e^x = 1 + x + x^2/2! + x^3/3! + x^4/4! + ... = Sum(k=0 ... inf) [(x^k)/k!] (for -inf < x < inf) sin(x) = x - x^3/3! + x^5/5! + x^7/7! + ... = Sum(k=0 ... inf) [((-1)^k)*(x^(2k+1))/(2k+1)!] (for -inf < x < inf) ln(x) = (x-1) - (x-1)^2/2 + (x-1)^3/3 ) + ...= Sum(k=0 ... inf) [((-1)^k)*((x-1)^k)/(k+1)] (for 0 < x <= 2) (Here, k! stands for factorial of k; i.e. k! = 1*2* ... (k-1)*k) Taylor series are often used to compute good approximate values of complicated functions at specified points. For example, taking x = 1.1 in the first five terms of the Taylor series equation for ln(x): ln(1.1) = 0.1 - 0.01/2 + 0.001/3 - 0.0001/4 + 0.00001/5 = 0.00953103333... This value is correct to six decimal places of accuracy. The approximate value of a function that has an appropriate Taylor series at a point x is computed by iterative calculation of members of the series and the sum of the series UNTIL the absolute value of the current member becomes less than the given approximation error, espilon. Problem Specification --------------------- Consider the problem of calculating the exponent function e^x for a given value of argument x and a given approximation error epsilon. Using Taylor series, the generic formula for calculation is as follows: Sum(k=0 ... inf) [(x^k)/k!] The iteration is applied until the absolute value of (x^k)/k! is less than epsilon. (1) Outline an algorithm (using design diagrams) for a program which receives two inputs x and epsilon and calculates the exponent e^x using the corresponding Taylor series formula. What type is most appropriate for x and epsilon? [5 marks] (2) Write a suitable C++ function interface for a function, named myexp, which returns the value of e^x, for given formal parameters x and epsilon. [2 marks] (3) Write a C++ implementation (function definition) for function myexp, following the algorithm designed in (1). [8 marks] (4) Write a small fragment of C++ code, in main(), that calls myexp with appropriate actual parameters, whose values are entered by the user from keyboard. [5 marks] Remark. To calculate the absolute value you may call the fabs ------ function with the following interface: double fabs (double x). e.g.: fabs(3.21) will return 3.21; fabs(-3.14) will return 3.14.