I am trying to plot a function f. Under is the code that I have.
import matplotlib.pyplot as plt
import numpy as np
def f(x):
if -np.pi < x < np.pi/2:
return 0
elif np.pi/2 < x < np.pi:
return 1
else:
print("Error")
x = np.linspace(-np.pi, np.pi, 1000)
plt.plot(x, f(x))
plt.show()
Could someone help me explain why I get the error message "ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()", and how do I fix it?
Have been trying to look on the internet for a solution to my error message, but I do not understand how to solve it.