Old high school files. Lessson notes/codes/projects etc.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hsf/mas/newton-raphsons/approx_method.py

35 lines
739 B

3 years ago
#!/usr/bin/python
import numpy as np
def nullfunc(x):
return 0
def derive(x: float, func=nullfunc, dx=0.1):
return (func(x+dx) - func(x))/dx
def new_rap(x, func=nullfunc, dx=0.1):
new_x = None
try:
while x != new_x:
y = func(x)
der = derive(x, func, dx)
new_x = x - y/der
if new_x == x:
return new_x
else:
x = new_x
return x
except ZeroDivisionError as error:
print(f"{y=} {der=} {new_x=}")
print(error)
def myfunc(x):
return 2*x - 2*(x**2) * np.sin(x) + 0.1
if __name__ == "__main__":
out = new_rap(3, myfunc, 0.00000001)
print(f"x={out}")
print(f"f(x)={myfunc(out)}")