嘿,我正在尝试为以下问题绘制图:U (x) =U0, if |x| ≥ x0 U (x)=U0*|x|/x0 如果 |x| < x0
和程序:
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
请帮忙:)