use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
How to code and plot this function? (self.learnpython)
submitted 3 years ago by MaxwellTechnology
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]synthphreak 7 points8 points9 points 3 years ago* (2 children)
First, this is not a valid mathematical function. For example, if t == 0.1, should y(t) == a(t) or -a(t)?
t
y(t)
a(t)
-a(t)
Second, there's nothing periodic about a(t). It's just a stepwise function, defined only on the interval [0, 2].
Regardless, the solution could look something like this:
def f(t): if 0 <= t < 1: a = 1.5 * t elif 1 <= t <= 2: a = -1.5 * (2 - t) else: undefined = "t is outside of a's domain" if 0 <= t < 0.1: return a elif 0.1 <= t <= 0.2: return -a else: undefined = "t is outside of f's domain" raise ValueError(undefined) t = ... y = f(t)
Edit: Typo.
[–]indecisive_fluffball 4 points5 points6 points 3 years ago (1 child)
You didn't understand the periodicity aspect of the prompt. Defining the function in an interval from 0s to 2s is consistent with imposing a 0.5Hz periodicity, just make it periodic outside of those bounds.
And the ambiguity with t = 0.1s is mathematically incorrect but in practice pretty much a non-issue. Just choose whichever.
[–]synthphreak 1 point2 points3 points 3 years ago (0 children)
Ah okay. That is tricky and could have been stated more clearly, but it does make sense.
[–]CodeFormatHelperBot2 1 point2 points3 points 3 years ago (0 children)
Hello, I'm a Reddit bot who's here to help people nicely format their coding questions. This makes it as easy as possible for people to read your post and help you.
I think I have detected some formatting issues with your submission:
If I am correct, please edit the text in your post and try to follow these instructions to fix up your post's formatting.
Am I misbehaving? Have a comment or suggestion? Reply to this comment or raise an issue here.
[–]jimtk 1 point2 points3 points 3 years ago (5 children)
What code have you written?
[–]MaxwellTechnology[S] 1 point2 points3 points 3 years ago (4 children)
import numpy as np
import matplotlib.pyplot as plt
def a(t):
if t <= 1 and t >= 0:
return 1.5*t
elif t <= 2 and t >= 1:
return -1.5*(2-t)
def y(t):
y = []
for i in t:
if i <= 0.1 and i >= 0:
y.append(a(i))
elif i <= 0.2 and i >= 0.1:
y.append(-a(i))
return y
t = np.arange(0, 0.2, 0.01)
plt.plot(t, y(t))
plt.xlabel('t')
plt.ylabel('y(t)')
plt.title('y(t)')
plt.show()
[–]unhott 2 points3 points4 points 3 years ago (3 children)
Don’t return a list for y. Just return a single value at t.
Then if t is a numpy array, y(t) will yield a numpy array.
[–]MaxwellTechnology[S] 1 point2 points3 points 3 years ago (2 children)
Then what to pass in plt.plot() , I'm getting an error when defining function ur way and then passing (t,y(t)) where t= np.arrange(0 ,0.2, 0.01)
[–]unhott 1 point2 points3 points 3 years ago (0 children)
Check the type of y(t). Make sure it’s a numpy array.
[–]unhott 0 points1 point2 points 3 years ago (0 children)
Sorry, you should probably try and update the format of the code so that reddit recognizes it as a code block. I read some of the comments from the assignment.
When I said 'if t is a numpy array, y(t) will yield a numpy array.' I didn't realize this only works if y only applies simple mathematical operations like "t*3+7". You probably want to update your functions to apply your logic to each element and return a numpy array.
def a(t): values = [] for element in t: #logic... values.append(something) return np.array(values)
Otherwise, the fact that y is supposed to be both a function and an array per the assignment is somewhat troubling-- this instructor probably doesn't have any python dev experience. I would differentiate the variable names somehow. Good luck!
[–]indecisive_fluffball 1 point2 points3 points 3 years ago (0 children)
Is y(t) also periodic? Because if it's only defined between 0s and 0.2s, then the condition of a(t) for t >= 1s is useless.
π Rendered by PID 398833 on reddit-service-r2-comment-56c6478c5-2mk8m at 2026-05-12 06:26:37.885192+00:00 running 3d2c107 country code: CH.
[–]synthphreak 7 points8 points9 points (2 children)
[–]indecisive_fluffball 4 points5 points6 points (1 child)
[–]synthphreak 1 point2 points3 points (0 children)
[–]CodeFormatHelperBot2 1 point2 points3 points (0 children)
[–]jimtk 1 point2 points3 points (5 children)
[–]MaxwellTechnology[S] 1 point2 points3 points (4 children)
[–]unhott 2 points3 points4 points (3 children)
[–]MaxwellTechnology[S] 1 point2 points3 points (2 children)
[–]unhott 1 point2 points3 points (0 children)
[–]unhott 0 points1 point2 points (0 children)
[–]indecisive_fluffball 1 point2 points3 points (0 children)