嘿我正试图找出以下问题的情节: U(x)= U0,如果| x | ≥x0 如果| x |,则U(x)= U0 * | x | / x0 < X0
和programm:
from pylab import*
x_0=5
U_0=200
#U_x=zeros(n,1)
#x=zeros(n,1)
x=arange(-20,20,0.01)
if float(abs(x))>=x_0:
U_x=U_0
elif float(abs(x))<x_0:
U_x=U_0*(float(abs(x))/x_0)
fig=figure()
suptitle("a)")
fig.subplots_adjust(hspace=0.5)
plot(x,U_x)
xlabel('x [m]')
ylabel('U_x [J]')
show()
但我总是犯这个错误:
if float(abs(x))>=x_0:
TypeError: only length-1 arrays can be converted to Python scalars
请帮助:)