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
ChiralSuperfields
1,223
132
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:
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 ChiralSuperfields
  • #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. What is a piecewise function?

A piecewise function is a function that is defined by different rules or equations for different intervals of the input variable. This allows us to represent a function that behaves differently in different parts of its domain.

2. How do I graph a piecewise function in Python?

To graph a piecewise function in Python, you can use the matplotlib library. First, define the different equations or rules for each interval of the function. Then, use the "plt.plot()" function to plot each interval separately, and use the "plt.show()" function to display the graph.

3. Can I graph a piecewise function with more than two intervals?

Yes, you can graph a piecewise function with as many intervals as needed. Simply define the equations or rules for each interval and plot them separately using the "plt.plot()" function. Make sure to include all intervals in the final graph.

4. How do I add labels and a title to my piecewise function graph in Python?

To add labels and a title to your graph, you can use the "plt.xlabel()", "plt.ylabel()", and "plt.title()" functions. These functions allow you to specify the labels and title for the x-axis, y-axis, and overall graph, respectively.

5. Can I change the style and color of each interval in a piecewise function graph?

Yes, you can change the style and color of each interval in a piecewise function graph by specifying the "color" and "linestyle" parameters in the "plt.plot()" function. You can also add a legend to your graph using the "plt.legend()" function to differentiate between the different intervals.

Similar threads

  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
2
Views
914
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
3
Views
341
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
14
Views
4K
  • Programming and Computer Science
Replies
1
Views
901
  • Programming and Computer Science
Replies
4
Views
3K
Back
Top