程序代写案例-MACM 203
时间:2022-02-18
MACM 203 Spring 2022 Lecture 6: Dynamical Systems
Table of Contents
EXAMPLE 1..............................................................................................................................................................1
A simple example to illustrate the convergence toward a steady state.................................................................... 1
Dynamics of the system............................................................................................................................................1
Comments:............................................................................................................................................................... 4
Eigenvalues/vectors..................................................................................................................................................4
EXAMPLE 2..............................................................................................................................................................5
EXAMPLE 3..............................................................................................................................................................6
Looking at a non-regular matrix................................................................................................................................6
Eigenvalues.............................................................................................................................................................. 6
Iterating the system.................................................................................................................................................. 7
Comments................................................................................................................................................................ 9
EXAMPLE 1
clear; close all;
A simple example to illustrate the convergence toward a steady state
We consider a system with states composed of two variables: - the number of healthy people - the number of
sick people. Defining the dynamical system matrix - 90% of healthy people stay healthy - 10% of healthy people
become sick - 30% of sick people recover and become healthy - 70% of sick people stay sick
M1 = [ 0.9 0.3; 0.1 0.7]
M1 = 2×2
0.9000 0.3000
0.1000 0.7000
Dynamics of the system
Let's record the values of the system after 20 iterations in an array with two different starting conditions
iterNo=20;
close all;
% Case 1: at time t=0, 100% of the population is healthy
x0 = [1;0]; % Initial conditions
XX = [x0]; % Array recording all states of the system
% XX(:,t) is a vector encoding the state of the system at time t
for t=1:iterNo
XX = [XX M1*XX(:,end)];
end
% Plotting the X trajectories
figure(1)
plot(0:iterNo,XX,'o')
title('X trajectories')
1
% Case 2: at time t=0, 100% of the peopulation is sick
y0 = [0;1]; % Initial conditions
YY = [y0]; % Array recording all states of the system
% YY(:,t) is a vector encoding the state of the system at time t
for t=1:iterNo
YY = [YY M1*YY(:,end)];
end
% Plotting the Y trajectories
figure(2)
plot(0:iterNo,YY,'o')
title('Y trajectories')
2
% Plotting the convergence of both trajectories
figure(3)
plot(0:iterNo,max(abs(XX-YY)), 'or')
xlabel('Iteration i')
ylabel('Max difference between the two vectors xi and yi')
3
Comments:
We can observe that we converge very quickly toward the same vectors, despite very different starting vectors.
This vector is the vector [0.75;0.25], so we can guess that the dynamics of the system described by M1,
independently of the initial conditions, tends to 75% of the population being healthy and 25% being sick.
Eigenvalues/vectors
[V1 D1]=eig(M1)
V1 = 2×2
0.9487 -0.7071
0.3162 0.7071
D1 = 2×2
1.0000 0
0 0.6000
We can observe an eigenvalue of 1 that dominates all other eigenvalues.
What is the associated probability eigenvector?
probaV1 = V1(:,1) / sum(V1(:,1))
probaV1 = 2×1
0.7500
0.2500
4
Not surprisingly, the probability eigenvector probV1 is the same as the vector toward which the system encoded
by M1 converges.
EXAMPLE 2
Generating a random stochastic matrix
close all;
n = 100;
randM = rand(n);
randStochM = randM ./ sum(randM);
% Illustration of Perron-Frobenius Theorem
% Dominance of the eigenvalue 1
figure(1)
bar(abs(eig(randStochM)))
% Probability eigenvector associated to the eigenvalue 1.
[randV randD] = eig(randStochM);
randD(1,1)
ans = 1.0000
randStochV = randV(:,1) / sum(randV(:,1));
figure(2)
bar(randStochV)
5
EXAMPLE 3
clear; close all;
Looking at a non-regular matrix
% 4x4 non-regular matrix.
M2 = [ 0.8 0.3 0 0; 0.2 0.7 0 0; 0 0 0.9 0.3; 0 0 0.1 0.7]
M2 = 4×4
0.8000 0.3000 0 0
0.2000 0.7000 0 0
0 0 0.9000 0.3000
0 0 0.1000 0.7000
% By its structure with two 2x2 blocks of zeros, any power of this matrix
% will always have these two blocks of zeros.
M2^10
ans = 4×4
0.6004 0.5994 0 0
0.3996 0.4006 0 0
0 0 0.7515 0.7455
0 0 0.2485 0.2545
Eigenvalues.
[V2, D2] = eig(M2)
6
V2 = 4×4
0.8321 -0.7071 0 0
0.5547 0.7071 0 0
0 0 0.9487 -0.7071
0 0 0.3162 0.7071
D2 = 4×4
1.0000 0 0 0
0 0.5000 0 0
0 0 1.0000 0
0 0 0 0.6000
sort(diag(D2),'descend')
ans = 4×1
1.0000
1.0000
0.6000
0.5000
% We can observe that eigenvalue 1 has algebraic mutiplicity 2.
Iterating the system
iterNo=50;
x0 = [0.7; 0.2; 0.1 ;0]; % Initial conditions
XX = [x0]; % Array recording all states of the system
% XX(:,t) is a vector encoding the state of the system at time t
for t=1:iterNo
XX = [XX M2*XX(:,end)];
end
plot(0:iterNo,XX,'o')
7
y0 = [0; 0.1; 0.5 ;0.4]; % Initial conditions
YY = [y0]; % Array recording all states of the system
% XX(:,t) is a vector encoding the state of the system at time t
for t=1:iterNo
YY = [YY M2*YY(:,end)];
end
plot(0:iterNo,YY,'o')
8
% Looking at the final states
finals=[XX(:,end) YY(:,end)]
finals = 4×2
0.5400 0.0600
0.3600 0.0400
0.0750 0.6750
0.0250 0.2250
% Looking at the probability eigenvectors of the two occurrences of 1 as an
% eigenvalue
evec=[ V2(:,1) ./ sum(V2(:,1)) V2(:,3) ./ sum(V2(:,3))]
evec = 4×2
0.6000 0
0.4000 0
0 0.7500
0 0.2500
% They do not correspond to the two final states of the system after 50
% iterations depending on the initial conditions.
Comments
The non-regularity of the matrix manifests that there can be different vectors toward which the system
converges depending on the initial conditions, and they do not match with the eigenvectors of 1.
9
essay、essay代写