mirror of
https://github.com/janishutz/eth-summaries.git
synced 2026-03-14 17:00:05 +01:00
Fix Newton's Method
The old one incorrectly uses the absolute value of the derivative
This commit is contained in:
@@ -23,9 +23,12 @@ falls $F(x^*) = 0$ und $F^(x^*) \neq 0$
|
|||||||
\begin{code}{python}
|
\begin{code}{python}
|
||||||
def newton_method(f, df, x: float, tol=1e-12, maxIter=100):
|
def newton_method(f, df, x: float, tol=1e-12, maxIter=100):
|
||||||
""" Use Newton's method to find zeros, requires derivative of f """
|
""" Use Newton's method to find zeros, requires derivative of f """
|
||||||
iter = 0
|
k = 0
|
||||||
while (np.abs(df(x)) > tol and iter < maxIter):
|
fx = f(x)
|
||||||
x -= f(x)/df(x); iter += 1
|
dfx = df(x)
|
||||||
|
while (np.abs(fx) > tol and k < maxIter):
|
||||||
|
x -= fx / dfx
|
||||||
|
k += 1
|
||||||
|
|
||||||
return x, iter
|
return x, k
|
||||||
\end{code}
|
\end{code}
|
||||||
|
|||||||
Reference in New Issue
Block a user