Python Graphing a piecewise function (Python)

AI Thread Summary
The discussion focuses on writing a Python script to plot a specific function defined by parameters V_0, t_0, and τ. The user seeks guidance on achieving a plot that includes a horizontal line. Key points include the need to correctly apply conditions to the time array, ensuring that the function modifies the voltage values (Vt) based on the time conditions. It is emphasized that the condition should be applied element-wise rather than using a method that checks the entire array at once. Suggestions include using boolean indexing to set Vt values for times less than or equal to t_0 and explicitly defining the time and voltage arrays using NumPy's concatenate and linspace functions. The user expresses gratitude for the assistance received.
member 731016
I am trying to write a python script to plot the function,
1683316565374.png

Where
##V_0 = 5~V##
##t_0 = 10~ms##
##\tau = 5~ms##

My script that I have written to try to do this is,
1683318107347.png

Which plots,
1683318013306.png

However, the plot is meant to look like this with the horizontal line.
1683317219596.png

Can someone please give me some guidance to get that graph?

Many thanks!
 
Last edited by a moderator:
Technology news on Phys.org
(Please copy your code and paste it between [code] tags; don't just post a screenshot. It is better to call your variable tau rather than use a unicode symbol.)

(t > t_0).all is a method; its boolean value is True. Thus your function will only ever return the unmodified Vt. You need to add () after all to actually call the method. But that isn't what you want either.

(t > t_0).all() is True if every element of t is greater than t_0 and False otherwise. But you need to apply the condition on the level of each element; an if statement won't do that unless included in a loop over the index of the array.

Having calculated Vt, you only need to overwrite those elements where t <= t_0 is True. That can be done easily:
Python:
Vt[t <= t_0] = V_0 

plt.plot(t,Vt)

Alternatively, you can explicitly allocate your points to each range:
Python:
t = np.concatenate(
    [np.linspace(0,t_0, 21), np.linspace(t_0, t_max, 101)],
)

Vt = np.concatenate(
    [V0 * np.ones(21), V0 * np.exp(-np.linspace(0,t_max-t_0,101)/tau)]
)
 
Last edited:
  • Like
Likes member 731016
pasmith said:
(Please copy your code and paste it between [code] tags; don't just post a screenshot. It is better to call your variable tau rather than use a unicode symbol.)

(t > t_0).all is a method; its boolean value is True. Thus your function will only ever return the unmodified Vt. You need to add () after all to actually call the method. But that isn't what you want either.

(t > t_0).all() is True if every element of t is greater than t_0 and False otherwise. But you need to apply the condition on the level of each element; an if statement won't do that unless included in a loop over the index of the array.

Having calculated Vt, you only need to overwrite those elements where t <= t_0 is True. That can be done easily:
Python:
Vt[t <= t_0] = V_0

plt.plot(t,Vt)

Alternatively, you can explicitly allocate your points to each range:
Python:
t = np.concatenate(
    [np.linspace(0,t_0, 21), np.linspace(t_0, t_max, 101)],
)

Vt = np.concatenate(
    [V0 * np.ones(21), V0 * np.exp(-np.linspace(0,t_max-t_0,101)/tau)]
)
Thank you for your help @pasmith! That is very helpful!
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Back
Top