Jul-25-2024, 10:20 AM
(This post was last modified: Jul-25-2024, 01:08 PM by deanhystad.)
Hello, I am trying to plot a peicewise function but I get an error (ValueError: The truth value of an array with more than one element is ambiguous.) and despite reading the forums/help, I am unable to fix it. Can someone please help. Thanks
import math import numpy as np import matplotlib.pyplot as plt import scipy from scipy import optimize import scipy.integrate as integrate def current(x): return np.piecewise(x, [0 < x < 50, x > 50], [4-0.3*x, 0]) def voltage(x): return np.piecewise(x, [0 < x < 50, x > 50], [x*(1-0.03*x), 0]) def power(x): return current(x)* voltage(x) points = 100 tlim = 10e-3 t = np.linspace(0, tlim, points) figure = plt.subplots(figsize=(10, 5)) axe = plt.subplot(311) plt.title('Voltage') plt.ylabel('Voltage [V]') plt.grid() plt.plot(t,voltage(t),color='black') axe = plt.subplot(312) plt.title('Current') plt.ylabel('Current [A]') plt.grid() plt.plot(t,current(t),color='black') axe = plt.subplot(313) plt.title('Power') plt.xlabel('Time [s]') plt.ylabel('Power [W]') plt.grid() plt.plot(t,power(t),color='blue') plt.show()
deanhystad write Jul-25-2024, 01:08 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.