This repository has been archived by the owner on Jun 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modello_tre_specie.m
executable file
·174 lines (160 loc) · 4.35 KB
/
modello_tre_specie.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
%% What we are doing???
% Volpi rabbiose
% 2 dimension
% In this case we choose Neumann BCs.
%% SPACE DOMAIN
L1 = 100;
L2 = 100;
Deltax = 0.5;
Deltay = 0.5;
Nx = L1/Deltax-1;
Ny = L2/Deltay-1;
s1 = linspace(0,L1,Nx+2);
s2 = linspace(0,L2,Ny+2);
s1 = s1(2:Nx+1);
s2 = s2(2:Ny+1);
%% TIME DOMAIN
Deltat = 0.2;
Tmax = 200;
Tout = 1;
%% PARAMETERS
a = 1.0; %1 per year
b = 0.5; %0.5 per year
k = 1; %0.25 to 4 foxes per km^-2
bet = 80.0; % 80 km^2 per year
sigma = 1.0/28; % days^-1
alfa = 1.0/5; % days^-1
d = 1.0; % from 60 to 330 km^2 per year
%Kt = (sigma + a)*(alfa + a)/(sigma*bet);
%% WRITE THE APPROXIMATE SCHEME (Crank-Nicholson method)
alpha = 0.5*Deltat/Deltax^2*d*d;
beta = 0.5*Deltat/Deltay^2*d*d;
% generate matrix of tridiagonal system
aux = zeros(Ny*Nx,5);
aux(:,3) = 1 + 2*alpha + 2*beta ; % main diagonal
for j = 1:Ny
aux((j-1)*Nx+1,3) = aux((j-1)*Nx+1,3) - beta;
aux(j*Nx,3) = aux(j*Nx,3) - beta;
end
for i = 1:Nx
aux(i,3) = aux(i,3) - alpha;
aux((Ny-1)*Nx+i,3) = aux((Ny-1)*Nx+i,3) - alpha;
end
aux(:,2) = -alpha;
aux(:,1) = -beta;
aux(:,4) = -alpha;
aux(:,5) = -beta;
for j = 1:Ny
aux(j*Nx,2) = 0;
aux((j-1)*Nx+1,4) = 0;
end
A = (spdiags( aux, [-Nx -1 0 1 Nx], Ny*Nx,Ny*Nx))'; % transposed of the tridiagonal matrix with the given values
aux(:,3) = 1 - 2*alpha - 2*beta ; % main diagonal
for j = 1:Ny
aux((j-1)*Nx+1,3) = aux((j-1)*Nx+1,3) + beta;
aux(j*Nx,3) = aux(j*Nx,3) + beta;
end
for i = 1:Nx
aux(i,3) = aux(i,3) + alpha;
aux((Ny-1)*Nx+i,3) = aux((Ny-1)*Nx+i,3) + alpha;
end
aux(:,2) = alpha;
aux(:,1) = beta;
aux(:,4) = alpha;
aux(:,5) = beta;
for j = 1:Ny
aux(j*Nx,2) = 0;
aux((j-1)*Nx+1,4) = 0;
end
B = (spdiags( aux, [-Nx -1 0 1 Nx], Ny*Nx,Ny*Nx))'; % transposed of the tridiagonal matrix with the given values
%% Initial Conditions
S = zeros(Ny*Nx, 1);
S(:) = k;
I = zeros(Ny*Nx, 1);
Rmat = zeros(Nx, Ny);
Rmat(floor(Nx/2),2) = 0.001;
R = reshape(Rmat,Ny*Nx,1);
M = Tmax/Deltat;
memory = cell(3,M+1);
t = 0;
memory{1,1} = reshape(S, [Nx, Ny]);
memory{2,1} = reshape(I, [Nx, Ny]);
memory{3,1} = reshape(R, [Nx, Ny]);
%% TIME EVOLUTIONARY STEPS
[L,U,P] = lu(A);
i=1;
for j=1:M % temporal steps
t = t + Deltat;
N = S + I + R;
SN = S + ((a-b)*(1-N/k) - bet*R).*S*Deltat;
IN = I + (bet*S.*R - (sigma + b +(a-b)*N/k).*I)*Deltat;
rhs = B*R + (sigma*I - (alfa + b + (a-b)*N/k).*R)*Deltat;
y = L\rhs;
RN = U\y; % solution through two triangular systems
S = SN;
I = IN;
R = RN;
if (t >= i*Tout)
i = i + 1;
memory{1,i} = reshape(S, [Nx, Ny]);
memory{2,i} = reshape(I, [Nx, Ny]);
memory{3,i} = reshape(R, [Nx, Ny]);
end
end
%% GIF
fig = figure('pos',[20 10 1200 600]);
filename = 'cosa.gif';
ha = tight_subplot(1,3,[.01 .04],[.1 .1],[.04 .01]);
for j=1:i
axes(ha(1));
surfl(s2, s1, memory{1,j});
xlim([0, L2])
ylim([0, L1])
zlim([0, 2])
%set(gca,'xtick',[])
set(gca,'xticklabel',[])
%set(gca,'ytick',[])
set(gca,'yticklabel',[])
shading interp;
title('Volpi sane')
colormap(gca, winter);
axes(ha(2));
surfl(s2, s1, memory{3,j});
xlim([0, L2])
ylim([0, L1])
zlim([0, 0.04])
%set(gca,'xtick',[])
set(gca,'xticklabel',[])
%set(gca,'ytick',[])
set(gca,'yticklabel',[])
shading interp;
title('Volpi rabbiose')
colormap(gca, spring);
delete(findall(gcf,'type','annotation'));
axes(ha(3));
surfl(s2, s1, memory{2,j});
xlim([0, L2])
ylim([0, L1])
zlim([0, 0.5])
%set(gca,'xtick',[])
set(gca,'xticklabel',[])
%set(gca,'ytick',[])
set(gca,'yticklabel',[])
shading interp;
title('Volpi quiescenti')
colormap(gca, spring);
delete(findall(gcf,'type','annotation'));
t = annotation('textbox', [0.5, 0.9, 0.1, 0.1], 'string', ['Time: ', num2str((j-1)*Tout)]);
t.FontSize = 12;
t.FontWeight = 'bold';
set(fig, 'NumberTitle', 'off', 'Name', sprintf('Time: %f', (j-1)*Tout));
drawnow
frame = getframe(fig);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if j == 1
imwrite(imind,cm,filename,'gif', 'DelayTime',0.05, 'Loopcount',inf);
else
imwrite(imind,cm,filename,'gif', 'DelayTime',0.05,'WriteMode','append');
end
end