Answer:
The function in Python is as follows:
import math
import numpy as np
def exp(x):
mylist = []
for n in range(10):
num = (x**n)/(math.factorial(n))
mylist.append([num])
exp_approx = np.asarray(mylist)
sum = 0
for num in exp_approx:
sum+=num
return sum
Step-by-step explanation:
The imports the python math module
import math
The imports the python numpy module
import numpy as np
The function begins here
def exp(x):
This creates an empty list
mylist = []
This iterates from 0 to 9
for n in range(10):
This calculates each term of the series
num = (x**n)/(math.factorial(n))
This appends the term to list, mylist
mylist.append([num])
This appends all elements of mylist to numpy array, exp_approx
exp_approx = np.asarray(mylist)
This initializes the sum of the series to 0
sum = 0
This iterates through exp_approx
for num in exp_approx:
This adds all terms of the series
sum+=num
This returns the calculated sum
return sum