Numerical Methods HW: Armen Hayrapetian
Numerical Methods HW: Armen Hayrapetian
Armen Hayrapetian
UFAR
2
Contents
3
4 CONTENTS
Chapter 1
6. Let P = 12 0 23 be the transition matrix for a Markov chain with the initial state vector
1 2
0
2 3
1
x0 = 0 .
0
(a) Compute x1 , x2 .
(b) Find the steady state vector.
5
6 CHAPTER 1. EIGENVALUES AND EIGENVECTORS, MARKOV CHAIN
Chapter 2
1. Consider the system of linear equations, solve the system by using the Gaussian method to
write it as RREF(reduced row echelon form).
x1 + 2x2 + 3x3 = 4
2x1 + x2 + x3 = 3 (2.1)
4x1 + 2x2 + 4x3 = 8
2x1 + x2 − 3x3 + 4x4 = 8
3x1 + 2x2 + x3 − 2x4 = 1
(2.2)
4x1 + x2 − 5x3 + x4 = 4
3x1 + 2x2 − 4x4 = −4
2. First write the system to the useful form for iteration and solve it by using Gauss-Jordan
iteration method and the given initial solution.
2x1 + x2 − 3x3 − 4x4 = 8 0
3x1 + 2x2 + x3 = 1 0
, x0 = (2.3)
4x1 + 2x2 + 2x3 − x4 = 2 0
2x1 + x2 + 3x3 + 2x4 = −4 0
3. First write the system to the useful form for iteration and solve it by using Gauss-Seidel
iteration method and the given initial solution.
2x1 + x2 − 4x3 − 2x4 = −10 −0.5
3x1 + 2x2 − x3 + x4 = 3 1.5
, x0 = (2.4)
x 1 + 3x 2 + 2x 3 − x 4 = 4 0.5
3x1 + 3x2 − 2x4 = −3 3.2
7
8 CHAPTER 2. SYSTEM OF LINEAR EQUATIONS
Chapter 3
Bisection Method
Use bisection method to find solutions, accurate to within 10−5 for the following equations.
1. x + 1 − 2 sin(πx) = 0, x ∈ [0.5, 1], x ∈ [0, 0.5]
2. ex − 2(x − 2)2 = 0
import numpy a s np
import math
def f (x ) :
r e t u r n x ∗∗3 + 2∗ x ∗∗2 −5
e l i f np . s i g n ( f ( a ) ) != np . s i g n ( f ( b ) ) :
p r i n t ( ” f has a t l e a s t one r o o t i n [ a , b ] . ” )
m = ( a + b)/2
p r i n t (m)
i f np . abs ( f (m) ) < t o l :
# Stopping condition , r e p o r t m as root
p r i n t (m)
e l i f np . s i g n ( f ( a ) ) == np . s i g n ( f (m) ) :
# Make r e c u r s i v e c a l l with a = m
r e t u r n B i s e c t i o n ( f , m, b , t o l )
e l i f np . s i g n ( f ( b ) ) == np . s i g n ( f (m) ) :
# Make r e c u r s i v e c a l l with b = m
r e t u r n B i s e c t i o n ( f , a , m, t o l )
Bisection ( f ,2 ,3)
9
10 CHAPTER 3. SOLVING EQUATIONS IN ONE VARIABLE
f (xn )
xn+1 = xn −
f ′ (xn )
Use Newton’s method to find solutions, accurate to within 10−5 for the following equations.
1. ex − 3x2 = 0, x ∈ [0, 1], x ∈ [3, 5]
2. ex − 2(x − 2)2 = 0
def f (x ) :
r e t u r n x∗∗3+2∗x−1
d e f Df ( x ) :
return 3∗( x ∗∗2) + 2
d e f Newton ( p ) :
N = p − f ( p ) / Df ( p )
i f f (N) < 0 . 0 0 0 0 1 and f (N) > −0.00001:
return N
else :
p r i n t ( ’ ˜ r o o t = ’ ,N , ’ f ( ˜ r o o t )= ’ , f (N) )
Newton (N)
Newton ( 1 )
11
Secant Method
f (xn )(xn − a)
xn+1 = xn −
f (xn ) − f (a)
Use Secant’s method to find solutions, accurate to within 10−5 for the following equations.
1. ex − 3x2 − 2 = 0
2. ln x − 2x − 8 = 0
import math
def f (x ) :
r e t u r n x ∗∗3+3∗( x ∗ ∗ 2 ) −1
d e f s e c a n t m e t h o d ( f , x0 , x1 , t o l =1e −5, n =0):
n += 1
y0 , y1 = f ( x0 ) , f ( x1 )
# c a l c u l a t e fu nctio n values at endpoints
xn = x1 − y1 ∗ ( ( x1 − x0 ) / ( y1 − y0 ) )
# c a l c u l a t e next r o o t a p p r o x i m a t i o n
p r i n t ( f ’ n={n} xn={xn } ’ )
r e t u r n xn , n
# check t o l e r a n c e c o n d i t i o n
r e t u r n s e c a n t m e t h o d ( f , x1 , xn , n=n )
# r e c u r s i v e c a l l with updated i n t e r v a l
s e c a n t m e t h o d ( f , −3, −2)
# C a l l f u n c t i o n and run .
12 CHAPTER 3. SOLVING EQUATIONS IN ONE VARIABLE
• If f ′ · f ′′ > 0, the {bn } is given by Newton’s method and {an } is given by secant method.
f (b)
b1 = b −
f ′ (b)
f (a)
a1 = a − (b − a)
f (b) − f (a)
So
f (bn )
bn+1 = bn −
f ′ (bn )
f (an )
an+1 = an − (bn − an )
f (bn ) − f (an )
• If f ′ · f ′′ < 0, the {an } is given by Newton’s method and {bn } is given by secant method.
• Let
an + bn
pn =
2
then the sequence {pn } tends to the root.
2. ln x − 2x − 8 = 0