Graphing a piecewise function (Python)

In summary, I need to modify my script to calculate Vt and then overwrite the points where t <= t_0 is True.
  • #1
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
  • #2
(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
  • #3
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!
 

Related to Graphing a piecewise function (Python)

1. How can I graph a piecewise function in Python?

To graph a piecewise function in Python, you can use libraries such as Matplotlib. Define your piecewise function using if-else statements and then plot the individual segments using Matplotlib's plotting functions.

2. Can you provide an example of graphing a piecewise function in Python?

Sure! Here is an example of graphing the piecewise function f(x) = { x^2 if x >= 0, -x^2 if x < 0 } using Matplotlib in Python:

3. How do I handle discontinuities when graphing a piecewise function in Python?

When graphing a piecewise function with discontinuities in Python, you can use the numpy library to handle these cases. Define separate functions for each segment of the piecewise function and use numpy's piecewise function to combine them.

4. Is it possible to customize the appearance of the graph of a piecewise function in Python?

Yes, you can customize the appearance of the graph of a piecewise function in Python using Matplotlib. You can set properties such as line color, line style, marker style, axis labels, and title to make your graph more visually appealing.

5. How can I add labels and a legend to a graph of a piecewise function in Python?

To add labels and a legend to a graph of a piecewise function in Python, use Matplotlib's labeling functions such as xlabel, ylabel, and legend. You can provide descriptive labels for the x and y axes, as well as create a legend to differentiate between different segments of the piecewise function.

Similar threads

  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
3
Views
525
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
14
Views
4K
  • Atomic and Condensed Matter
Replies
7
Views
501
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top