import math
import numpy as np
from math import*classSolution:defsolve(self, s, e):"""
:type s, e: int, int
:rtype: list
"""#请在此按照“编程要求”填写代码#********** Begin *********#
xlist=[]
d=(e-s)/40.0
a=[i for i in np.arange(s,e+0.000001,d)]for x in a:
hx=(1/math.sqrt(2*math.pi))*math.exp(x*x*(-1/2))
xlist.append(hx)return xlist
##********** End **********#
第2关:填充数组(循环版本)
import numpy as np
import math
classSolution:defsolve(self, s, e):"""
:type s, e: int, int
:rtype: numpy.ndarray
"""#请在此按照“编程要求”填写代码#********** Begin *********#
xlist = np.zeros(41)
ylist = np.zeros(41)for i inrange(41):
xlist[i]=s+i*(e - s)/40
ylist[i]=1/np.sqrt(2*np.pi)*np.exp(-0.5*xlist[i]**2)return ylist
##********** End **********#
第3关:填充数组(向量化版本)
import numpy as np
import math
classSolution:defsolve(self, s, e):"""
:type s, e: int, int
:rtype xlist, ylist: numpy.array, numpy.array
"""#请在此按照“编程要求”填写代码#********** Begin *********#
xlist=np.linspace(s, e,41)
ylist=1/np.sqrt(2*np.pi)*np.exp(-0.5*xlist**2)return xlist,ylist
##********** End **********#
第4关:绘制函数
import numpy as np
import math
import matplotlib.pyplot as plt
classSolution:defsolve(self, s, e):"""
:type s, e: int, int
:rtype: None
"""#请在此按照“编程要求”填写代码#********** Begin *********#
x = np.linspace(s, e,41)
y=1/np.sqrt(2*np.pi)*np.exp(-0.5*x**2)
plt.plot(x,y)
plt.show()##********** End **********#
plt.savefig("step4/stu_img/student.png")
第5关:函数作用于向量
import numpy as np
import math
classSolution:defsolve_1(self, v):"""
:type v: list
:rtype: list
"""#请在此按照“编程要求”填写代码#********** Begin *********#deff(x):return x**3+x*math.exp(x)+1
y =[f(a)for a in v]return y
##********** End **********#defsolve_2(self, v):"""
:type v: list
:rtype: numpy.array
"""#请在此按照“编程要求”填写代码#********** Begin *********#
xlist = np.array(v)
ylist = xlist**3+xlist*np.exp(xlist)+1return ylist
##********** End **********#
第6关:手工模拟执行向量表达式
import numpy as np
import math
classSolution:defsolve_1(self, x, t):"""
:type x, t: list, list
:rtype: list
"""#请在此按照“编程要求:使用math库实现”填写代码#********** Begin *********#
y=[]deff(a,b):return np.cos(np.sin(a))+np.exp(1/b)for k inrange(0,len(x)):
y.append(f(x[k],t[k]))return y
##********** End **********#defsolve_2(self, x, t):"""
:type x, t: list, list
:rtype: numpy.array
"""#请在此按照“编程要求:使用numpy库实现”填写代码#********** Begin *********#
xlist=np.array(x)
tlist=np.array(t)
y_1=np.cos(np.sin(xlist))+np.exp(1/tlist)return y_1
##********** End **********#