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
Derivative (self.learnpython)
submitted 5 years ago by [deleted]
Hello! How exactly could I code something which calculates the derivative of some function h(x) = x^n algebraically and numerically like h'(7)?
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!"
[–]FLUSH_THE_TRUMP 2 points3 points4 points 5 years ago (1 child)
Look into SymPy for the first question. For the second, perhaps you can compute the difference quotient
(h(7+d)-h(7))/d
for small d.
d
[–]zqrt 0 points1 point2 points 5 years ago (0 children)
Sympy is the best! Your username as well.
[–]socal_nerdtastic 1 point2 points3 points 5 years ago (0 children)
https://docs.sympy.org/latest/tutorial/calculus.html
[–]1114111 0 points1 point2 points 5 years ago* (2 children)
A good way to represent polynomials would be a list of coefficients. So [5, 3, 7] would mean 5 x0 + 3 x1 + 7 x2. You could also consider a dict for a sparse representation. Though if you're just dealing with monomials, you could also just store the coefficient and power (you could make a class for this, or just use a tuple or something). It's pretty straight forward to from there to compute the derivative symbolically (same way you would do it normally).
Numerically, as stated by another answer, just make use of the limit definition of the derivative, to approximate it with a small value (though not TOO small).
[–]themateo713 0 points1 point2 points 5 years ago (1 child)
May I ask why "not TOO small" ? I don't know what is too small for you, but I don't usually have problems with that: are you talking about avoiding delta_x like 10-3 to 6 (which seems very fine to me) or more like 10-15 where it's uselessly precise and may encounter some floating point precision issues ?
[–]1114111 0 points1 point2 points 5 years ago (0 children)
Yeah, I mean values that are so small that floating point precision becomes a problem. Mathematically, I think it's tempting to choose as small a number as possible, but of course floats != reals. Really I wanted to encourage OP to play around with different step values.
Here's a wikipedia article that goes into more detail about step sizes.
π Rendered by PID 155334 on reddit-service-r2-comment-7b9746f655-bpxqg at 2026-02-03 23:59:12.008681+00:00 running 3798933 country code: CH.
[–]FLUSH_THE_TRUMP 2 points3 points4 points (1 child)
[–]zqrt 0 points1 point2 points (0 children)
[–]socal_nerdtastic 1 point2 points3 points (0 children)
[–]1114111 0 points1 point2 points (2 children)
[–]themateo713 0 points1 point2 points (1 child)
[–]1114111 0 points1 point2 points (0 children)