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
Secant method
(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!
==Computational example== Below, the secant method is implemented in the [[Python (programming language)|Python]] programming language. It is then applied to find a root of the function {{math|''f''(''x'') {{=}} ''x''<sup>2</sup> β 612}} with initial points <math>x_0 = 10</math> and <math>x_1 = 30</math> <syntaxhighlight lang="python3"> def secant_method(f, x0: int, x1: int, iterations: int) -> float: """Return the root calculated using the secant method.""" for i in range(iterations): x2 = x1 - f(x1) * (x1 - x0) / float(f(x1) - f(x0)) x0, x1 = x1, x2 # Apply a stopping criterion here (see below) return x2 def f_example(x): return x ** 2 - 612 root = secant_method(f_example, 10, 30, 5) print(f"Root: {root}") # Root: 24.738633748750722 </syntaxhighlight> It is very important to have a good stopping criterion above, otherwise, due to limited numerical precision of floating point numbers, the algorithm can return inaccurate results if running for too many iterations. For example, the loop above can stop when one of these is reached first: {{Mono|abs(x0 - x1) < tol}}, or {{Mono|abs(x0/x1-1) < tol}}, or {{Mono|abs(f(x1)) < tol}}. <ref>{{cite web | url=https://www.cfm.brown.edu/people/dobrush/am33/Matlab/ch3/secant.html | title=MATLAB Tutorial for the First Course. Part 1.3: Secant Methods }}</ref>
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)