Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating step function
#1
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()

# .................................................................................................#
Gribouillis write Jul-25-2021, 07:19 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.

I fixed for you this time. Please use code tags on future posts.
Reply
#2
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.])
erdemath likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trouble with creating a looping function in python 2.7 (complex) Afterdarkreader 3 3,104 Dec-12-2017, 06:56 PM
Last Post: Afterdarkreader

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020