Python/Intersection.py
MindTraper 24bad2e273
Add files via upload
There are three methods that find approximately the roots of a non-linear function
2017-12-17 22:02:36 +02:00

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))