-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseries.py
More file actions
46 lines (34 loc) · 1.75 KB
/
Copy pathseries.py
File metadata and controls
46 lines (34 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from numpy import *
from scipy.special import factorial
import matplotlib.pyplot as plt
def get_coefficients(a,n):
# This function computes the coefficients a_0,a_1,...,a_n
# of a power series
return array([a(k) for k in range(n+1)])
def get_power_series(a,n):
# This function constructs a series approximation of degree n
coefficients = get_coefficients(a,n)
return lambda x: array([ak*x**k for k,ak in enumerate(coefficients)]).sum(axis=0)
def plot_series_approximations(a,x,y_exact=None,k0=0,L=-5,U=5):
# This function plots 9 different series approximations
# x is an array of x-values at which the approximations will be evaluated.
# y_exact is an exact function that is being approximated (optional).
# If provided, the exact solution will be plotted as a dashed black line.
# k0 is the starting approximation degree (optional).
# The function will plot the approximations of degree k0, k0+1, k0+2, ... k0 + 9.
# L and U are the lower and upper y bounds for the plots (optional).
fig,axes = plt.subplots(nrows=3,ncols=3,figsize=(10,10))
for k,ax in enumerate(axes.flatten()):
power_series = get_power_series(a,k+k0)
y = power_series(x)
ax.plot(x,y,'C{:}'.format(k),linewidth=3)
if y_exact is not None: ax.plot(x,y_exact,'k--',linewidth=2)
ax.grid()
ax.set_xlim((x[0],x[-1]))
ax.set_ylim((L,U))
ax.set_title('Degree-{:} approximation'.format(k+k0))
for ax in axes[:,1:].flatten(): ax.set_yticklabels('')
for ax in axes[:-1,:].flatten(): ax.set_xticklabels('')
for ax in axes[:,0]: ax.set_ylabel('$y$')
for ax in axes[-1,:]: ax.set_xlabel('$x$')
fig.tight_layout()