0

How to produce Taylor Series of Exponential function?

I am trying to create a program, that ask no. Of terms and the value assigned from user and print result in a form of Taylor expansion. Currently, it has error and not working properly. My attempt # Taylor Series Expansion of Exponential Function. import math exp = math.e series = 1 pow = 1 y = 1 size = int(input("Enter number of terms.")) x = int(input("Enter any value. ")) real_val = exp**x print("") print("Real Value = ", real_val) # Define Power function. def Power(pow): pow = x**size return pow # Define Exponential Function. def Exp(y): for i in range(1, size+1): y = Power(i)/math.factorial(i) return y print("Final answer is ", y)

19th Aug 2023, 4:33 AM
Yedu Krishnan
Yedu Krishnan - avatar
6 Antworten
+ 2
I pasted your code into the playground and it runs after correcting indentation in Exp(). There is an extra space before the for statement. Noted: the Power function uses external variables x and size, and does not use the pow parameter that is passed in. Its return value is constant x**size, and unrelated to the loop index i where Power gets called. The value y in the main program never changes from 1, as it is used in Exp() but its scope there is limited to inside the function. I recommend studying more to get a better understanding about functions, and about variable scope.
19th Aug 2023, 6:42 AM
Brian
Brian - avatar
+ 4
Can you share your attempt?
19th Aug 2023, 5:12 AM
Sakshi [Offline 🙃]
Sakshi [Offline 🙃] - avatar
+ 3
It would be easier to help you, Yedu Krishnan, if we could try out the code. Please share a link to what you have so far. Indicate what input values you used, what output you are expecting, and be more explicit about what error you are getting. Have you tried searching in the Code section? For reference, here are a couple Taylor Series Python programs I found. https://code.sololearn.com/cHYCV3w032C1/?ref=app https://code.sololearn.com/cB1Cb1bXpqIX/?ref=app
19th Aug 2023, 5:41 AM
Brian
Brian - avatar
+ 1
Brian Thanks a lot. I removed some function and now code is working. 😁👍
19th Aug 2023, 9:46 AM
Yedu Krishnan
Yedu Krishnan - avatar
0
Sakshi # Taylor Series Expansion of Exponential Function. import math exp = math.e series = 1 pow = 1 y = 1 size = int(input("Enter number of terms.")) x = int(input("Enter any value. ")) real_val = exp**x print("") print("Real Value = ", real_val) # Define Power function. def Power(pow): pow = x**size return pow # Define Exponential Function. def Exp(y): for i in range(1, size+1): y = Power(i)/math.factorial(i) return y print("Final answer is ", y)
19th Aug 2023, 6:30 AM
Yedu Krishnan
Yedu Krishnan - avatar
0
Bro me nya hu koi mijhe web development karna sikha do
20th Aug 2023, 8:29 PM
Prince __bhai
Prince __bhai - avatar