Numerical Methods For Solutions of Equations in Python
Numerical Methods For Solutions of Equations in Python
Contents
Introduction .................................................................................................................................................. 1
Norms: Vectors and matrices.................................................................................................................... 1
Code in Python ...................................................................................................................................... 2
Symbolic calculus ...................................................................................................................................... 3
Numerical methods for non-linear equations in R...................................................................................... 4
Bisection method ...................................................................................................................................... 4
Methodology......................................................................................................................................... 4
Python code .......................................................................................................................................... 4
Contraction method.................................................................................................................................. 5
Methodology......................................................................................................................................... 5
Python code .......................................................................................................................................... 6
Other Methods: Newton, secant, chord ................................................................................................... 6
Methodology......................................................................................................................................... 6
Python code .......................................................................................................................................... 7
Numerical methods for non-linear equations in 𝑹𝒏 .................................................................................. 8
Contraction principle ................................................................................................................................ 8
Introduction
Norms: Vectors and matrices
Definition:
1
For 𝑥 = (𝑥1 , 𝑥2 , … , 𝑥𝑑 ) ∈ 𝑅 𝑑 , 𝑝 ∈ 𝑅+ , 𝑝 < ∞, ||𝑥||𝑝 = (|𝑥1 |𝑝 + |𝑥2 |𝑝 + ⋯ + |𝑥𝑑 |𝑝 )𝑝 .
Definition 2:
2
Frobenius norm: ||𝐴||𝐹 = √∑𝑚 𝑛
𝑖=1 ∑𝑗=1|𝑎𝑖𝑗 |
Properties:
||𝑨||𝟐 = √𝝆(𝑨𝒕 𝑨) where 𝝆(𝑿) = spectral radius of 𝑿 ∈ 𝑴𝒎 (𝑪): = 𝒎𝒂𝒙{|𝝀𝟏 |, … , |𝝀𝒎 |}
Code in Python
import numpy as np
def test_norms():
def test1():
"I will test norm1 of vector: the sum of absolute values of elems"
v = np.array([1,2,3,4])
v2 = np.array([1,-2,3,-4])
print("You are inside test1")
print("Norm-1 of v is:",np.linalg.norm(v,1))
print("Norm-1 of v2 is:",np.linalg.norm(v2,1))
test1()
def test2():
v = np.array([1,2,3,4])
print("Inside test 2")
print("Usual norm of v is",np.linalg.norm(v))
print("2-norm of V is",np.linalg.norm(v,2))
print("Infinity norm of V is",np.linalg.norm(v,np.inf))
print("As an alternative, the same norm of V is",np.max(np.abs(v)))
"by default the norm computed is the norm-2"
def test3():
v = np.array([[1,2,3,4],[-1,2,3,3]])
print("Inside test 3:")
print("Norm 2 of the matrix v is:",np.linalg.norm(v))
def test4():
v = np.array([[1,2,3,4],[-1,2,3,3]])
print("The inf norm of matrix v is:",np.linalg.norm(v,np.inf))
res1,res2 = np.sum(np.abs(v[0])),np.sum(np.abs(v[1]))
print("Alternatively, norm inf of matrix v is",np.max([res1,res2]))
test2()
test3()
test4()
Exercise1 :
a. Implement your own functions for norms (p and ∞).(At least for vectors).
Solution :
a.
import numpy as np
def norms(num):
def norm1(v):
"v is a vector"
return np.sum(np.abs(v))
def norm2(v):
"v is also vector"
return np.sqrt(np.sum(v**2))
def test():
v = np.array([3,4,5])
print(norm2(v))
def norm(v,p):
return (np.sum(v**p))**(1/p)
def test2():
v = np.array([3,4,5])
print(norm(v,2))
dic = {1:norm1,2:norm2,3:norm}
return dic[num]
You can add test1() and test2() calls inside norms library to witness the functionality of the defined
functions.
b.
def exercise():
def spectral_radius(A):
eig = np.linalg.eig(A)[0]
return np.max(np.abs(eig))
def quantity(A):
"the RHS of the equality"
X = np.dot(np.transpose(A),A)
return np.sqrt(spectral_radius(X))
def test():
v = np.array([[1,2,3,4],[-1,2,3,3]])
print("Norm 2 of the matrix v is:",np.linalg.norm(v,2))
print("The RHS quantity is:",quantity(v))
test()
Symbolic calculus
Sympy is a library defined to handle symbolic calculus.
This library has various features for limits, differentiation, integration, etc.
Then the sequence (𝑐𝑛 )𝑛≥0 built above converges to the unique solution 𝑧 ∈ [𝑎, 𝑏] of equation 𝑓(𝑥) =
𝑏−𝑎
0 and |𝑐𝑛 − 𝑧| ≤ 2𝑛
.
Python code
import numpy as np
def bisection_method():
def condition(f,a,b):
"We test if f(a)f(b)<0"
if f(a)*f(b)>=0:
return 0
else:
return 1
def bisect(f,a,b,tol):
if condition(f,a,b)==0:
raise Exception("Choose another pair of endpoints")
else:
x,y,z = a,b,(a+b)/2
err,n = 1,1
while err>=tol and n<30:
if f(z) == 0:
return z,err,n
elif f(x)*f(z)<0:
x=x
y=z
z = (x+y)/2
err = np.abs(z-x)
n = n+1
elif f(x)*f(z)>0:
x=z
z = (x+y)/2
err = np.abs(z-x)
n = n+1
return z,err,n
def test1():
f = lambda x: np.exp(x)-x-2
a,b = 1,2
res = bisect(f,a,b,0.00001)[0]
print(f(res))
print(bisect(f,a,b,0.00001))
test1()
Contraction method
Methodology
Let 𝐼 ⊂ 𝑅 an interval and 𝑓: 𝐼 → 𝑅 a function. The function 𝑓 is called contraction if and only if :
𝑏) 𝑓(𝐼) ⊂ 𝐼.
a) If ∃𝑞 ∈ (0,1) such that |𝑓 ′ (𝑥)| ≤ 𝑞 for which 𝑥 ∈ [𝑎, 𝑏] ⇒ |𝑓(𝑥) − 𝑓(𝑦)| ≤ 𝑞|𝑥 − 𝑦|, ∀𝑥, 𝑦 ∈ [𝑎, 𝑏].
𝑎+𝑏 𝑎+𝑏 𝑏−𝑎
b) If |𝑓(𝑥) − 𝑓(𝑦)| ≤ 𝑞|𝑥 − 𝑦|and |𝑓 ( 2
)− 2 | ≤ (1 − 𝑞) ⋅ 2
then 𝑓([𝑎, 𝑏]) ⊂ [𝑎, 𝑏].
Main statement :
Let 𝒇: 𝑰 → 𝑹 be a contraction and 𝑥0 ∈ 𝐼. We define the sequence (𝑥𝑛 )𝑛 through the relation 𝑥𝑛+1 =
𝑓(𝑥𝑛 ), ∀𝑛 ∈ 𝑁. Then the equation 𝑓(𝑥) = 𝑥 has a unique solution 𝑧 ∈ [𝑎, 𝑏] and the sequence 𝑥𝑛 → 𝑧,
with the following error evaluation formula :
𝑞 𝑞𝑛
|𝑥𝑛 − 𝑧| ≤ |𝑥𝑛 − 𝑥𝑛−1 | ≤ |𝑥 − 𝑥0 |, ∀𝑛 ∈ 𝑁.
1−𝑞 1−𝑞 1
Python code
import numpy as np
def contraction(f,x0,tol):
"this is for functions f:R->R"
err,n = 1,0
x = x0
while err>=tol and n<30:
y = f(x)
err = np.abs(x-y)
n = n+1
x=y
return x,err,n
def test():
f = lambda x:x**2+x-7/16
x0 = -5/8
tol = 0.00001
print(contraction(f,x0,tol))
res = contraction(f,x0,tol)[0]
print(f(res))
#res will be a fixed point for f, f(res)=res
Conditions: Let [𝑎, 𝑏] ⊂ 𝑅, 𝑓: [𝑎, 𝑏] → 𝑅. Suppose the next conditions are met:
Remarks:
1. Secant method requires less information about the function, 𝑓 must be only one time differentiable
(compared to two times in Newton or chord), but the number of operations involved is bigger,
because at step 𝑛 + 1 it depends on the solutions found at steps 𝑛& 𝑛 − 1.
2. Newton method requires the biggest number of conditions to be fulfilled but is the fastest method.
Python code
import numpy as np
def num_method(num):
def chord(f,a,b,no_iter):
if f(a)*f(b)<0:
x,y=a,b
for i in range(no_iter):
x,y = y,(a*f(x)-x*f(a))/(f(x)-f(a))
return y
else:
return "Choose another pair of endpoints"
def secant(f,x0,x1,no_iter):
if f(x0)*f(x1)<0:
x,y = x0,x1
for i in range(no_iter):
x,y = y,(x*f(y)-y*f(x))/(f(y)-f(x))
return y
else:
return "Choose another pair of values for x0 and x1"
def Newton(f,fprime,x0,no_iter):
x = x0
for i in range(no_iter):
x = x-f(x)/fprime(x)
return x
dic = {1:chord,2:secant,3:Newton}
return dic[num]
def tests():
def test_chord():
g=num_method(1)
h = lambda x:np.exp(x)-x-2
print(g(h,0,2,20))
def test_secant():
h = lambda x:np.exp(x)-x-2
g = num_method(2)
print(g(h,0,4,50))
def test_Newton():
h1 = lambda x:np.exp(x)-x-2
h1_prime = lambda x:np.exp(x)-1
g = num_method(3)
#print(g(h1,h1_prime,0,10))
print(g(h1,h1_prime,1,10))
def test_Newton2():
import scipy.optimize as scp
h1 = lambda x:np.exp(x)-x-2
h1_prime = lambda x:np.exp(x)-1
print(scp.newton(h1,1,h1_prime)) #uses Newton method
print(scp.newton(h1,1))
test_Newton()
test_Newton2()
𝑏) 𝑓(𝑀) ⊂ 𝑀.
Statement:
Exercise:
80+𝑥 3 −𝑥 2 −3𝑦𝑧
𝑥= 100
60−𝑦 3 −𝑧 2 +4𝑦𝑧
Solve the following system: 𝑦 = on the set 𝑉 = [0,2]3 , starting from 𝑥 (0) = (1,1,1).
86
40+𝑧 3 +𝑦 2 −5𝑥𝑦
{𝑧 = 72
Python code:
def function():
F = lambda x: np.array([(80+x[0]**3-x[0]**2-3*x[1]*x[2])/100,\
(60-x[1]**3-x[2]**2+4*x[0]*x[2])/86,\
(40+x[2]**3+x[1]**2-5*x[0]*x[1])/72])
return F
def fixed_point_function(x0,tol):
'I will compute the fixed point on [0,2]x[0,2]x[0,2] starting from the '
'point x0'
x,err,n = x0,1,1
F = function()
while err>=tol and n<30:
z = F(x)
err = np.linalg.norm(z-x,np.inf)
n = n+1
x=z
return x,err,n
def test_fixed_point_function():
x0 = np.array([1,1,1])
tol = np.exp(-5)
print(fixed_point_function(x0,tol))
Exercise 2:
For the previous system show at each iteration up to the 10th the solution 𝑥 (𝑛) .
Solution:
def fixed_point_function2(x0):
x,n=x0,1
F=function()
while n<=10:
z=F(x)
err = np.linalg.norm(z-x,np.inf)
n = n+1
x = z
print(x,err)
def test_fixed_point_function2():
x0=np.array([1,1,1])
fixed_point_function2(x0)