SlideShare a Scribd company logo
Numerical Analysis – Hakaton 1
3.12.2018
Group 19:
Shirel Biton 207276452 Liz Ohayon 209636794
Shani Waizman 316440262 Liat Golber 313301129
Yakir Kobaivanov 313566390
Mentors:
-Professor Shlomo Mark.
-Mrs. Eti Amitai.
Numerical Methods
Finding Roots Solving Matrices
Approximations
Back
Numerical Methods – Finding Roots
Bisection
Newton -
Raphson
Secant
Back
Utility
Numerical Methods – Solving Matrices
Gauss-Seidel
SOR
Back
Utility
Numerical Methods – Approximations
Polynomial
Back
Polynomial Approximation Method
Background info: https://en.wikipedia.org/wiki/Approximation_theory
Full Code: PolynomialApprox.txt
Used libraries: NumPy.
User Guide: Click me.
Back
Polynomial Approximation Method
Code:
def polynomial_approx(x, y):
mat = []
row = []
for j in range(len(x)):
for i in range(len(x)):
z = lambda y : y ** (len(x) -i - 1)
row.append(z(x[j]))
mat.append(row)
row = []
mat = np.matrix(mat)
y = [[10.5],[6.1], [3.5]]
mat,y= calcDominant(mat,np.mat(y))
return SOR(mat,y)
def polynomial_calc(p, num, pow):
sum = 0
for i in range(pow + 1):
sum = sum + p[i].item(0,0) * (num ** (pow - i))
return sum
Back
Finding Roots – Utility
Background info: https://en.wikipedia.org/wiki/Derivative
Full Code: We used NumPy’s built in derivative function called “diff” for both
Newton-Raphson and Bisection methods.
The function receives two vectors representing the x and y coordinates and a
degree. The function returns a vector of coefficients that minimizes the squared
error.
Used libraries: NumPy.
diff API: Click me.
Back
Finding Roots - Utility
Our Newton-Raphson and Bisection methods used a NumPy built in function
called “diff”
https://docs.scipy.org/doc/numpy/reference/generated/numpy.diff.html
Back
Bisection Method
Background info: https://en.wikipedia.org/wiki/Bisection_method
Full Code: Bisection.txt
Used libraries: NumPy, SymPy, SciPy , matplotlib.
User Guide: Click me.
Back
Bisection Method – User Guide
Functions:
• f
• bisection
• derivative
• roots
• all_roots
• intervals
Back
Bisection Method – function “f”
def f(symbolic_fuction):
return lambdify(x, symbolic_fuction)
The function receives a
symbolic mathematical
expression and returns a
mathematical function.
Back
Bisection Method – function “bisection”pt.1
Code:
def bisection(a, b, iteration, function, epsilon):
try:
i=0
if(isinstance(function(a),complex) == True
or isinstance(function(b),complex) == True):
return "none"
else:
if function(a) * function(b) <= 0 :
c = (a + b) / 2
while(abs(b - a) > epsilon or
iteration == 0 ) :
if(function(a) == 0):
return a
if(function(b) == 0):
return b
if(function(c) == 0):
return c
The function receives a range ,
amount of iterations, a mathematical
function and an epsilon representing
how close we want our answer to be
and returns the root of the received
function in the given range , or none
in case it doesn’t have a root.
BackNext
Bisection Method – function “bisection”pt.2
Code:
elif(function(a) * function(c) > 0):
a = c
elif(function(c) * function(b) > 0):
b = c
iteration = iteration - 1
c = (a + b) / 2
print("number iteration:", i)
print("Approximation:", c)
i += 1
return c
else:
return "none"
except ValueError:
return "none"
except ZeroDivisionError:
return "none"
except DomainError:
return "none"
Back
Bisection Method – function “derivative”
Code:
f_tag = diff(symbolic_fuction)
return lambdify(x, f_tag, 'numpy')
The function receives a
mathematical expression and
returns its derivative form.
Back
Bisection Method – function “roots”
Code:
count = 0
j = 0
rootList = []
for k in ranging:
if( j == len(ranging) - 1):
return rootList
tmp = bisection(ranging[j], ranging[j + 1], iteration, function, epsilon)
if (not(tmp == "none")):
if(len(rootList) > 0):
if(tmp + 2 * epsilon >= rootList[count - 1] or tmp - 2 * epsilon<= rootList[count - 1] ):
count = count + 1
rootList.append(tmp)
else:
count = count + 1
rootList.append(tmp)
j = j + 1
return rootList
The function receives a range , amount of
iterations, a mathematical function and an
epsilon representing how close we want
our answer to be and returns all the roots
of the function in the given range (without
the roots of its derivative)
Back
Bisection Method – function “all_roots”
Code:
def all_roots(ranging, fx, fx_tag, iteration, epsilon):
fx_list = []
fx_tag_list = []
root_list = []
merged_list = []
fx_list = roots(ranging, fx, iteration, epsilon)
fx_tag_list = roots(ranging, fx_tag, iteration, epsilon)
almostE = 0.00001
for i in fx_tag_list:
if(fx(i) <= almostE and fx(i) >= -almostE):
root_list.append(i)
for i in root_list:
for j in fx_list:
if (j <= i + almostE and j >= i - almostE):
continue
else:
merged_list.append(j)
for i in merged_list:
fx_list += i
return fx_list
The function receives a range , amount
of iterations, a mathematical function
and an epsilon representing how close
we want our answer to be and returns
the roots of the function’s derivative
form with the function roots.
Back
Bisection Method – function “intervals”
Code:
def intervals(Start_Domain, End_Domain,
interval_jump):
domains = []
parameter = Start_Domain
tmp = (End_Domain - Start_Domain) /
interval_jump
for i in range(int(tmp)):
domains.append(parameter)
parameter += interval_jump
domains.append(End_Domain)
return domains
The function receives a range and
returns a list of intervals
Back
Newton - Raphson Method
Background info: https://en.wikipedia.org/wiki/Newton%27s_method
Full Code: Newton_Raphson.txt
Used libraries: NumPy, SymPy, cmath.
User Guide: Click me.
Back
Newton-Raphson Method– User Guide
Functions:
• func
• derivative
• find_intervals
• NR
• checkVariable
• checkRoot
• Print_roots
• all_roots
Back
Newton-Raphson Method – function
“func”
Code:
def func(symbolic_fuction)
return lambdify(x, symbolic_fuction, 'numpy')
The function receives a
symbolic mathematical
expression and returns its
derivative form.
Back
Newton-Raphson Method – function
“derivative”
Code:
def derivative(symbolic_fuction)
g_tagx = diff(symbolic_fuction)
return lambdify(x, g_tagx, 'numpy')
The function receives a
symbolic mathematical
expression and returns its
derivative form.
Back
Newton-Raphson Method – function “find
intervals”
Code:
def find_intervals(rangex)
intervals = []
j = rangex[0]
while j = rangex[1]
intervals.append([j, j + 0.05])
j = j + 0.05
return intervals
The function a range and
returns a list of intervals
Back
Newton-Raphson Method – function
“make_intervals”
Code:
intervals = []
j = rangex[0]
for _ in range((abs(rangex[1])+abs(rangex[0])) * 2):
intervals.append([j, j + 0.5])
j = j + 0.5
return intervals
Back
The function separates the
range into smaller intervals
Newton-Raphson Method – function “NR”
Code:
def NR(f, f_tag, x,eps, itration,printList)
i=0
while abs(f(x)) eps or itration == 0
if f_tag(x) == 0
return None
x = x - f(x) f_tag(x)
printList.append((x, i))
i += 1
itration -= 1
return x
The function receives the
function and its derivative
form,a range and an epsilon and
returns a root in the given
range.
Back
Newton-Raphson Method – function
“checkVariable”
Code:
def checkVariable(root, roots, eps)
for i in roots
if abs(i - root) eps
return False
return True
The function receives roots and
an epsilon and checks if the
solutions we found are close
enough.
Back
Newton-Raphson Method – function
“checkRoot”
Code:
def checkRoot(f,root, eps):
return abs(f(root)) < eps
The function receives a function
and roots and epsilon and
returns whether the solution is
close enough.
Back
Newton-Raphson Method – function
“Print_roots”
Code:
def Print_roots(printList)
for i in printList
print(number iteration, i[1])
print(Approximation, i[0])
The function receives a a list of
roots and prints them.
Back
Newton-Raphson Method – function
“all_roots” pt.1
Code:
def all_roots(f, f_tag, eps, rangex, itration)
roots = []
intervals = find_roots(rangex)
for i in intervals
printList=[]
x = (i[0] + i[1]) 2
root = NR(f, f_tag, x, eps, itration, printList)
if root != None
if (len(roots)==0)and root = rangex[1] and root = rangex[0] and checkRoot(f, root, eps)
roots.append(root)
BackNext
The function calculates all the
roots (function and derivative)
in the given range or none in
case they don’t exist.
Newton-Raphson Method – function
“all_roots” pt.2
Continution code:
Print_roots(printList)
else
if root == None
continue
if (not checkVariable(root, roots, 0.1))
continue
if f(root) == None
continue
elif root = rangex[1] and root = rangex[0] and checkRoot(f, root, eps)
roots.append(root)
Print_roots(printList)
if len(roots) == 0
return None
roots.sort()
return roots
Back
Secant Method
Background info: https://en.wikipedia.org/wiki/Secant_method
Full Code: Secant.txt
Used libraries: NumPy, SymPy, cmath.
User Guide: Click me.
Back
Secant Method– User Guide
Functions:
• func
• find_intervals
• secant
• checkVariable
• checkRoot
• Print_roots
• all_roots
Back
Secant Method – function “func”
Code:
def func(symbolic_fuction)
return lambdify(x, symbolic_fuction, 'numpy')
The function receives a
symbolic mathematical
expression and returns its
derivative form.
Back
Secant– function “find intervals”
Code:
def find_intervals(rangex)
intervals = []
j = rangex[0]
while j = rangex[1]
intervals.append([j, j + 0.05])
j = j + 0.05
return intervals
The function a range and
returns a list of intervals
Back
Secant Method – function “secant”
Code:
def secant(rangex, fx, x0, x1, eps, itration,printList):
i=0
while abs(x1 - x0) > eps or itration == 0:
if abs(fx(x1) - fx(x0)) < eps:
return None
x = x1 - fx(x1) * ((x1 - x0)/(fx(x1) - fx(x0)))
printList.append((x,i))
i += 1
x0 = x1
x1 = x
itration -= 1
return x
The function receives the
function and its derivative
form,a range and an epsilon and
returns a root in the given
range.
Back
Secant Method – function
“checkVariable”
Code:
def checkVariable(root, roots, eps)
for i in roots
if abs(i - root) eps
return False
return True
The function receives roots and
an epsilon and checks if the
solutions we found are close
enough.
Back
Secant Method – function “checkRoot”
Code:
def checkRoot(f,root, eps):
return abs(f(root)) < eps
The function receives a function
and roots and epsilon and
returns whether the solution is
close enough.
Back
Secant Method – function “Print_roots”
Code:
def Print_roots(printList)
for i in printList
print(number iteration, i[1])
print(Approximation, i[0])
The function receives a a list of
roots and prints them.
Back
Secant Method – function “all_roots”
pt.1
Code:
def all_roots(f, f_tag, eps, rangex, itration)
roots = []
intervals = find_roots(rangex)
for i in intervals
printList=[]
x = (i[0] + i[1]) 2
root = NR(f, f_tag, x, eps, itration, printList)
if root != None
if (len(roots)==0)and root = rangex[1] and root = rangex[0] and checkRoot(f, root, eps)
roots.append(root)
BackNext
The function calculates all the
roots (function and derivative)
in the given range or none in
case they don’t exist.
Secant Method – function “all_roots”
pt.2
Continution code:
Print_roots(printList)
else
if root == None
continue
if (not checkVariable(root, roots, 0.1))
continue
if f(root) == None
continue
elif root = rangex[1] and root = rangex[0] and checkRoot(f, root, eps)
roots.append(root)
Print_roots(printList)
if len(roots) == 0
return None
roots.sort()
return roots
Back
SOR Method
Background info: https://en.wikipedia.org/wiki/Successive_over-relaxation
Full Code: SOR.txt
Used libraries: NumPy.
User Guide: Click me.
Back
SOR Method– User Guide
Functions:
• SOR
Back
SOR Method – function “is_close”
Code:
def is_close(line, b, result, eps):
sum = 0
for i in range(line.getA().size):
sum = sum +(result[i] * line.item(0,i))
if abs(sum - b) < eps:
return True
return False
The function receives results
from the SOL method and checks
if they are close enough to the
original matrix’s results.
Back
SOR Method – function “SOR” pt.1
Code:
def SOR(matrix, b):
flag = 0
w = 0.05
while flag == 0 and w <= 2:
printList = []
D = create_D(matrix)
L = create_L(matrix)
U = create_U(matrix)
W_L = multi_scalar(L, w)
D_WL = sum_m(W_L, D)
W_D = multi_scalar(D, 1 - w)
W_U = multi_scalar(U, w)
new_b = []
for i in range(len(b)):
new_b.append(b[i] * w)
result = []
BackNext
The function solves a given
matrix using the SOR method and
returns the results.
SOR Method – function “SOR” pt.2
for i in range(len(matrix)):
m = []
for j in range(0, i):
m.append(-D_WL.item(i,j))
m.append(W_D.item(i,i))
for k in range(i + 1, len(matrix)):
m.append(-W_U.item(i, k))
result.append(m)
mat = np.matrix(result)
vector = []
for i in range(len(matrix)):
vector.append(D.item(i, i))
BackNext
SOR Method – function “SOR” pt.3
Code continuation:
vector = []
for i in range(len(matrix)):
vector.append(D.item(i, i))
r = iterative(mat, vector, new_b, printList)
close = True
for i in range(len(matrix)):
if is_close(matrix[i], b[i], r, 0.0001) == False:
close = False
if close == False:
w = w + 0.05
else:
flag = 1
PrintList(printList, len(b))
return r
Back
Gauss-Seidel Method
Background info: https://en.wikipedia.org/wiki/Gauss%E2%80%93Seidel_method
Full Code: Gauss-Seidel.txt
Used libraries: NumPy.
User Guide: Click me.
Back
Gauss-Seidel Method– User Guide
Functions:
• gauss-seidel
Back
Gauss-Seidel Method– function “gauss-seidel”
Code:
def gaus(matrix, b):
D = create_D(matrix)
L = create_L(matrix)
U = create_U(matrix)
L_D = sum_m(L, D)
L_D_INVERSE = (L_D.I)
L_D_U = np.matmul(L_D_INVERSE, U)
L_D_b = np.matmul(L_D_INVERSE, b)
L_D_U_1 = multi_scalar(L_D_U,-1)
new_b = []
for i in range(len(b)):
new_b.append(1.0)
L_D_b = L_D_b.getA()[0]
printList = []
r = iterative(L_D_U_1, new_b ,L_D_b, printList)
PrintList(printList)
return r
Back
The function solves a given
matrix using the Gauss-Seidel
method and returns the results.
Utility
Full Code: Utility.txt
Used libraries: NumPy.
User Guide: Click me.
Back
Utility– User Guide
Functions:
• iterative
• StrongcalcDominant
• calcDominant
• sum_m
• multi_m
• multi_scalar
• normMax
• create_D
• create_L
• create_U
• PrintList
Back
Utility – function “iterative” pt.1
def iterative(matrix, b, c, printList):
result = []
sum = 0
count = 0
k = 0
flag = 0
flag2 = 0
for i in range(len(matrix)):
result.append(0)
while(flag2 == 0 and count <= 50):
for i in range(len(matrix)):
for j in range(len(matrix)):
sum = sum + (matrix.item(i, j)*result[j])
sum = sum + c[i]
if b[i] > 0.001:
The function receives a matrix
and two vectors and returns the
matrix’s solution.
BackNext
Utility – function “iterative” pt.2
sum = sum / b[i]
if abs(sum - result[i]) > 0.0000001:
flag = 1
result[i] = sum
sum = 0
if flag == 0:
flag2 = 1
else:
flag = 0
count = count + 1
printList.append(count)
for j in result:
printList.append(j)
return result
Back
Utility – function “StrongcalcDominant”
def StrongcalcDominant(mat):
mat = np.matrix(mat)
newOrder = list(range(len(mat)))
found = False
for n in range(len(mat)):
for m in range(n, len(mat)):
if (abs(mat[n, m]) > abs(np.sum(mat[n, :]) -
mat[n, m])):
newOrder.pop(m)
newOrder.insert(n, m)
found = True
if not found:
return None
mat = mat[:, newOrder]
newOrder = list(range(len(mat)))
found=False
return mat
The function receives a matrix,
checks if it is diagonally dominant
and if not , tries to force it into
strong diagonally dominant.
Returns the new order of the
matrix if it can be changed or
“None” in case it couldn’t.
Back
Utility – function “calcDominant”
def calcDominant(mat,b):
mat = np.matrix(mat)
newOrder = list(range(len(mat)))
found = False
for m in range(len(mat)):
for n in range(m, len(mat)):
if (abs(mat[n, m]) > abs(np.sum(mat[n:, m]) -
mat[n, m])):
newOrder.pop(n)
newOrder.insert(m, n)
found = True
break
if not found:
return None
mat = mat[newOrder,:]
b=b[newOrder]
newOrder = list(range(len(mat)))
found=False
return mat,b
The function receives a
matrix,and vector b checks if it is
diagonally dominant and if not ,
tries to force it into diagonally
dominant. Returns the new order
of the matrix if it can be changed
or “None” in case it couldn’t.
Back
Utility – function “sum_m”
Code:
def sum_m(mat1, mat2):
result = []
for i in range(len(mat1)):
mylist = []
for j in range(len(mat2)):
mylist.append(0.0)
result.append(mylist)
mat = np.matrix(result)
for i in range(len(mat1)):
for j in range(len(mat2)):
mat.itemset((i, j), (mat1.item(i, j) +
mat2.item(i, j)))
new_mat = np.matrix(mat)
return new_mat
The function receives two
matrices and returns the
calculated sum of the two.
Back
Utility – function “multi_m” pt.1
Code:
def multi_m(mat1, mat2):
result = []
for i in range(len(mat1)):
mylist = []
for j in range(len(mat1)):
mylist.append(0.0)
result.append(mylist)
mat = np.matrix(result)
BackNext
The function receives two
matrices and returns the
calculated multiply of the two.
Utility - function “multi_m” pt.2
Code continuation:
for i in range(len(mat1)):
for j in range(len(mat1)):
for k in range(len(mat1)):
mat.itemset((i, j), (mat1.item(i, k) +
mat2.item(k, j)))
new_mat = np.matrix(mat)
return new_mat
Back
Utility – function “multi_scalar”
Code:
def multi_scalar(mat, num):
result = []
for i in range(len(mat)):
mylist = []
for j in range(len(mat)):
mylist.append(mat.item(i, j) * num)
result.append(mylist)
new_mat = np.matrix(result)
return new_mat
Back
The function receives a matrix
and a scalar and returns the
multiply of the scalar with the
matrix.
Utility – function “normMax”
Code:
def normMax(mat):
sum = 0
list = []
for i in range(len(mat)):
for j in range(len((mat.getA()[0]))):
sum = sum + abs(mat.item(i,j))
list.append(sum)
sum = 0
return(max(list))
Back
The function receives a matrix
and returns the matrix’s norm .
Utility – function “create_D”
Code:
def create_D(matrix):
result = []
for i in range(len(matrix)):
m = []
for j in range(len(matrix)):
if i == j:
m.append(float(matrix.item(i, j)))
else:
m.append(0.0)
result.append(m)
D = np.matrix(result)
return D Back
The function receives a matrix
and creates a diagonally matrix
from it.
Utility – function “create_L”
Code:
def create_L(matrix):
result = []
for i in range(len(matrix)):
m = []
for j in range(len(matrix)):
if i > j:
m.append(float(matrix.item(i, j)))
else:
m.append(0.0)
result.append(m)
L = np.matrix(result)
return L Back
The function receives a matrix
and creates a lower triangular
matrix from it.
Utility – function “create_U”
Code:
def create_U(matrix):
result = []
for i in range(len(matrix)):
m = []
for j in range(len(matrix)):
if i < j:
m.append(float(matrix.item(i, j)))
else:
m.append(0.0)
result.append(m)
U = np.matrix(result)
return U
Back
The function receives a matrix
and creates a lower triangular
matrix from it.
Utility – function “PrintList”
Code:
def PrintList(printList, num):
for i in range(0 ,len(printList), num + 1):
print("number iteration:", printList[i])
for j in range(1 , num + 1):
print("x", j, ": ", printList[i + j])
Back
The function receives a list that
includes a matrix’s solutions and
prints it.

