5/5
## Newton's Method in Python We will work through a couple of examples of using Newton's method in Python. ### Example 1 Let's say we have a simple function given by the following equation: ```python def f(x): return x**2 - 2 ``` and we want to find the value of *x* for which *f(x) = 0*. Newton's method is a numerical method that allows us to approximate the solution to this equation. Here's the Python code that implements Newton's method to solve for *x*: ```python def f(x): return x**2 - 2 def df(x): return 2*x def f_solve(f, df, x0, delta): for i in range(n): print(i, x0) x1 = x0 - f(x0) / df(x0) if abs(x1 - x0) <= delta: return x1 assert False, "cannot find solution" x0 = x1 n = 1000 delta = 0.0001 x = f_solve(f, df, 1, n, delta) print("f(x) =", f(x)) print("x =", x) ``` In this code: * `f(x)` is our function, which takes a single input *x* and returns the value of *x� - 2*. * `df(x)` is the derivative of our function, which returns the value of *2x*. * `f_solve(f, df, x0, delta)` is the function that implements Newton's method. It takes the function `f`, its derivative `df`, an initial guess `x0`, and a tolerance `delta`. The function iterates until the difference between successive approximations is less than the tolerance. * `n` is the maximum number of iterations allowed. * `delta` is the tolerance value. **Running the code:** We set our initial guess to *1*, the maximum number of iterations to *1,000*, and the tolerance to *0.0001*. When we run this code, we get the following output: ```bash python newton_example.py ``` The output will show the iterations performed by the code until it finds a solution. The final value of *x* will be approximately *1.41421*, which is the square root of 2. ### Example 2 Now let's say we have a curve defined by the following equation: ```python def f(x, y, A, D): K = A * x * y / (D / 2)**2 return x * y + K - (D / 2)**2 - K * D**2 ``` We know the values of *x*, *A*, and *D*, and we want to find the value of *y* that satisfies this equation. To solve for *y*, we'll use Newton's method again. The Python code is similar to the previous example, but we need to define new functions for our curve and its derivative with respect to *y*. Here's the modified code: ```python def f(x, y, A, D): K = A * x * y / (D / 2)**2 return x * y + K - (D / 2)**2 - K * D**2 n = 100 delta = 0.0001 x = 120 y = 90 A = 10 D = 200 def f_D(d): return f(x, y, A, d) def df_d(d): return 4 * A * x * y * d / 2 - d / 2 d = f_solve(f_D, df_d, D, n, delta) print("d", d) print("f_D(d)", f_D(d)) def f_y(y): return f(x, y, A, d) def df_dy(y): return x + 4 * A * x * y * d / 2 - A * x * d * (y * d / 2) / d**2 y = f_solve(f_y, df_dy, y, n, delta) print("y", y) print("f_y(y)", f_y(y)) ``` Here, we've defined: * `f_y(y)` is the function for the curve, taking *y* as input and using the known values of *x*, *A*, and *D*. * `df_dy(y)` is the partial derivative of the curve's function with respect to *y*. **Running the code:** We'll execute this code and get the following output: ```bash python newton_example.py ``` The output shows that Newton's method finds a value for *y* that is close enough to satisfy the curve's equation. ### Conclusion Newton's method is a powerful tool for finding solutions to equations. We have seen how to use it in Python, and how to modify it to solve different problems.
We will work through a couple of examples of using Newton's method in Python.
Let's say we have a simple function given by the following equation:
and we want to find the value of x for which f(x) = 0. Newton's method is a numerical method that allows us to approximate the solution to this equation.
Here's the Python code that implements Newton's method to solve for x:
In this code:
f(x) is our function, which takes a single input x and returns the value of x� - 2.
df(x) is the derivative of our function, which returns the value of 2x.
f_solve(f, df, x0, delta) is the function that implements Newton's method. It takes the function f, its derivative df, an initial guess x0, and a tolerance delta. The function iterates until the difference between successive approximations is less than the tolerance.
n is the maximum number of iterations allowed.
delta is the tolerance value.
Running the code:
We set our initial guess to 1, the maximum number of iterations to 1,000, and the tolerance to 0.0001. When we run this code, we get the following output:
The output will show the iterations performed by the code until it finds a solution. The final value of x will be approximately 1.41421, which is the square root of 2.
Now let's say we have a curve defined by the following equation:
We know the values of x, A, and D, and we want to find the value of y that satisfies this equation.
To solve for y, we'll use Newton's method again. The Python code is similar to the previous example, but we need to define new functions for our curve and its derivative with respect to y. Here's the modified code:
Here, we've defined:
f_y(y) is the function for the curve, taking y as input and using the known values of x, A, and D.
df_dy(y) is the partial derivative of the curve's function with respect to y.
Running the code:
We'll execute this code and get the following output:
The output shows that Newton's method finds a value for y that is close enough to satisfy the curve's equation.
Newton's method is a powerful tool for finding solutions to equations. We have seen how to use it in Python, and how to modify it to solve different problems.
A practical guide to implementing Newton’s Method in Python. The lesson demonstrates how to use Newton’s Method to find the roots of a function in Python, and also how to calculate the values of a curve’s equation.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
AMM math for Curve Stableswap
How to calculate swap amount and liquidity
Curve Stableswap contracts
How to implement a swap function
How to implement the add and remove liquidity functions
How to quantify liquidity pools
How to control the flatness of the curve
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Blockchain Financial Analyst
$100,000 - $150,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Last updated on August 11, 2025
Duration: 14min
Duration: 32min
Duration: 26min
Duration: 23min
Duration: 20min
Duration: 23min
Course Overview
About the course
AMM math for Curve Stableswap
How to calculate swap amount and liquidity
Curve Stableswap contracts
How to implement a swap function
How to implement the add and remove liquidity functions
How to quantify liquidity pools
How to control the flatness of the curve
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Blockchain Financial Analyst
$100,000 - $150,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Last updated on August 11, 2025