Creating step function - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: Creating step function (/thread-34369.html) |
Creating step function - erdemath - Jul-25-2021 Hi, For some time dependent biophysical model, I´m trying to visualize the certain impulses occured on certain time. I define some step function below called as Id(.) below. But, python complains about that. The message I receive is>> The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() I do so, but then the results is not what I want. I just want to get the value 150.0 and 50. at specific time range. # ............................... The Code ........................................# import matplotlib.pyplot as plt import numpy as np # Start and end time (in milliseconds) tmin = 0.0 tmax = 50.0 # Time values T = np.linspace(tmin, tmax, 5000) # Input stimulus def Id(t): if 0.0 < t < 1.0: return 150.0 elif 10.0 < t < 11.0: return 50.0 return 0.0 # Input stimulus Idv = Id(T) #Idv = [Id(t) for t in T] fig, ax = plt.subplots(figsize=(12, 7)) ax.plot(T, Idv) ax.set_xlabel('Time (ms)') ax.set_ylabel(r'Current density (uA/$cm^2$)') ax.set_title('Stimulus (Current density)') plt.grid() # .................................................................................................# RE: Creating step function - Gribouillis - Jul-25-2021 Occasional numpy user here, the following example may help >>> import numpy as np >>> t = np.linspace(0, 12, 30) >>> t array([ 0. , 0.4137931 , 0.82758621, 1.24137931, 1.65517241, 2.06896552, 2.48275862, 2.89655172, 3.31034483, 3.72413793, 4.13793103, 4.55172414, 4.96551724, 5.37931034, 5.79310345, 6.20689655, 6.62068966, 7.03448276, 7.44827586, 7.86206897, 8.27586207, 8.68965517, 9.10344828, 9.51724138, 9.93103448, 10.34482759, 10.75862069, 11.17241379, 11.5862069 , 12. ]) >>> np.where(np.logical_and(0 < t, t < 1), 150.0, 0.0) + np.where(np.logical_and(10.0 < t, t < 11.0), 50.0, 0.0) array([ 0., 150., 150., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 50., 50., 0., 0., 0.]) |