Exempel 1: urbanisering
- X=invånare i städer
- Y=invånare på landsbygd
- varje år flyttar andel a, 0 < a < 1, av stadsborna ut på landet
- varje år flyttar andel b, 0 < b < 1, av lantisarna in till stan
- Linjär avbildning T(X,Y) = ((1-a)X +bY,aX + (1-b)Y)
- Tidsindexerad vektor Vn = (Xn,Yn)
- V_(n+1) = T(Vn)
# Startvärden
X0,Y0 = 1/10,9/10
# parametervärden
a,b = 1/100,4/100
# Avbildningsmatris
A = matrix([[1-a,b],[a,1-b]])
# initiera V
VS = vector([X0,Y0])
V = [VS]
# Prognosticera N år framåt
N=100
Messages
for j in range(N):
VS = A*VS
V.append(VS)
Messages
list_plot(V)
Messages
list_plot([u[0] for u in V]) + list_plot([u[1] for u in V],color='red')
Messages
(A -identity_matrix(2)).right_kernel()
Vector space of degree 2 and dimension 1 over Rational Field Basis matrix: [ 1 1/4]
Messages
(A -identity_matrix(2)).right_kernel().matrix()
[ 1 1/4]
Messages
(A -identity_matrix(2)).right_kernel().matrix()*4/5
[4/5 1/5]
Messages
A*vector([4/5,1/5])
(4/5, 1/5)
Messages
# Modifiering: Stad, landsbygd, invandring, utvandring
# X stad, Y landsbygd
# Har även ett utland Z
# varje år invandrar en andel c, till städer
# och en andel d utvandrar, från landsbygd, andel e, från städer
# Xn = (1-a-e)X + bY + cZ
# Yn = aX + (1-b-c)Y
# Zn = eX + dY +(1-c)Z
# Startvärden
X0,Y0,Z0 = 1/100,9/100,9/10
# parametervärden
a,b,c,d,e = 1/100,4/100,1/300,1/150,1/200
# Avbildningsmatris
A = matrix([[1-a-e,b,c],[a,1-b-c,0],[e,d,(1-c)]])
# initiera V
VS = vector([X0,Y0,Z0])
V = [VS]
# Prognosticera N år framåt
N=500
Messages
for j in range(N):
VS = A*VS
V.append(VS)
Messages
list_plot(V)
Messages
list_plot([u[0] for u in V]) + list_plot([u[1] for u in V],color='red') + list_plot([u[2] for u in V],color='green')
Messages
Exempel 2: Råttor i labyrint
# Xj antal råttor i rum nr j
# Varje timme förflyttar sig andel c_ij från rum i till rum j
# Linjär avbildning med matris C = (c_ij)
#
Messages
G = graphs.DodecahedralGraph()
G.plot()
# notera att alla hörn har grad 3
Messages
A = G.adjacency_matrix()
n=20
A
20 x 20 dense matrix over Integer Ring (use the '.str()' method to see the entries)
Messages
show(A)
\(\displaystyle \left(\begin{array}{rrrrrrrrrrrrrrrrrrrr}
0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \\
1 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 1 & 0 & 1 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 1 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \\
0 & 0 & 0 & 1 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\
0 & 0 & 0 & 0 & 1 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \\
0 & 0 & 1 & 0 & 0 & 1 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\
0 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 1 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\
1 & 0 & 0 & 0 & 0 & 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 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & \frac{1}{3} & 0 & \frac{1}{3} & 0 & 0 & \frac{1}{3} & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & \frac{1}{3} & 0 & \frac{1}{3} & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \frac{1}{3} \\
0 & 0 & 0 & \frac{1}{3} & 0 & \frac{1}{3} & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \frac{1}{3} & 0 & 0 \\
0 & 0 & 0 & 0 & \frac{1}{3} & 0 & \frac{1}{3} & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \frac{1}{3} & 0 & 0 & 0 & 0 \\
0 & 0 & \frac{1}{3} & 0 & 0 & \frac{1}{3} & 0 & \frac{1}{3} & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & \frac{1}{3} & 0 & \frac{1}{3} & 0 & 0 & 0 & 0 & 0 & \frac{1}{3} & 0 & 0 & 0 & 0 & 0 \\
0 & \frac{1}{3} & 0 & 0 & 0 & 0 & 0 & \frac{1}{3} & 0 & \frac{1}{3} & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \frac{1}{3} & 0 & \frac{1}{3} & 0 & 0 & \frac{1}{3} & 0 & 0 & 0 & 0 & 0 & 0 \\
\frac{1}{3} & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \frac{1}{3} & 0 & \frac{1}{3} & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \frac{1}{3} & 0 & \frac{1}{3} & 0 & 0 & 0 & 0 & 0 & \frac{1}{3} & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \frac{1}{3} & 0 & \frac{1}{3} & 0 & 0 & \frac{1}{3} & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \frac{1}{3} & 0 & 0 & \frac{1}{3} & 0 & \frac{1}{3} & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & \frac{1}{3} & 0 & 0 & 0 & 0 & 0 & \frac{1}{3} & 0 & \frac{1}{3} & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & \frac{1}{3} & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \frac{1}{3} & 0 & \frac{1}{3} & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \frac{1}{3} & 0 & 0 & \frac{1}{3} & 0 & \frac{1}{3} & 0 & 0 \\
0 & 0 & 0 & 0 & \frac{1}{3} & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \frac{1}{3} & 0 & \frac{1}{3} & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \frac{1}{3} & 0 & 0 & 0 & 0 & 0 & \frac{1}{3} & 0 & \frac{1}{3} \\
\frac{1}{3} & 0 & 0 & \frac{1}{3} & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \frac{1}{3} & 0
\end{array}\right)\)
Messages
# Startvärden
X0 = vector([0 for j in range(n)])
X0[0]=200
X0[1]=200
X0[2]=200
X0[3]=200
X0[4]=200
# initiera V
VS =X0
V = [VS]
# Prognosticera N år framåt
N=100
Messages
for j in range(N):
VS = C*VS
V.append(VS)
Messages
P = Graphics()
for k in range(20):
P += list_plot([u[k] for u in V], color=hue(k/n))
Messages
P
Messages
n=10
V=[j for j in range(n)]
E = [{1,2},{2,3},{3,4},{3,7},{4,8},{5,9},{2,7},{0,4},{3,9},{4,9},{8,3},{2,4},{1,6}]
G = Graph([V,E])
n=10
G.plot()
Messages
A = G.adjacency_matrix()
A
n=A.dimensions()[0]
A,n
( [0 0 0 0 1 0 0 0 0 0] [0 0 1 0 0 0 1 0 0 0] [0 1 0 1 1 0 0 1 0 0] [0 0 1 0 1 0 0 1 1 1] [1 0 1 1 0 0 0 0 1 1] [0 0 0 0 0 0 0 0 0 1] [0 1 0 0 0 0 0 0 0 0] [0 0 1 1 0 0 0 0 0 0] [0 0 0 1 1 0 0 0 0 0] [0 0 0 1 1 1 0 0 0 0], 10 )
Messages
add(A.row(0))
1
Messages
A[1,2]
1
Messages
C = matrix(QQ,n,n,lambda i,j: A[i,j]/(add(A.column(j))))
C
[ 0 0 0 0 1/5 0 0 0 0 0] [ 0 0 1/4 0 0 0 1 0 0 0] [ 0 1/2 0 1/5 1/5 0 0 1/2 0 0] [ 0 0 1/4 0 1/5 0 0 1/2 1/2 1/3] [ 1 0 1/4 1/5 0 0 0 0 1/2 1/3] [ 0 0 0 0 0 0 0 0 0 1/3] [ 0 1/2 0 0 0 0 0 0 0 0] [ 0 0 1/4 1/5 0 0 0 0 0 0] [ 0 0 0 1/5 1/5 0 0 0 0 0] [ 0 0 0 1/5 1/5 1 0 0 0 0]
Messages
# Startvärden
X0 = vector([1000 for j in range(n)])
# initiera V
VS =X0
V = [VS]
# Prognosticera N år framåt
N=20
Messages
for j in range(N):
VS = C*VS
V.append(VS)
Messages
P = Graphics()
for k in range(n):
P += list_plot([u[k] for u in V], color=hue(k/n))
Messages
P
Messages
C.right_eigenvectors()
[(1, [ (1, 1/2, 2, 5/2, 5/2, 1/2, 1/2, 1, 1, 3/2) ], 1), (-0.8398235082107391?, [(1, -0.2060833806506?, 0.6922946708875?, -0.3437644039590?, -1.22230160823193?, -0.2806794823016?, -0.5953631865643533?, -0.1242175122632?, 0.37295122055524?, 0.7071636825278?)], 1), (-0.6893981609955728?, [(1, 0.269644896337?, -0.743570782626?, 1.3154176342885?, 0.1793606149182?, 0.702087831979?, -0.725270283979204?, -0.111968432132?, -0.4336472981152?, -1.452054180670?)], 1), (-0.6199401674756040?, [(1, 2.854465097039?, -7.078390281247?, -2.070301304438?, 0.93294639401529?, -1.486957759128?, -0.806529446278662?, 3.522368682919?, 0.3669240904502?, 2.765474526668?)], 1), (-0.3830446005915859?, [(1, -3.7838617141439?, 5.7975511959522?, -41.4760585090107?, 4.61143210754075?, 13.1699242103245?, -1.305331022099737?, 17.8721326243503?, 19.2482161839823?, -15.1340050768956?)], 1), (-0.2188362404008838?, [(1, -2.4363381262593?, 2.132636303584?, -4.365737859192?, 10.32988638122712?, -1.392952225987?, -2.284813516646308?, 1.5536206220672?, -5.4507868633729?, 0.914485284579?)], 1), (0.09651438158260960?, [(1, -13.314511685743?, -5.14016744570?, 19.868481354575?, -25.4203034875033?, 1.142285688750?, 5.180575079083263?, 27.857552060154?, -11.504652554140?, 0.33074099053?)], 1), (0.3313555084022865?, [(1, 4.229473707518478?, 5.60583764251556?, -6.594557783882132?, -5.887989140921879?, 3.722740820258636?, 1.508953336586663?, 0.249121718997483?, -7.534232332513037?, 3.700652031440236?)], 1), (0.5933328417055978?, [(1, 2.915936185702642?, 6.920482813180519?, -0.0498998370638647?, -1.246822428775766?, -4.620294648099053?, 0.8426973274607509?, 2.899116001968863?, -0.4370977551527624?, -8.22411765922133?)], 1), (0.7298399459838917?, [(1, -0.1897236883660372?, -0.553871705875734?, -0.4878410218391427?, 0.2237911677315279?, -0.08831112972764365?, 0.685081712437587?, -0.3234080734215819?, -0.0723582904883759?, -0.1933589704505997?)], 1)]
Messages
Itererade funktionssystem
- En affin avbildning är på formen X -> AX + B
- Vi skapar 4 affina avbildningar F1,F2,F3,F4
- Vi tar en startpunkt P0
- Vi sätter P_(n+1) = F_*(P_n), slumpvis vald F
- Vi ritar trajektoriet
- I bästa fall närmar sig det en "attraktor"
# Fyra affina avbildningar
A =[matrix([[0,0],[0,0.16]]),
matrix([[0.85,0.04],[-0.04,0.85]]),
matrix([[0.2,-0.26],[0.23,0.22]]),
matrix([[-0.15,0.28],[0.26,0.24]])]
B = [vector([0,0]),vector([0,1.6]),vector([0,1.6]),vector([0,0.44])]
Messages
var('x,y')
X = vector([x,y])
for j in range(4):
show(A[k]*X + B[k])
\(\displaystyle \left(0.200000000000000 \, x - 0.260000000000000 \, y,\,0.230000000000000 \, x + 0.220000000000000 \, y + 1.60000000000000\right)\)
\(\displaystyle \left(0.200000000000000 \, x - 0.260000000000000 \, y,\,0.230000000000000 \, x + 0.220000000000000 \, y + 1.60000000000000\right)\)
\(\displaystyle \left(0.200000000000000 \, x - 0.260000000000000 \, y,\,0.230000000000000 \, x + 0.220000000000000 \, y + 1.60000000000000\right)\)
\(\displaystyle \left(0.200000000000000 \, x - 0.260000000000000 \, y,\,0.230000000000000 \, x + 0.220000000000000 \, y + 1.60000000000000\right)\)
Messages
# Startpunkt
P = vector([2,5])
Messages
# Iterate N times
N = 999
PL = [P]
for j in range(N):
k=randint(0,3)
P = A[k]*P + B[k]
PL.append(P)
Messages
list_plot(PL)
Messages
Rekursivt definierade följder
- Anta vi har rekursivt samband s_(n+2) = as_(n+1) + bs_n (med initalvärden s0,s1)
- Inför t_n = s_(n+1)
- Får (t_(n+1),s_(n+1)) = (s_(n+2),t_n) = (as_(n+1) + bs_n,t_n) =(at_n + bs_n,t_n)
# Fibonacci: s0=s1=1, s_(n+2) = s_(n+1) + s_n
X0 = vector([1,1])
a=b=1
A = matrix([[a,b],[1,0]])
Messages
# Räkna ut första N värdena
N= 25
VS = X0
V = [VS]
for j in range(N):
VS = A*VS
V.append(VS)
Messages
V[:10]
[(1, 1), (2, 1), (3, 2), (5, 3), (8, 5), (13, 8), (21, 13), (34, 21), (55, 34), (89, 55)]
Messages
list_plot([u[0] for u in V])
Messages
list_plot([log(u[0]) for u in V])
Messages
Messages