CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
AllenDowney

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: AllenDowney/ModSimPy
Path: blob/master/chap15.py
Views: 531
1
from modsim import *
2
3
def make_system(T_init, volume, r, t_end):
4
return System(T_init=T_init,
5
T_final=T_init,
6
volume=volume,
7
r=r,
8
t_end=t_end,
9
T_env=22,
10
t_0=0,
11
dt=1)
12
13
from modsim import *
14
15
def change_func(t, T, system):
16
r, T_env, dt = system.r, system.T_env, system.dt
17
return -r * (T - T_env) * dt
18
19
from modsim import *
20
21
def run_simulation(system, change_func):
22
t_array = linrange(system.t_0, system.t_end, system.dt)
23
n = len(t_array)
24
25
series = TimeSeries(index=t_array)
26
series.iloc[0] = system.T_init
27
28
for i in range(n-1):
29
t = t_array[i]
30
T = series.iloc[i]
31
series.iloc[i+1] = T + change_func(t, T, system)
32
33
system.T_final = series.iloc[-1]
34
return series
35
36
37