More Related Content

PDF
Arrays in python
PDF
Arrays In Python | Python Array Operations | Edureka
PPTX
18. Java associative arrays
PDF
Functional programming from its fundamentals
PDF
Python Workshop Part 2. LUG Maniapl
PDF
Python programming : Arrays
PDF
Python programming : Strings
PPTX
Mixing functional programming approaches in an object oriented language
Arrays in python
Arrays In Python | Python Array Operations | Edureka
18. Java associative arrays
Functional programming from its fundamentals
Python Workshop Part 2. LUG Maniapl
Python programming : Arrays
Python programming : Strings
Mixing functional programming approaches in an object oriented language

What's hot (20)

PDF
Python programming : List and tuples
PDF
Python_ 3 CheatSheet
PPTX
List in Python
PPTX
Introduction to python programming 1
PDF
Python data handling notes
PPTX
Introduction to python programming 2
PPTX
16. Arrays Lists Stacks Queues
PPTX
Python programming for Beginners - II
PDF
List , tuples, dictionaries and regular expressions in python
PPTX
Chapter 14 strings
PDF
Python Cheat Sheet
PPT
9781439035665 ppt ch09
PPTX
18. Dictionaries, Hash-Tables and Set
PPTX
Chapter 15 Lists
PDF
Python :variable types
PPTX
Basic data structures in python
PDF
Introduction to haskell
PDF
Datatypes in python
PDF
C Language Lecture 20
Python programming : List and tuples
Python_ 3 CheatSheet
List in Python
Introduction to python programming 1
Python data handling notes
Introduction to python programming 2
16. Arrays Lists Stacks Queues
Python programming for Beginners - II
List , tuples, dictionaries and regular expressions in python
Chapter 14 strings
Python Cheat Sheet
9781439035665 ppt ch09
18. Dictionaries, Hash-Tables and Set
Chapter 15 Lists
Python :variable types
Basic data structures in python
Introduction to haskell
Datatypes in python
C Language Lecture 20
Ad

