mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-03-13 09:50:19 +08:00
Merge branch 'master' of https://github.com/MindTraper/Python into MindTraper-master
This commit is contained in:
33
ArithmeticAnalysis/Bisection.py
Normal file
33
ArithmeticAnalysis/Bisection.py
Normal file
@@ -0,0 +1,33 @@
|
||||
import math
|
||||
|
||||
|
||||
def bisection(function, a, b): # finds where the function becomes 0 in [a,b] using bolzano
|
||||
|
||||
start = a
|
||||
end = b
|
||||
if function(a) == 0: # one of the a or b is a root for the function
|
||||
return a
|
||||
elif function(b) == 0:
|
||||
return b
|
||||
elif function(a) * function(b) > 0: # if none of these are root and they are both positive or negative,
|
||||
# then his algorithm can't find the root
|
||||
print("couldn't find root in [a,b]")
|
||||
return
|
||||
else:
|
||||
mid = (start + end) / 2
|
||||
while abs(start - mid) > 0.0000001: # until we achieve precise equals to 10^-7
|
||||
if function(mid) == 0:
|
||||
return mid
|
||||
elif function(mid) * function(start) < 0:
|
||||
end = mid
|
||||
else:
|
||||
start = mid
|
||||
mid = (start + end) / 2
|
||||
return mid
|
||||
|
||||
|
||||
def f(x):
|
||||
return math.pow(x, 3) - 2*x - 5
|
||||
|
||||
|
||||
print(bisection(f, 1, 1000))
|
||||
16
ArithmeticAnalysis/Intersection.py
Normal file
16
ArithmeticAnalysis/Intersection.py
Normal file
@@ -0,0 +1,16 @@
|
||||
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))
|
||||
34
ArithmeticAnalysis/LUdecomposition.py
Normal file
34
ArithmeticAnalysis/LUdecomposition.py
Normal file
@@ -0,0 +1,34 @@
|
||||
import math
|
||||
import numpy
|
||||
|
||||
def LUDecompose (table): #table that contains our data
|
||||
#table has to be a square array so we need to check first
|
||||
rows,columns=numpy.shape(table)
|
||||
L=numpy.zeros((rows,columns))
|
||||
U=numpy.zeros((rows,columns))
|
||||
if rows!=columns:
|
||||
return
|
||||
for i in range (columns):
|
||||
for j in range(i-1):
|
||||
sum=0
|
||||
for k in range (j-1):
|
||||
sum+=L[i][k]*U[k][j]
|
||||
L[i][j]=(table[i][j]-sum)/U[j][j]
|
||||
L[i][i]=1
|
||||
for j in range(i-1,columns):
|
||||
sum1=0
|
||||
for k in range(i-1):
|
||||
sum1+=L[i][k]*U[k][j]
|
||||
U[i][j]=table[i][j]-sum1
|
||||
return L,U
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
matrix =numpy.array([[2,-2,1],[0,1,2],[5,3,1]])
|
||||
L,U = LUDecompose(matrix)
|
||||
print(L)
|
||||
print(U)
|
||||
15
ArithmeticAnalysis/NeutonMethod.py
Normal file
15
ArithmeticAnalysis/NeutonMethod.py
Normal file
@@ -0,0 +1,15 @@
|
||||
def newton(function,function1,startingInt): #function is the f(x) and function1 is the f'(x)
|
||||
x_n=startingInt
|
||||
while True:
|
||||
x_n1=x_n-function(x_n)/function1(x_n)
|
||||
if abs(x_n-x_n1)<0.00001:
|
||||
return x_n1
|
||||
x_n=x_n1
|
||||
|
||||
def f(x):
|
||||
return (x**3)-2*x-5
|
||||
|
||||
def f1(x):
|
||||
return 3*(x**2)-2
|
||||
|
||||
print(newton(f,f1,3))
|
||||
Reference in New Issue
Block a user