Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Numerical differentiation
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Code === The following is an example of a [[Python (programming language)|Python]] implementation for finding derivatives numerically for <math>f(x) = \frac{2x}{1+\sqrt{x}}</math> using the various three-point difference formulas at <math>x_{0} = 4</math>. The function <code>func</code> has derivative <code>func_prime</code>. {| role="presentation" class="wikitable mw-collapsible mw-collapsed" |'''Example implementation in [[Python (programming language)|Python]]''' |- |<syntaxhighlight lang="python3" line="1"> import math def func(x): return (2*x) / (1 + math.sqrt(x)) def func_prime(x): return (2 + math.sqrt(x)) / ((1 + math.sqrt(x))**2) def three_point_forward(value, h): return ((-3/2) * func(value) + 2*func(value + h) - (1/2)*func(value + 2*h)) / h def three_point_backward(value, h): return ((-1/2)*func(value - h) + (1/2)*func(value + h)) / h def three_point_central(value, h): return ((1/2)*func(value - 2*h) - 2*func(value - h) + (3/2)*func(value)) / h value = 4 actual = func_prime(value) print("Actual value " + str(actual)) print("============================================") for step_size in [0.1, 0.01, 0.001, 0.0001]: print("Step size " + str(step_size)) forward = three_point_forward(value, step_size) backward = three_point_backward(value, step_size) central = three_point_central(value, step_size) print("Forward {:>12}, Error = {:>12}".format(str(forward), str(abs(forward - actual)))) print("Backward {:>12}, Error = {:>12}".format(str(forward), str(abs(backward - actual)))) print("Central {:>12}, Error = {:>12}".format(str(forward), str(abs(central - actual)))) print("============================================") </syntaxhighlight> |} {| role="presentation" class="wikitable mw-collapsible mw-collapsed" |'''Output''' |- |<syntaxhighlight lang="python3" line="1"> Actual value 0.4444444444444444 ============================================ Step size 0.1 Forward 0.4443963018050967, Error = 4.814263934771468e-05 Backward 0.4443963018050967, Error = 2.5082646145202503e-05 Central 0.4443963018050967, Error = 5.231976394060034e-05 ============================================ Step size 0.01 Forward 0.4444439449793336, Error = 4.994651108258807e-07 Backward 0.4444439449793336, Error = 2.507721614808389e-07 Central 0.4444439449793336, Error = 5.036366184096863e-07 ============================================ Step size 0.001 Forward 0.4444444394311464, Error = 5.013297998957e-09 Backward 0.4444444394311464, Error = 2.507574814458735e-09 Central 0.4444444394311464, Error = 5.017960935660426e-09 ============================================ Step size 0.0001 Forward 0.4444444443896245, Error = 5.4819926376126205e-11 Backward 0.4444444443896245, Error = 2.5116131396885066e-11 Central 0.4444444443896245, Error = 5.037903427762558e-11 ============================================ </syntaxhighlight> |}
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)