先纵览一下思考与练习的题目,可以发现主要是画图和解方程方面的题目。
下面是我给出的解答。
准备工作首先导入几乎每题都需要用的库,并进行绘图和科学计数法的设置。其他模块在使用时再导入。
import matplotlib.pyplot as plt import numpy as np plt.rcParams['font.sans-serif']=['SimHei'] # 在图中显示中文字体 plt.rcParams['axes.unicode_minus'] = False # 在图中显示负号 np.set_printoptions(precision=3, suppress=True) # 设置浮点数小数位数为3 但科学计数法感觉没法关闭
01 绘制函数图像
题1:
x = np.linspace(-10,10,100)
z = np.cosh(x)
plt.plot(x, z, label='双曲余弦函数')
z = np.sinh(x)
plt.plot(x, z, label='双曲正弦函数')
z = np.exp(x)*0.5
plt.plot(x, z, label='y=(1/2)*exp(x)')
plt.axis('square')
plt.xlim([-10,10])
plt.ylim([-10,10])
plt.grid()
plt.legend()
x = np.linspace(-5,5,100)
y = np.cosh(x)
plt.plot(x, y)
plt.title('双曲余弦函数')
print(y.min())
1.001275651185361
z = np.sinh(x)
plt.plot(x, z)
plt.title('双曲正弦函数')
z = np.exp(x)*0.5 plt.plot(x, z)
题2:
伽马函数可以当成是阶乘在实数集上的延拓,在实数上的伽马函数定义域大于0。对于正整数n,具有如下性质:
伽马n = n-1的阶乘。
首先,在1728年,哥德巴赫在考虑数列插值的问题,通俗的说就是把数列的通项公式定义从整数集合延拓到实数集合,例如数列1,4,9,16…
可以用通项公式n²自然的表达,即便 n 为实数的时候,这个通项公式也是良好定义的。直观的说也就是可以找到一条平滑的曲线y=x²通过所有的整数点(n,n²),从而可以把定义在整数集上的公式延拓到实数集合。
然后有一天,哥德巴赫开始处理阶乘序列1,2,6,24,120,720,…,我们可以计算2!,3!,是否可以计算2.5!呢?我们把最初的一些(n,n!)的点画在坐标轴上,确实可以看到,容易画出一条通过这些点的平滑曲线。
但是哥德巴赫无法解决阶乘往实数集上延拓的这个问题,于是写信请教尼古拉斯·伯努利和他的弟弟丹尼尔·伯努利,由于欧拉当时和丹尼尔·伯努利在一块,他也因此得知了这个问题。欧拉于1729 年解决了这个问题,由此导致了伽玛函数的诞生,当时欧拉只有22岁。–百度百科
from scipy.special import gamma
x = np.arange(1,5,0.1) # 1.0 ~ 5.9 共50个点
gamma_z = gamma(x)
plt.plot(x, gamma_z)
plt.axis('square')
(0.8049999999999998, 22.56427494004573, -0.10283102636750252, 21.656443913678228)
03 单个窗口绘图
题3:
def fun1(x,k):
return k*(x**2) + 2*k
plt.figure(figsize=(6, 6))
plt.axis('square') # 使得x,y的刻度比例一致 放后面会出问题
# 绘制图像
for k in range(1,7):
x = np.linspace(-10, 10, 100)
# z = [fun1(a, k) for a in x] # 生成
z = fun1(x, k)
plt.plot(x, z, label = k)
plt.ylim([0, 20])
plt.xlim([-10, 10])
# 关于图像的装饰性函数
plt.grid()
plt.legend()
plt.title('k=1、2、3、4、5、6')
plt.suptitle('y = k*x^2 + 2*k')
Text(0.5, 0.98, 'y = k*x^2 + 2*k')
题4:
row = 2 # 设置子图行数
col = 3 # 设置子图列数
plt.figure(figsize=(10, 6.18))
for k in range(1,7):
x = np.linspace(-10, 10, 100)
# z = [fun1(a, k) for a in x]
z = fun1(x, k)
plt.subplot(row, col, k)
plt.plot(x, z)
plt.title("k=" + str(k))
plt.ylim([0,100])
plt.grid()
plt.suptitle('y = k*x^2 + 2*k')
题5:
# 单叶双曲面
def fun2(x, y):
z = np.sqrt((x**2/4 + y**2/10 - 1)*8)
# z[np.where(np.isnan(z))] = 0 将nan设为0
return z
x = np.linspace(-10, 10, 2000) x, y = np.meshgrid(x, x) z = fun2(x ,y) fig = plt.figure(figsize=(8,8)) ax = fig.gca(projection='3d') # 或者 ax = plt.axes(projection='3d') axes :axis的复数 这行代码在jupyter中需要与画图在同一格子才起作用 ax.plot_surface(x, y, z, color='y') ax.plot_surface(x, y, -z, color='y') # 手动添加开方可取到的负值 plt.show() # x,y,z的比例只支持auto 不支持equal等
05 - 2 绘制椭圆双曲面
# 椭圆抛物面
def fun3(z, y):
z = x**2/4 + y**2/6
return z
x = np.linspace(-10, 10, 2000) x, y = np.meshgrid(x, x) z = fun3(x ,y) fig = plt.figure(figsize=(8,8)) ax = fig.gca(projection='3d') # 或者 ax = plt.axes(projection='3d') axes :axis的复数 这行代码在jupyter中需要与画图在同一格子才起作用 ax.plot_surface(x, y, z, color='y') plt.show() # x,y,z的比例只支持auto06 题目无数据跳过
07 求方程组解
题7:
# 人工检验R(A)=R(A拔)所以有唯一解
A = np.array([[4,2,-1],
[3,-1,2],
[11,3,0]])
b = np.array([2,10,8])
x = np.linalg.inv(A) @ b x
array([-4.053e+15, 1.486e+16, 1.351e+16])
# 或者 x = np.linalg.solve(A, b) x
array([-4.053e+15, 1.486e+16, 1.351e+16])
方程组02
A = np.array([[2,3,1],
[1,-1,2],
[3,8,-2],
[4,-1,9]])
b = np.array([4,-15,13,-6])
np.linalg.pinv(A)*b
array([[ -0. , -30. , 2.6 , 2.4 ],
[ 0.19 , 15. , 0.124, -1.314],
[ 0.095, 15. , -1.238, -1.857]])
08 求非线性方程组的符号解和数值解
题8:
数值解
from scipy.optimize import fsolve
fx = lambda x: [x[0]**2 - x[1] + x[0] -3,
x[0] + 3*x[1] -2] # x是列表输入
s = fsolve(fx, [1,1]) # [1,1]函数初值
s
array([1.361, 0.213])
符号解
import sympy as sp
x,y = sp.var('x y')
sp.solve([x**2 - y + x -3, x + 3*y -2], [x,y]) # 有两个解! 但数值解只返回了一个
[(-2/3 + sqrt(37)/3, 8/9 - sqrt(37)/9), (-sqrt(37)/3 - 2/3, sqrt(37)/9 + 8/9)]
09 已知f(x)和g(x),求非线性方程组的解
题9:
from scipy.optimize import fsolve
def f(x):
return (abs(x + 1) - abs(x - 1))/2 + np.sin(x)
def g(x):
return (abs(x + 3) - abs(x - 3))/2 + np.cos(x)
fx = lambda x: [-2*x[0] + 3*f(x[2]) + 4*g(x[3]) - 1,
-3*x[1] + 2*f(x[2]) + 6*g(x[3]) - 2,
-x[2] + f(x[0]) + 3*g(x[1]) -3,
-5*x[3] + 4*f(x[0]) + 6*g(x[1])] # x是列表输入 返回的是冒号后面的东西
fsolve(fx, [1,1,1,1])
array([4.383, 3.381, 3.14 , 2.477])
10 求超定(矛盾)方程组的最小二乘解
题10:
from scipy.optimize import least_squares
fs = lambda x:[-2*x[0] + 3*f(x[2]) + 4*g(x[3]) - 1,
-3*x[1] + 2*f(x[2]) + 6*g(x[3]) - 2,
-x[2] + f(x[0]) + 3*g(x[1]) - 3,
-5*x[3] + 4*f(x[0]) + 6*g(x[1]) -1,
-x[0] - x[2] + f(x[3]) + g(x[1]) - 2,
-x[1] + 3*x[3] + 2*f(x[0]) - 10*g(x[2]) - 5]
x = least_squares(fx, [0,0,0,0]) x
active_mask: array([0., 0., 0., 0.])
cost: 1.575281272016496e-25
fun: array([-0., -0., 0., 0.])
grad: array([ 0., 0., -0., -0.])
jac: array([[-2. , 0. , 4.731, 3.609],
[-0. , -3. , 3.154, 5.414],
[ 1.563, 2.006, -1. , 0. ],
[ 6.252, 4.012, -0. , -5. ]])
message: '`gtol` termination condition is satisfied.'
nfev: 8
njev: 7
optimality: 4.536140070440464e-12
status: 1
success: True
x: array([-0.973, 0.338, -0.956, 0.098])



