Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Attention Please submit your take-home final exam including your Python codes el

ID: 3721052 • Letter: A

Question

Attention Please submit your take-home final exam including your Python codes electronically through the Blackboard. E-mail submissions will not be accepted 1. 2. Codes are expected to be clear, readable and compact with minimum number of lines and variables, and proper indentation. Variable names are expected to be appropriate, meaningful and self-explanatory. Codes will be graded based on these criteria. A correctly working code does not guarantee full credit. 3. Cheating is prohibited and will cause loss of points FINAL EXAM 1. (35 points) Consider the following exponential function with a real variable, x: The Taylor series expansion for this function around x-0 is given as Write a program that prompts the user to enter a floating-point number, x, and a nonnegative integer, n, that represents the highest power of x to be included irn computing the Taylor series. ??. The program must have a recursive function with x and n as input parameters, and use it to compute ??. For example, if n-3, only the terms shown above must be included in yT. After ?? is computed, the program must print the actual value of y using the math-exp() function. ??, and the absolute value of the percentage error, e, made in computing yT such that e-1100"(y-??)/yl You may directly use the math.factorial() function to compute the factorials in ?? Assume that the user gives the inputs properly, so the program does not need to check if they are valid or not. Hint: The function with x, n as inputs should include a function call to itself with x, n-1 as inputs. The function with x, O as input must return 1

Explanation / Answer

#Python3 Code:

import math         #importing math module for calculations.
def y_t(x,n,flag):      #this is a Recursive function.
    if(flag==n):        #function break condition.
        return pow(x,n)/math.factorial(n)       #return last term of series.
    y=math.pow(x,flag)/math.factorial(flag)+y_t(x,n,flag+1)     #Else return current calculated term + recursion with next term as input.
    return y #return the last calculated answer.

x=float(input("Enter float number x:")) #input of x
n=int(input("Enter power n:"))          #input of n
y=math.exp(x)                           #calculate actual y
print("Actual value:",y)                #print actual y
yt=y_t(x,n,0)                           #calculate y from taylor series as yt
print("yt:",yt)                         #print yt.
error=math.fabs((y-yt)*100/y)           #calculate error.
print("Error is:",error)                #print error.