我在数值上为x网格和x向量以及时间网格设置了网格,但我还是为x(位置)设置了一个数组,该数组只能在0到20之间,并且t(时间)从0到因此,为了求解热方程,请先将其设为1000。但是每次例如要执行步骤10时,都会出现错误:
"Traceback (most recent call last):
File "/home/universe/Desktop/Python/Heat_1.py", line 33, in <module>
x[i] = a + i*h
IndexError: index 10 is out of bounds for axis 0 with size 10"
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
from math import sin,pi
import numpy
import numpy as np
#Constant variables
N = int(input("Number of intervals in x (<=20):"))
M = int(input("Number of time steps (<=1000):" ))
#Some initialised varibles
a = 0.0
b = 1.0
t_min = 0.0
t_max = 0.5
# Array …Run Code Online (Sandbox Code Playgroud) 我想-2.5 to 2.0通过使用该np.linspace()命令插入 8 个数据点,并完全理解这些数字应该是非负数,但是当我运行我的代码时,我收到一个错误,上面写着raise ValueError("x and y arrays must be equal in length along "
ValueError: x and y arrays must be equal in length along interpolation axis.这是我的代码:
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
X = np.linspace(-2.5, 2.0, num=8, endpoint=True)
Y = np.linspace(1, 44, num=44, endpoint=True)
f = interp1d(X,Y)
f2 = interp1d(X, Y, kind='cubic')
Xnew = np.linspace(-2.5, 2.0, num=44, endpoint=True)
plt.plot(X, Y, 'o', Xnew, f(Xnew), Xnew, f2(Xnew), …Run Code Online (Sandbox Code Playgroud)