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/chap22.py
Views: 531
1
from modsim import *
2
3
params = Params(
4
x = 0, # m
5
y = 1, # m
6
angle = 45, # degree
7
speed = 40, # m / s
8
9
mass = 145e-3, # kg
10
diameter = 73e-3, # m
11
C_d = 0.33, # dimensionless
12
13
rho = 1.2, # kg/m**3
14
g = 9.8, # m/s**2
15
t_end = 10, # s
16
)
17
18
from modsim import *
19
20
from numpy import pi, deg2rad
21
22
def make_system(params):
23
24
# convert angle to radians
25
theta = deg2rad(params.angle)
26
27
# compute x and y components of velocity
28
vx, vy = pol2cart(theta, params.speed)
29
30
# make the initial state
31
init = State(x=params.x, y=params.y, vx=vx, vy=vy)
32
33
# compute the frontal area
34
area = pi * (params.diameter/2)**2
35
36
return System(params,
37
init = init,
38
area = area)
39
40
from modsim import *
41
42
def drag_force(V, system):
43
rho, C_d, area = system.rho, system.C_d, system.area
44
45
mag = rho * vector_mag(V)**2 * C_d * area / 2
46
direction = -vector_hat(V)
47
f_drag = mag * direction
48
return f_drag
49
50
from modsim import *
51
52
def slope_func(t, state, system):
53
x, y, vx, vy = state
54
mass, g = system.mass, system.g
55
56
V = Vector(vx, vy)
57
a_drag = drag_force(V, system) / mass
58
a_grav = g * Vector(0, -1)
59
60
A = a_grav + a_drag
61
62
return V.x, V.y, A.x, A.y
63
64
from modsim import *
65
66
def event_func(t, state, system):
67
x, y, vx, vy = state
68
return y
69
70
71