Skip to content
This repository was archived by the owner on Jul 25, 2023. It is now read-only.

Commit 5b5ff38

Browse files
author
Jordi
committed
2 parents 8fc8c76 + 7a9270a commit 5b5ff38

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+3384
-2
lines changed

day_6/Lecture Monday.pdf

919 KB
Binary file not shown.

day_6/Lorenz63.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
"""
4+
Created on Thu Jul 21 11:53:19 2022
5+
6+
@author: Simone Servadio
7+
"""
8+
"""This file solves the Lorenz 63 system"""
9+
"""simplified mathematical model for atmospheric convection
10+
The equations relate the properties of a two-dimensional fluid layer uniformly
11+
warmed from below and cooled from above. In particular, the equations describe
12+
the rate of change of three quantities with respect to time: x is proportional
13+
to the rate of convection, y to the horizontal temperature variation, and z to
14+
the vertical temperature variation. The constants σ, ρ, and β are system parameters
15+
proportional to the Prandtl number, Rayleigh number, and certain physical
16+
dimensions of the layer itself."""
17+
18+
19+
#%matplotlib qt
20+
import sys
21+
import numpy as np
22+
import matplotlib.pyplot as plt
23+
from scipy.integrate import odeint
24+
25+
26+
27+
28+
def Lorenz63(x,t,sigma,rho,beta):
29+
"""The Lorenz63 system for different coefficients"""
30+
xdot = np.zeros(3)
31+
xdot[0] = sigma*(x[1]-x[0]);
32+
xdot[1] = x[0]*(rho-x[2])-x[1];
33+
xdot[2] = x[0]*x[1] - beta*x[2];
34+
return xdot
35+
36+
37+
"Set parameters"
38+
rho = 28
39+
sigma = 10
40+
beta = 8/3
41+
42+
x0 = 5*np.ones(3); #initial state
43+
t_in = 0 #initial time
44+
t_fin = 20 #final time
45+
n_points = 1000
46+
time = np.linspace(t_in,t_fin,n_points) #time vector
47+
48+
# Solve ODE
49+
y = odeint(Lorenz63,x0,time,args = (sigma,rho,beta)) #propagation
50+
51+
# Display
52+
fig1 = plt.figure()
53+
ax = plt.axes(projection='3d')
54+
ax.plot3D(y[:,0], y[:,1], y[:,2],'b') #3d plottingsolution
55+
#sys.exit()
56+
57+
58+
59+
60+
61+
62+
63+
64+
65+
"Random Initial Condition"
66+
n_ic = 20 #number of initial conditions
67+
x0s = np.zeros((3,n_ic))
68+
69+
#Initial condition domain
70+
x0s[0,:] = 20 * 2 * (np.random.rand(n_ic) - 0.5);
71+
x0s[1,:] = 30 * 2 * (np.random.rand(n_ic) - 0.5);
72+
x0s[2,:] = 50 * np.random.rand(n_ic);
73+
74+
75+
fig2 = plt.figure()
76+
ax = plt.axes(projection='3d')
77+
for i in range(n_ic):
78+
y = odeint(Lorenz63,x0s[:,i],time,args = (sigma,rho,beta)) #propagation
79+
ax.plot3D(y[:,0], y[:,1], y[:,2]) #visualization
80+
81+
ax.set_xlabel('x')
82+
ax.set_ylabel('y')
83+
ax.set_zlabel('z')
84+
sys.exit()
85+
86+
87+
88+
89+
90+
91+
92+
93+
94+
95+
96+
97+
98+
99+
100+
101+
102+

