要求将代码的“-----”部分补充完整,救救孩子的作业吧啊啊啊啊
以下为代码
import numpy as np
import matplotlib.pyplot as plt
#####################################################
FUNCTIONS
#####################################################
def SimpsonMethodValues(fvalues, a, b):
""" Simpson method for determining an approximation of
the integral of a function over the interval [a, b]
The function is known by its sampling "fvalues"
Input : fvalues = sampling of the function to be integrated (array)
: a,b = bounds of the interval
Output : S = Simpson approximation of the integral
"""
N = len(fvalues)
h = 2 * (b-a) / (N-1)
# partial sum S1
S1 = -----
S1 = S1 / 3.
# partial sum S2
S2 = -----
S2 = 2*S2 / 3.
# sum S
S = -----
S = S + S1 + S2
S *= h
return S
functions to be approximated :
def g0(x):
return np.ceil(x) # on ]-1,1]
def g1(x):
return x
def g2(x):
return x**2
def g3(x):
return np.abs(x)
def g4(x):
return np.sqrt(x)
#####################################################
MAIN
#####################################################
plt.cla()
plt.grid()
eps = 1e-10
Choice of a function :
f = g0; a = -1+eps; b = 1;
#f = g1; a = 0; b = 1;
#f = g2; a = -1; b = 1;
#f = g3; a = -1; b = 1;
#f = g4; a = 0; b = 1;
Period and pulsation
T = b-a;
w = 2*np.pi / T
Plot of the function to be approximated :
n = 8 # ==> 2^n+1 evaluation points (odd number of points)
t = np.linspace(a,b,2**n + 1) # for graph plotting
ft = f(t)
plt.plot(t-T,ft,'r')
plt.plot(t, ft,'r')
plt.plot(t+T,ft,'r')
N = 7
Fourier coefficients an and bn :
an = np.zeros(N+1)
bn = np.zeros(N+1)
an[0] = SimpsonMethodValues(ft, a, b) / T
for k in range(1,N+1):
-----
-----
-----
-----
Fourier series at order N (over 3 periods)
tt = np.linspace(a-T,b+T,500)
ff = np.ones(np.size(tt))
ff = an[0] * ff
for k in range(1,N+1):
-----
plt.plot(tt,ff)