Created
August 29, 2015 10:35
-
-
Save mick001/13f28ae4560e8ce61439 to your computer and use it in GitHub Desktop.
Taylor series with Python and Sympy part 3. Full article at http://www.firsttimeprogrammer.blogspot.com/2015/03/taylor-series-with-python-and-sympy.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Plot results | |
def plot(): | |
x_lims = [-5,5] | |
x1 = np.linspace(x_lims[0],x_lims[1],800) | |
y1 = [] | |
# Approximate up until 10 starting from 1 and using steps of 2 | |
for j in range(1,10,2): | |
func = taylor(f,0,j) | |
print('Taylor expansion at n='+str(j),func) | |
for k in x1: | |
y1.append(func.subs(x,k)) | |
plt.plot(x1,y1,label='order '+str(j)) | |
y1 = [] | |
# Plot the function to approximate (sine, in this case) | |
plt.plot(x1,np.sin(x1),label='sin of x') | |
plt.xlim(x_lims) | |
plt.ylim([-5,5]) | |
plt.xlabel('x') | |
plt.ylabel('y') | |
plt.legend() | |
plt.grid(True) | |
plt.title('Taylor series approximation') | |
plt.show() | |
plot() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment