mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-24 13:31:07 +00:00
24bad2e273
There are three methods that find approximately the roots of a non-linear function
17 lines
429 B
Python
17 lines
429 B
Python
import math
|
|
|
|
def intersection(function,x0,x1): #function is the f we want to find its root and x0 and x1 are two random starting points
|
|
x_n = x0
|
|
x_n1 = x1
|
|
while True:
|
|
x_n2 = x_n1-(function(x_n1)/((function(x_n1)-function(x_n))/(x_n1-x_n)))
|
|
if abs(x_n2 - x_n1)<0.00001 :
|
|
return x_n2
|
|
x_n=x_n1
|
|
x_n1=x_n2
|
|
|
|
def f(x):
|
|
return math.pow(x,3)-2*x-5
|
|
|
|
print(intersection(f,3,3.5))
|