-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
178 lines (156 loc) · 4.78 KB
/
main.py
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
175
176
177
178
import numpy as np
#import seaborn as sb
import matplotlib.pyplot as plt
# a,b,c :les coeficients de la matrice de runge kutta
# x0,t0 : les conditions initiales
# f : fonction dependant de x et de t (equation differentielle)
# h : pas de temps
def RungeKutta(a,b,c,x0,t0,f,h):
p=np.array([x0 for i in range(len(a))])
x=x0
t=t0
for i in range(len(a)):
t=t0+h*c[i]
x=x0+h*sum(np.array([a[i,j]*p[j] for j in range(len(a))]))
p[i]=f(x,t)
return(x0+h*sum(np.array([b[j]*p[j] for j in range(len(a))])),t0+h);
# a,b,c :les coeficients de la matrice de runge kutta
# x0,t0 : les conditions initiales
# f : fonction dependant de x et de t (equation differentielle)
# h : pas de temps
# param : nombre d'iteration/ temps de simulation
# cond : vaut false si on veut simuler sur n iteraion et True si on veut simuler sur un certain temps
def RungeKuttaSim(a,b,c,x0,t0,f,h,param=100,cond=False):
n=0
if(cond):
n=(int)(param/h)
else:
n=param
t=[0]*n
x=[0]*n
x[0]=x0
t[0]=t0
for i in range(1,n):
if(i%500==0):
print("progress :",i/n*100,"%")
(x[i],t[i])=RungeKutta(a,b,c,x[i-1],t[i-1],f,h)
return (x,t)
# x0,t0 : les conditions initiales
# f : fonction dependant de x et de t (equation differentielle)
# derf : la jacobienne de f
# h : pas de temps
# solveIter : le nombre d'itération de la méthode de Newton à effectuer
def EulerImplicite(x0,t0,f,derf,h,solveIter=5):
#schema euler implicite
x=x0
t=t0+h
for i in range(solveIter):
x=x-np.dot(np.linalg.inv(h*derf(x,t)-np.eye(len(x))),x0+h*f(x,t)-x)
return(x,t)
# x0,t0 : les conditions initiales
# f : fonction dependant de x et de t (equation differentielle)
# derf : la jacobienne de f
# h : pas de temps
# solveIter : le nombre d'itération de la méthode de Newton à effectuer
# param : nombre d'iteration/ temps de simulation
# cond : vaut false si on veut simuler sur n iteraion et True si on veut simuler sur un certain temps
def EulerImpliciteSim(x0,t0,f,derf,h,solveIter,param=100,cond=False):
n=0
if(cond):
n=(int)(param/h)
else:
n=param
t=[0]*n
x=[0]*n
x[0]=x0
t[0]=t0
for i in range(1,n):
if(i%500==0):
print("progress :",i/n*100,"%")
(x[i],t[i])=EulerImplicite(x[i-1],t[i-1],f,derf,h,solveIter)
return(x,t)
#RK-4 coefficient
a=np.array([[0,0,0,0],
[1/2,0,0,0],
[0,1/2,0,0],
[0,0,1,0]])
b=np.array([1/6,2/6,2/6,1/6])
c=np.array([0,1/2,1/2,1])
#coefficient for the testing system
#sys1
#"""
alpha=1
beta=1/20
delta=1/200
gamma=1/2
"""
#sys2
alpha=3
beta=1/40
delta=2
gamma=1/10
epsilon=1/20
zeta=1/40
eta=1/20
theta=1
iota=1/20
kappa=1/20
#"""
#custom
hunt=0.05
C=0.5
debut=10
fin=30
A=0.25
#testing system
def sys1(x,t):
return np.array([x[0]*(alpha-beta*x[1])
,x[1]*(delta*x[0]-gamma)])
def sys2(x,t):
return np.array([x[0]*(alpha-beta*x[0]-gamma*x[1]),
x[1]*(delta-epsilon*x[1]-zeta*x[0]-eta*x[2]),
x[2]*(theta*x[1]-iota*x[2]-kappa)])
def derSys1(x,t):
return np.array([[(alpha-beta*x[1]),-x[0]*beta],
[x[1]*delta,(delta*x[0]-gamma)]])
def derSys2(x,t):
return np.array([[(alpha-2*beta*x[0]-gamma*x[1]),-x[0]*gamma,0],
[-x[1]*zeta,(delta-2*epsilon*x[1]-zeta*x[0]-eta*x[2]),-x[1]*eta],
[0,-x[2]*theta,(theta*x[1]-2*iota*x[2]-kappa)]])
def sysCustom(x,t):
return np.array([x[0]*(alpha-beta*x[1])
,x[1]*(delta*x[0]-gamma-t*hunt)])
def guerreMondiale(x,t):
return np.array([x[0]*(alpha-beta*x[1]-chasse(t))
,x[1]*(delta*x[0]-gamma)])
def chasse(t):
out=C
if(t>debut and t<fin):
out=out-A*(np.sin(np.pi*((t-debut)/(fin-debut)))**(2))
return out
"""
(x,t)=EulerImpliciteSim(np.array([80,30]),0,sys1,derSys1,0.01,5,50,True)
plt.plot(t,[i[0] for i in x])
plt.plot(t,[i[1] for i in x])
#plt.plot(t,[i[2] for i in x])
plt.title("Système 2 Euler Implicite système 1")
#"""
(x1,t1)=RungeKuttaSim(a,b,c,np.array([100,10]),0,guerreMondiale,0.001,60,True)
plt.plot(t1,[i[0] for i in x1])
plt.plot(t1,[i[1] for i in x1])
plt.plot(t1,[90*chasse(i) for i in t1])
plt.plot(t1,[8*i[0]/i[1] for i in x1])
plt.title("chasse sur les proies")
plt.legend(["proie","predateur","chasse (pas à l'échelle)","rapport proie/prédateur (pas à l'échelle)"])
"""
plt.title("surchasse sur les proies")
(x1,t1)=RungeKuttaSim(a,b,c,np.array([100,20]),0,sysCustom,0.001,60,True)
plt.xlabel('Prédateur')
plt.ylabel('Proie')
plt.plot([i[1] for i in x1],[i[0] for i in x1])
#"""
plt.grid(which='major', linestyle='-')
plt.grid(which='minor', linestyle='--',linewidth=0.3)
plt.minorticks_on()
plt.gca().set_ylim(bottom=0)
plt.show()