在arange生成的数组添加元素
import numpy as np
import math
import matplotlib.pyplot as plt
x = np.arange(2,8.1,0.1)
n = len(x) - 1
alpha = 0.5
rho = np.zeros((n+1,1))
for i in range(0,n):
h = x[i + 1] - x[i]
def f(x):
return 0.03 * (x - 6) ** 4
a = f(x[i])
b = f(x[i+1])
t = (a**2 + b**2)*h/2
rho[i] = h**2*t
#mylist = np.zeros((n+1,1))
for i in range (0,n+1):
c = max(rho)
if rho[i] > alpha*c:
x = np.insert(x,0,(x[i+1]+x[i]) /2)
print(x)
上面是正确的代码
本来我是想用x.append(),结果报错AttributeError: ‘numpy.ndarray’ object has no attribute ‘append’