day_6/LotkaVolterra.py

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
"""
4+
Created on Thu Jul 21 12:30:53 2022
5+
6+
@author: home
7+
"""
8+
9+
"""Data available for this script at
10+
https://github.com/stan-dev/example-models/tree/master/knitr/lotka-volterra
11+
and useful presentation that explains the derivation of optimal values
12+
https://jmahaffy.sdsu.edu/courses/f17/math636/beamer/lotvol-04.pdf"""
13+
14+
import numpy as np
15+
import matplotlib.pyplot as plt
16+
import pandas as pd
17+
from scipy.integrate import odeint
18+
19+
def Predator_vs_prey(x,t,prey_up,prey_down,pred_up,pred_down):
20+
xdot = np.zeros(2)
21+
xdot[0] = prey_up*x[0] - prey_down*x[0]*x[1] # prey equation
22+
xdot[1] = pred_down*x[0]*x[1]- pred_up*x[1] # predator equation
23+
return xdot
24+
25+
"Load Population"
26+
pop = pd.read_csv("hudson-bay-lynx-hare.csv")
27+
28+
29+
"Dysplay population"
30+
fig1 = plt.figure()
31+
plt.plot(pop['Year'],pop['Hare'],'o-b')
32+
plt.plot(pop['Year'],pop['Lynx'],'o-r')
33+
plt.grid()
34+
plt.xlabel('Years')
35+
plt.ylabel('Population')
36+
37+
fig2 = plt.figure()
38+
plt.plot(pop['Lynx'],pop['Hare'],'o-b')
39+
plt.grid()
40+
plt.xlabel('Lynx')
41+
plt.ylabel('Hare')
42+
43+
"Propagation"
44+
n_points = len(pop['Year'])
45+
grow_hare = 0.48069
46+
shrink_hare = 0.024822
47+
grow_lynx = 0.92718
48+
shrink_lynx = 0.027564
49+
50+
time = np.linspace(pop['Year'][0],pop['Year'].iat[-1],1000)
51+
52+
fig3 = plt.figure()
53+
plt.grid()
54+
for i in range(n_points):
55+
x0 = [pop['Hare'][i],pop['Lynx'][i]]
56+
y = odeint(Predator_vs_prey,x0,time,args=(grow_hare,shrink_hare,grow_lynx,shrink_lynx))
57+
plt.plot(y[:,1],y[:,0],'grey')
58+
59+
plt.plot(pop['Lynx'],pop['Hare'],'ob')
60+
61+
62+
63+
"Optimal Population"
64+
x0_opt = [34.9134, 3.8566] # Optimal Initial Estimates
65+
y = odeint(Predator_vs_prey,x0_opt,time,args=(grow_hare,shrink_hare,grow_lynx,shrink_lynx))
66+
plt.plot(y[:,1],y[:,0],'r')
67+
68+
fig4 = plt.figure()
69+
plt.plot(pop['Year'],pop['Hare'],'ob');
70+
plt.plot(pop['Year'],pop['Lynx'],'or');
71+
plt.plot(time,y[:,0],'b')
72+
plt.plot(time,y[:,1],'r')
73+
plt.grid()
74+
plt.xlabel('Years')
75+
plt.ylabel('Population')
76+
plt.legend(['Hare truth','Lynx thruth','Hare model','Lynx model'])
77+
78+
"Average populations"
79+
hare_ave_theory = grow_lynx/shrink_lynx
80+
lynx_ave_theory = grow_hare/shrink_hare
81+
82+
hare_ave_data = pop["Hare"].mean()
83+
lynx_ave_data = pop["Lynx"].mean()
84+
85+
hare_ave_prop = y[:,0].mean()
86+
lynx_ave_prop = y[:,1].mean()
87+
88+
x0s = np.array([[hare_ave_theory, lynx_ave_theory],
89+
[hare_ave_data, lynx_ave_data],
90+
[hare_ave_prop,lynx_ave_prop]])
91+
92+
for i in range(3):
93+
y = odeint(Predator_vs_prey,x0s[i,:],time,args=(grow_hare,shrink_hare,grow_lynx,shrink_lynx))
94+
fig3.axes[0].plot(y[:,1],y[:,0],'b')
95+
fig4.axes[0].plot(time,y[:,0],'c')
96+
fig4.axes[0].plot(time,y[:,1],'m')
97+
98+
99+
100+
101+
102+
103+
104+
105+
106+
107+
108+
109+
110+
111+
112+
113+
114+
115+

0 commit comments

Comments
 (0)