-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathderiv.py
More file actions
37 lines (31 loc) · 1.04 KB
/
deriv.py
File metadata and controls
37 lines (31 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import sympy as sp
from typing import Callable, List
def derivative(func: Callable[[float], float], x: float):
h = sp.symbols('h')
if not callable(func):
func = sp.lambdify(sp.symbols('x'), func)
return sp.limit((func(x + h) - func(x)) / h, h, 0)
def tanline(func: Callable[[float], float], x0: float):
x = sp.symbols('x')
if not callable(func):
func = sp.lambdify(x, func)
slope = derivative(func, x).subs(x, x0)
y0 = func(x0)
return lambda x: slope * (x - x0) + y0
def newton(func: Callable[[sp.Symbol], sp.Expr], tol: float = 1e-7) -> list[Callable[[float], float]]:
a: float = 10.0
tanlist = []
x = sp.symbols('x')
resint = 0
for i in range(1, 1000):
tanl = tanline(func, a)
tanlist.append(tanl)
tanl_expr = tanl(x)
new_a = sp.solve(tanl_expr, x)[0]
resint = round(new_a)
if abs(new_a - a) < tol:
break
a = new_a
resint = None
print(f"The solve for x to equation(x) = 0 is: {resint}")
return tanlist