increment 1 · coderjack/algorithms-in-python@564179a · GitHub
Skip to content

Commit 564179a

Browse files
committed
increment 1
1 parent 718b99a commit 564179a

131 files changed

Lines changed: 16252 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 38 additions & 0 deletions
26.1 KB
Loading
29.3 KB
Loading
81.9 KB
Loading

arithmetic_analysis/bisection.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import math
2+
3+
4+
def bisection(function, a, b): # finds where the function becomes 0 in [a,b] using bolzano
5+
6+
start = a
7+
end = b
8+
if function(a) == 0: # one of the a or b is a root for the function
9+
return a
10+
elif function(b) == 0:
11+
return b
12+
elif function(a) * function(b) > 0: # if none of these are root and they are both positive or negative,
13+
# then his algorithm can't find the root
14+
print("couldn't find root in [a,b]")
15+
return
16+
else:
17+
mid = (start + end) / 2
18+
while abs(start - mid) > 0.0000001: # until we achieve precise equals to 10^-7
19+
if function(mid) == 0:
20+
return mid
21+
elif function(mid) * function(start) < 0:
22+
end = mid
23+
else:
24+
start = mid
25+
mid = (start + end) / 2
26+
return mid
27+
28+
29+
def f(x):
30+
return math.pow(x, 3) - 2*x - 5
31+
32+
33+
print(bisection(f, 1, 1000))
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import math
2+
3+
def intersection(function,x0,x1): #function is the f we want to find its root and x0 and x1 are two random starting points
4+
x_n = x0
5+
x_n1 = x1
6+
while True:
7+
x_n2 = x_n1-(function(x_n1)/((function(x_n1)-function(x_n))/(x_n1-x_n)))
8+
if abs(x_n2 - x_n1)<0.00001 :
9+
return x_n2
10+
x_n=x_n1
11+
x_n1=x_n2
12+
13+
def f(x):
14+
return math.pow(x,3)-2*x-5
15+
16+
print(intersection(f,3,3.5))
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import numpy
2+
3+
def LUDecompose (table):
4+
#table that contains our data
5+
#table has to be a square array so we need to check first
6+
rows,columns=numpy.shape(table)
7+
L=numpy.zeros((rows,columns))
8+
U=numpy.zeros((rows,columns))
9+
if rows!=columns:
10+
return
11+
for i in range (columns):
12+
for j in range(i-1):
13+
sum=0
14+
for k in range (j-1):
15+
sum+=L[i][k]*U[k][j]
16+
L[i][j]=(table[i][j]-sum)/U[j][j]
17+
L[i][i]=1
18+
for j in range(i-1,columns):
19+
sum1=0
20+
for k in range(i-1):
21+
sum1+=L[i][k]*U[k][j]
22+
U[i][j]=table[i][j]-sum1
23+
return L,U
24+
25+
26+
27+
28+
29+
30+
31+
matrix =numpy.array([[2,-2,1],[0,1,2],[5,3,1]])
32+
L,U = LUDecompose(matrix)
33+
print(L)
34+
print(U)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def newton(function,function1,startingInt): #function is the f(x) and function1 is the f'(x)
2+
x_n=startingInt
3+
while True:
4+
x_n1=x_n-function(x_n)/function1(x_n)
5+
if abs(x_n-x_n1)<0.00001:
6+
return x_n1
7+
x_n=x_n1
8+
9+
def f(x):
10+
return (x**3)-2*x-5
11+
12+
def f1(x):
13+
return 3*(x**2)-2
14+
15+
print(newton(f,f1,3))
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Implementing Newton Raphson method in Python
2+
# Author: Haseeb
3+
4+
from sympy import diff
5+
from decimal import Decimal
6+
7+
def NewtonRaphson(func, a):
8+
''' Finds root from the point 'a' onwards by Newton-Raphson method '''
9+
while True:
10+
c = Decimal(a) - ( Decimal(eval(func)) / Decimal(eval(str(diff(func)))) )
11+
12+
a = c
13+
14+
# This number dictates the accuracy of the answer
15+
if abs(eval(func)) < 10**-15:
16+
return c
17+
18+
19+
# Let's Execute
20+
if __name__ == '__main__':
21+
# Find root of trigonometric function
22+
# Find value of pi
23+
print ('sin(x) = 0', NewtonRaphson('sin(x)', 2))
24+
25+
# Find root of polynomial
26+
print ('x**2 - 5*x +2 = 0', NewtonRaphson('x**2 - 5*x +2', 0.4))
27+
28+
# Find Square Root of 5
29+
print ('x**2 - 5 = 0', NewtonRaphson('x**2 - 5', 0.1))
30+
31+
# Exponential Roots
32+
print ('exp(x) - 1 = 0', NewtonRaphson('exp(x) - 1', 0))
33+
34+
35+
36+
Lines changed: 116 additions & 0 deletions

0 commit comments

Comments
 (0)