Similar to Numerical analysisgroup19 (20)

PPTX
Finding root of equation (numarical method)
DOCX
Numerical Methods Lab - Shakil Anower Samrat
PDF
PPT
Numerical method
PDF
Methods of calculate roots of equations
PPTX
Numerical Techniques
PDF
Introduction to comp.physics ch 3.pdf
PPTX
ROOTS OF NON LINEAR FUNCTION (BISECTION AND NEWTON RAPHSON).pptx
PPT
Newton Raphson Method.ppt
PDF
Solution of non-linear equations
PDF
Non linearequationsmatlab
PDF
Non linearequationsmatlab
PPTX
PPT
Applications of numerical methods
PPT
Bisection method
DOCX
Roots of equations
DOCX
Equations root
PDF
Numerical methods course project report
Finding root of equation (numarical method)
Numerical Methods Lab - Shakil Anower Samrat
Numerical method
Methods of calculate roots of equations
Numerical Techniques
Introduction to comp.physics ch 3.pdf
ROOTS OF NON LINEAR FUNCTION (BISECTION AND NEWTON RAPHSON).pptx
Newton Raphson Method.ppt
Solution of non-linear equations
Non linearequationsmatlab
Non linearequationsmatlab
Applications of numerical methods
Bisection method
Roots of equations
Equations root
Numerical methods course project report
Ad

