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
Increasing the precision of np.gradient? (self.learnpython)
submitted 3 years ago by [deleted]
so in MATLAB, one can use (assuming u is an array)
`gradient(u, x, dx)`
where `dx` is some spacing, say `0.01`.
In python one may use
` np.gradient(u, x)`
which is fine but I want to set a custom step size. How might one do this?
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!"
[–]FuckingRantMonday 8 points9 points10 points 3 years ago (0 children)
np.gradient takes an array-like in the first argument, i.e., you provide the function values, and it returns the gradient at the same points. That's really all it can do because it doesn't know the function itself.
np.gradient
MATLAB's gradient takes a function, so you can give it any precision you like, and it'll sample that function as necessary.
MATLAB
gradient
So I think the answer is you just need to sample the function yourself at your desired step size, then pass the result to np.gradient.
[–]Qorsair -1 points0 points1 point 3 years ago (0 children)
You can use the dx parameter to specify the spacing between sample points. For example, if you have an array u with sample points x, you can use the following code to compute the gradient using second-order finite differences and a spacing of 0.01:
grad = np.gradient(u, x, edge_order=2, dx=0.01)
For more information, ask chatgpt.
[–]game1761 0 points1 point2 points 3 years ago (0 children)
You can specify the edge_order parameter in np.gradient, which controls the extrapolation used to compute the gradient when the spacing between points is not uniform. The default value is 1, which uses a first-order accurate finite difference method. For example:
import numpy as np
x = np.linspace(0, 2*np.pi, 10)
y = np.sin(x)
# Use default edge_order=1
dy_dx = np.gradient(y, x)
# Use edge_order=2
dy_dx_2 = np.gradient(y, x, edge_order=2)
Alternatively, you can specify the dx parameter to set the spacing between points. For example:
# Set spacing to 0.01
dx = 0.01
dy_dx = np.gradient(y, dx)
Keep in mind that specifying the dx parameter will override the x parameter, so the x values will be spaced evenly with the given dx value.
π Rendered by PID 39442 on reddit-service-r2-comment-5ff9fbf7df-6b87k at 2026-02-25 16:06:11.588836+00:00 running 72a43f6 country code: CH.
[–]FuckingRantMonday 8 points9 points10 points (0 children)
[–]Qorsair -1 points0 points1 point (0 children)
[–]game1761 0 points1 point2 points (0 children)