Recently uploaded (20)

PPT
Mechanical Engineering MATERIALS Selection
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
OOP with Java - Java Introduction (Basics)
PDF
PPT on Performance Review to get promotions
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
Well-logging-methods_new................
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
UNIT 4 Total Quality Management .pptx
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
Lecture Notes Electrical Wiring System Components
Mechanical Engineering MATERIALS Selection
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
CYBER-CRIMES AND SECURITY A guide to understanding
OOP with Java - Java Introduction (Basics)
PPT on Performance Review to get promotions
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Foundation to blockchain - A guide to Blockchain Tech
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Model Code of Practice - Construction Work - 21102022 .pdf
bas. eng. economics group 4 presentation 1.pptx
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Well-logging-methods_new................
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
UNIT 4 Total Quality Management .pptx
Automation-in-Manufacturing-Chapter-Introduction.pdf
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Lecture Notes Electrical Wiring System Components

Numerical analysisgroup19

  • 1. Numerical Analysis – Hakaton 1 3.12.2018 Group 19: Shirel Biton 207276452 Liz Ohayon 209636794 Shani Waizman 316440262 Liat Golber 313301129 Yakir Kobaivanov 313566390 Mentors: -Professor Shlomo Mark. -Mrs. Eti Amitai.
  • 2. Numerical Methods Finding Roots Solving Matrices Approximations Back
  • 3. Numerical Methods – Finding Roots Bisection Newton - Raphson Secant Back Utility
  • 4. Numerical Methods – Solving Matrices Gauss-Seidel SOR Back Utility
  • 5. Numerical Methods – Approximations Polynomial Back
  • 6. Polynomial Approximation Method Background info: https://en.wikipedia.org/wiki/Approximation_theory Full Code: PolynomialApprox.txt Used libraries: NumPy. User Guide: Click me. Back
  • 7. Polynomial Approximation Method Code: def polynomial_approx(x, y): mat = [] row = [] for j in range(len(x)): for i in range(len(x)): z = lambda y : y ** (len(x) -i - 1) row.append(z(x[j])) mat.append(row) row = [] mat = np.matrix(mat) y = [[10.5],[6.1], [3.5]] mat,y= calcDominant(mat,np.mat(y)) return SOR(mat,y) def polynomial_calc(p, num, pow): sum = 0 for i in range(pow + 1): sum = sum + p[i].item(0,0) * (num ** (pow - i)) return sum Back
  • 8. Finding Roots – Utility Background info: https://en.wikipedia.org/wiki/Derivative Full Code: We used NumPy’s built in derivative function called “diff” for both Newton-Raphson and Bisection methods. The function receives two vectors representing the x and y coordinates and a degree. The function returns a vector of coefficients that minimizes the squared error. Used libraries: NumPy. diff API: Click me. Back
  • 9. Finding Roots - Utility Our Newton-Raphson and Bisection methods used a NumPy built in function called “diff” https://docs.scipy.org/doc/numpy/reference/generated/numpy.diff.html Back
  • 10. Bisection Method Background info: https://en.wikipedia.org/wiki/Bisection_method Full Code: Bisection.txt Used libraries: NumPy, SymPy, SciPy , matplotlib. User Guide: Click me. Back
  • 11. Bisection Method – User Guide Functions: • f • bisection • derivative • roots • all_roots • intervals Back
  • 12. Bisection Method – function “f” def f(symbolic_fuction): return lambdify(x, symbolic_fuction) The function receives a symbolic mathematical expression and returns a mathematical function. Back
  • 13. Bisection Method – function “bisection”pt.1 Code: def bisection(a, b, iteration, function, epsilon): try: i=0 if(isinstance(function(a),complex) == True or isinstance(function(b),complex) == True): return "none" else: if function(a) * function(b) <= 0 : c = (a + b) / 2 while(abs(b - a) > epsilon or iteration == 0 ) : if(function(a) == 0): return a if(function(b) == 0): return b if(function(c) == 0): return c The function receives a range , amount of iterations, a mathematical function and an epsilon representing how close we want our answer to be and returns the root of the received function in the given range , or none in case it doesn’t have a root. BackNext
  • 14. Bisection Method – function “bisection”pt.2 Code: elif(function(a) * function(c) > 0): a = c elif(function(c) * function(b) > 0): b = c iteration = iteration - 1 c = (a + b) / 2 print("number iteration:", i) print("Approximation:", c) i += 1 return c else: return "none" except ValueError: return "none" except ZeroDivisionError: return "none" except DomainError: return "none" Back
  • 15. Bisection Method – function “derivative” Code: f_tag = diff(symbolic_fuction) return lambdify(x, f_tag, 'numpy') The function receives a mathematical expression and returns its derivative form. Back
  • 16. Bisection Method – function “roots” Code: count = 0 j = 0 rootList = [] for k in ranging: if( j == len(ranging) - 1): return rootList tmp = bisection(ranging[j], ranging[j + 1], iteration, function, epsilon) if (not(tmp == "none")): if(len(rootList) > 0): if(tmp + 2 * epsilon >= rootList[count - 1] or tmp - 2 * epsilon<= rootList[count - 1] ): count = count + 1 rootList.append(tmp) else: count = count + 1 rootList.append(tmp) j = j + 1 return rootList The function receives a range , amount of iterations, a mathematical function and an epsilon representing how close we want our answer to be and returns all the roots of the function in the given range (without the roots of its derivative) Back
  • 17. Bisection Method – function “all_roots” Code: def all_roots(ranging, fx, fx_tag, iteration, epsilon): fx_list = [] fx_tag_list = [] root_list = [] merged_list = [] fx_list = roots(ranging, fx, iteration, epsilon) fx_tag_list = roots(ranging, fx_tag, iteration, epsilon) almostE = 0.00001 for i in fx_tag_list: if(fx(i) <= almostE and fx(i) >= -almostE): root_list.append(i) for i in root_list: for j in fx_list: if (j <= i + almostE and j >= i - almostE): continue else: merged_list.append(j) for i in merged_list: fx_list += i return fx_list The function receives a range , amount of iterations, a mathematical function and an epsilon representing how close we want our answer to be and returns the roots of the function’s derivative form with the function roots. Back
  • 18. Bisection Method – function “intervals” Code: def intervals(Start_Domain, End_Domain, interval_jump): domains = [] parameter = Start_Domain tmp = (End_Domain - Start_Domain) / interval_jump for i in range(int(tmp)): domains.append(parameter) parameter += interval_jump domains.append(End_Domain) return domains The function receives a range and returns a list of intervals Back
  • 19. Newton - Raphson Method Background info: https://en.wikipedia.org/wiki/Newton%27s_method Full Code: Newton_Raphson.txt Used libraries: NumPy, SymPy, cmath. User Guide: Click me. Back
  • 20. Newton-Raphson Method– User Guide Functions: • func • derivative • find_intervals • NR • checkVariable • checkRoot • Print_roots • all_roots Back
  • 21. Newton-Raphson Method – function “func” Code: def func(symbolic_fuction) return lambdify(x, symbolic_fuction, 'numpy') The function receives a symbolic mathematical expression and returns its derivative form. Back
  • 22. Newton-Raphson Method – function “derivative” Code: def derivative(symbolic_fuction) g_tagx = diff(symbolic_fuction) return lambdify(x, g_tagx, 'numpy') The function receives a symbolic mathematical expression and returns its derivative form. Back
  • 23. Newton-Raphson Method – function “find intervals” Code: def find_intervals(rangex) intervals = [] j = rangex[0] while j = rangex[1] intervals.append([j, j + 0.05]) j = j + 0.05 return intervals The function a range and returns a list of intervals Back
  • 24. Newton-Raphson Method – function “make_intervals” Code: intervals = [] j = rangex[0] for _ in range((abs(rangex[1])+abs(rangex[0])) * 2): intervals.append([j, j + 0.5]) j = j + 0.5 return intervals Back The function separates the range into smaller intervals
  • 25. Newton-Raphson Method – function “NR” Code: def NR(f, f_tag, x,eps, itration,printList) i=0 while abs(f(x)) eps or itration == 0 if f_tag(x) == 0 return None x = x - f(x) f_tag(x) printList.append((x, i)) i += 1 itration -= 1 return x The function receives the function and its derivative form,a range and an epsilon and returns a root in the given range. Back
  • 26. Newton-Raphson Method – function “checkVariable” Code: def checkVariable(root, roots, eps) for i in roots if abs(i - root) eps return False return True The function receives roots and an epsilon and checks if the solutions we found are close enough. Back
  • 27. Newton-Raphson Method – function “checkRoot” Code: def checkRoot(f,root, eps): return abs(f(root)) < eps The function receives a function and roots and epsilon and returns whether the solution is close enough. Back
  • 28. Newton-Raphson Method – function “Print_roots” Code: def Print_roots(printList) for i in printList print(number iteration, i[1]) print(Approximation, i[0]) The function receives a a list of roots and prints them. Back
  • 29. Newton-Raphson Method – function “all_roots” pt.1 Code: def all_roots(f, f_tag, eps, rangex, itration) roots = [] intervals = find_roots(rangex) for i in intervals printList=[] x = (i[0] + i[1]) 2 root = NR(f, f_tag, x, eps, itration, printList) if root != None if (len(roots)==0)and root = rangex[1] and root = rangex[0] and checkRoot(f, root, eps) roots.append(root) BackNext The function calculates all the roots (function and derivative) in the given range or none in case they don’t exist.
  • 30. Newton-Raphson Method – function “all_roots” pt.2 Continution code: Print_roots(printList) else if root == None continue if (not checkVariable(root, roots, 0.1)) continue if f(root) == None continue elif root = rangex[1] and root = rangex[0] and checkRoot(f, root, eps) roots.append(root) Print_roots(printList) if len(roots) == 0 return None roots.sort() return roots Back
  • 31. Secant Method Background info: https://en.wikipedia.org/wiki/Secant_method Full Code: Secant.txt Used libraries: NumPy, SymPy, cmath. User Guide: Click me. Back
  • 32. Secant Method– User Guide Functions: • func • find_intervals • secant • checkVariable • checkRoot • Print_roots • all_roots Back
  • 33. Secant Method – function “func” Code: def func(symbolic_fuction) return lambdify(x, symbolic_fuction, 'numpy') The function receives a symbolic mathematical expression and returns its derivative form. Back
  • 34. Secant– function “find intervals” Code: def find_intervals(rangex) intervals = [] j = rangex[0] while j = rangex[1] intervals.append([j, j + 0.05]) j = j + 0.05 return intervals The function a range and returns a list of intervals Back
  • 35. Secant Method – function “secant” Code: def secant(rangex, fx, x0, x1, eps, itration,printList): i=0 while abs(x1 - x0) > eps or itration == 0: if abs(fx(x1) - fx(x0)) < eps: return None x = x1 - fx(x1) * ((x1 - x0)/(fx(x1) - fx(x0))) printList.append((x,i)) i += 1 x0 = x1 x1 = x itration -= 1 return x The function receives the function and its derivative form,a range and an epsilon and returns a root in the given range. Back
  • 36. Secant Method – function “checkVariable” Code: def checkVariable(root, roots, eps) for i in roots if abs(i - root) eps return False return True The function receives roots and an epsilon and checks if the solutions we found are close enough. Back
  • 37. Secant Method – function “checkRoot” Code: def checkRoot(f,root, eps): return abs(f(root)) < eps The function receives a function and roots and epsilon and returns whether the solution is close enough. Back
  • 38. Secant Method – function “Print_roots” Code: def Print_roots(printList) for i in printList print(number iteration, i[1]) print(Approximation, i[0]) The function receives a a list of roots and prints them. Back
  • 39. Secant Method – function “all_roots” pt.1 Code: def all_roots(f, f_tag, eps, rangex, itration) roots = [] intervals = find_roots(rangex) for i in intervals printList=[] x = (i[0] + i[1]) 2 root = NR(f, f_tag, x, eps, itration, printList) if root != None if (len(roots)==0)and root = rangex[1] and root = rangex[0] and checkRoot(f, root, eps) roots.append(root) BackNext The function calculates all the roots (function and derivative) in the given range or none in case they don’t exist.
  • 40. Secant Method – function “all_roots” pt.2 Continution code: Print_roots(printList) else if root == None continue if (not checkVariable(root, roots, 0.1)) continue if f(root) == None continue elif root = rangex[1] and root = rangex[0] and checkRoot(f, root, eps) roots.append(root) Print_roots(printList) if len(roots) == 0 return None roots.sort() return roots Back
  • 41. SOR Method Background info: https://en.wikipedia.org/wiki/Successive_over-relaxation Full Code: SOR.txt Used libraries: NumPy. User Guide: Click me. Back
  • 42. SOR Method– User Guide Functions: • SOR Back
  • 43. SOR Method – function “is_close” Code: def is_close(line, b, result, eps): sum = 0 for i in range(line.getA().size): sum = sum +(result[i] * line.item(0,i)) if abs(sum - b) < eps: return True return False The function receives results from the SOL method and checks if they are close enough to the original matrix’s results. Back
  • 44. SOR Method – function “SOR” pt.1 Code: def SOR(matrix, b): flag = 0 w = 0.05 while flag == 0 and w <= 2: printList = [] D = create_D(matrix) L = create_L(matrix) U = create_U(matrix) W_L = multi_scalar(L, w) D_WL = sum_m(W_L, D) W_D = multi_scalar(D, 1 - w) W_U = multi_scalar(U, w) new_b = [] for i in range(len(b)): new_b.append(b[i] * w) result = [] BackNext The function solves a given matrix using the SOR method and returns the results.
  • 45. SOR Method – function “SOR” pt.2 for i in range(len(matrix)): m = [] for j in range(0, i): m.append(-D_WL.item(i,j)) m.append(W_D.item(i,i)) for k in range(i + 1, len(matrix)): m.append(-W_U.item(i, k)) result.append(m) mat = np.matrix(result) vector = [] for i in range(len(matrix)): vector.append(D.item(i, i)) BackNext
  • 46. SOR Method – function “SOR” pt.3 Code continuation: vector = [] for i in range(len(matrix)): vector.append(D.item(i, i)) r = iterative(mat, vector, new_b, printList) close = True for i in range(len(matrix)): if is_close(matrix[i], b[i], r, 0.0001) == False: close = False if close == False: w = w + 0.05 else: flag = 1 PrintList(printList, len(b)) return r Back
  • 47. Gauss-Seidel Method Background info: https://en.wikipedia.org/wiki/Gauss%E2%80%93Seidel_method Full Code: Gauss-Seidel.txt Used libraries: NumPy. User Guide: Click me. Back
  • 48. Gauss-Seidel Method– User Guide Functions: • gauss-seidel Back
  • 49. Gauss-Seidel Method– function “gauss-seidel” Code: def gaus(matrix, b): D = create_D(matrix) L = create_L(matrix) U = create_U(matrix) L_D = sum_m(L, D) L_D_INVERSE = (L_D.I) L_D_U = np.matmul(L_D_INVERSE, U) L_D_b = np.matmul(L_D_INVERSE, b) L_D_U_1 = multi_scalar(L_D_U,-1) new_b = [] for i in range(len(b)): new_b.append(1.0) L_D_b = L_D_b.getA()[0] printList = [] r = iterative(L_D_U_1, new_b ,L_D_b, printList) PrintList(printList) return r Back The function solves a given matrix using the Gauss-Seidel method and returns the results.
  • 50. Utility Full Code: Utility.txt Used libraries: NumPy. User Guide: Click me. Back
  • 51. Utility– User Guide Functions: • iterative • StrongcalcDominant • calcDominant • sum_m • multi_m • multi_scalar • normMax • create_D • create_L • create_U • PrintList Back
  • 52. Utility – function “iterative” pt.1 def iterative(matrix, b, c, printList): result = [] sum = 0 count = 0 k = 0 flag = 0 flag2 = 0 for i in range(len(matrix)): result.append(0) while(flag2 == 0 and count <= 50): for i in range(len(matrix)): for j in range(len(matrix)): sum = sum + (matrix.item(i, j)*result[j]) sum = sum + c[i] if b[i] > 0.001: The function receives a matrix and two vectors and returns the matrix’s solution. BackNext
  • 53. Utility – function “iterative” pt.2 sum = sum / b[i] if abs(sum - result[i]) > 0.0000001: flag = 1 result[i] = sum sum = 0 if flag == 0: flag2 = 1 else: flag = 0 count = count + 1 printList.append(count) for j in result: printList.append(j) return result Back
  • 54. Utility – function “StrongcalcDominant” def StrongcalcDominant(mat): mat = np.matrix(mat) newOrder = list(range(len(mat))) found = False for n in range(len(mat)): for m in range(n, len(mat)): if (abs(mat[n, m]) > abs(np.sum(mat[n, :]) - mat[n, m])): newOrder.pop(m) newOrder.insert(n, m) found = True if not found: return None mat = mat[:, newOrder] newOrder = list(range(len(mat))) found=False return mat The function receives a matrix, checks if it is diagonally dominant and if not , tries to force it into strong diagonally dominant. Returns the new order of the matrix if it can be changed or “None” in case it couldn’t. Back
  • 55. Utility – function “calcDominant” def calcDominant(mat,b): mat = np.matrix(mat) newOrder = list(range(len(mat))) found = False for m in range(len(mat)): for n in range(m, len(mat)): if (abs(mat[n, m]) > abs(np.sum(mat[n:, m]) - mat[n, m])): newOrder.pop(n) newOrder.insert(m, n) found = True break if not found: return None mat = mat[newOrder,:] b=b[newOrder] newOrder = list(range(len(mat))) found=False return mat,b The function receives a matrix,and vector b checks if it is diagonally dominant and if not , tries to force it into diagonally dominant. Returns the new order of the matrix if it can be changed or “None” in case it couldn’t. Back
  • 56. Utility – function “sum_m” Code: def sum_m(mat1, mat2): result = [] for i in range(len(mat1)): mylist = [] for j in range(len(mat2)): mylist.append(0.0) result.append(mylist) mat = np.matrix(result) for i in range(len(mat1)): for j in range(len(mat2)): mat.itemset((i, j), (mat1.item(i, j) + mat2.item(i, j))) new_mat = np.matrix(mat) return new_mat The function receives two matrices and returns the calculated sum of the two. Back
  • 57. Utility – function “multi_m” pt.1 Code: def multi_m(mat1, mat2): result = [] for i in range(len(mat1)): mylist = [] for j in range(len(mat1)): mylist.append(0.0) result.append(mylist) mat = np.matrix(result) BackNext The function receives two matrices and returns the calculated multiply of the two.
  • 58. Utility - function “multi_m” pt.2 Code continuation: for i in range(len(mat1)): for j in range(len(mat1)): for k in range(len(mat1)): mat.itemset((i, j), (mat1.item(i, k) + mat2.item(k, j))) new_mat = np.matrix(mat) return new_mat Back
  • 59. Utility – function “multi_scalar” Code: def multi_scalar(mat, num): result = [] for i in range(len(mat)): mylist = [] for j in range(len(mat)): mylist.append(mat.item(i, j) * num) result.append(mylist) new_mat = np.matrix(result) return new_mat Back The function receives a matrix and a scalar and returns the multiply of the scalar with the matrix.
  • 60. Utility – function “normMax” Code: def normMax(mat): sum = 0 list = [] for i in range(len(mat)): for j in range(len((mat.getA()[0]))): sum = sum + abs(mat.item(i,j)) list.append(sum) sum = 0 return(max(list)) Back The function receives a matrix and returns the matrix’s norm .
  • 61. Utility – function “create_D” Code: def create_D(matrix): result = [] for i in range(len(matrix)): m = [] for j in range(len(matrix)): if i == j: m.append(float(matrix.item(i, j))) else: m.append(0.0) result.append(m) D = np.matrix(result) return D Back The function receives a matrix and creates a diagonally matrix from it.
  • 62. Utility – function “create_L” Code: def create_L(matrix): result = [] for i in range(len(matrix)): m = [] for j in range(len(matrix)): if i > j: m.append(float(matrix.item(i, j))) else: m.append(0.0) result.append(m) L = np.matrix(result) return L Back The function receives a matrix and creates a lower triangular matrix from it.
  • 63. Utility – function “create_U” Code: def create_U(matrix): result = [] for i in range(len(matrix)): m = [] for j in range(len(matrix)): if i < j: m.append(float(matrix.item(i, j))) else: m.append(0.0) result.append(m) U = np.matrix(result) return U Back The function receives a matrix and creates a lower triangular matrix from it.
  • 64. Utility – function “PrintList” Code: def PrintList(printList, num): for i in range(0 ,len(printList), num + 1): print("number iteration:", printList[i]) for j in range(1 , num + 1): print("x", j, ": ", printList[i + j]) Back The function receives a list that includes a matrix’s solutions and prints it.