-
Notifications
You must be signed in to change notification settings - Fork 29
/
lecture1_ex1.jl
99 lines (77 loc) · 3.22 KB
/
lecture1_ex1.jl
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
md"""
## Exercise 1 — **Car travel**
"""
#md # 👉 [Download the notebook to get started with this exercise!](https://github.com/eth-vaw-glaciology/course-101-0250-00/blob/main/exercise-notebooks/notebooks/lecture1_ex1.ipynb)
#md #
#md # \warn{Write a monolithic Julia script to solve this exercise in a Jupyter notebook and hand it in on Moodle ([more](/homework)).}
md"""
The goal of this exercise is to familiarise with:
- code structure `# Physics, # Numerics, # Time loop, # Visualisation`
- array initialisation
- `for` loop & `if` condition
- update rule
- solving ODEs
"""
md"""
A car cruises on a straight road at given speed $\mathrm{V = 113}$ km/h for 16 hours, making a U-turn after a distance $\mathrm{L = 200}$ km. The car speed is defined as the change of position its $x$ per time $t$:
$$
V = \frac{\partial x}{\partial t}
$$
In the above derivative, $\partial x$ and $\partial t$ are considered infinitesimal - a representation it is not possible to handle within a computer (as it would require infinite amount of resources). However, we can discretise this differential equation in order to solve it numerically by transforming the infinitesimal quantities into discrete increments:
$$
V = \frac{\partial x}{\partial t} \approx \frac{\Delta x}{\Delta t} = \frac{x_{t+\Delta t}-x_t}{\Delta t}~,
$$
where $\Delta x$ and $\Delta t$ are discrete quantities. This equation can be re-organised to return an explicit solution of the position $x$ at time $t+\Delta t$:
$$
x_{t+\Delta t} = x_{t} + V \Delta t~.
$$
1. Based on this equation, your task is to setup a numerical model to predict the position of the car as function of time. In order not to start from scratch this time, you can complete the code draft below, filling in the relevant quantities in following order:
"""
using Plots
@views function car_travel_1D()
## Physical parameters
## Numerical parameters
## Array initialisation
## Time loop
## Visualisation
return
end
car_travel_1D()
md"""
2. Implement a condition to allow you doing U-turns whenever you reach the position $x=0$ or $x=200$.
The sample code you can use to get started looks like:
"""
using Plots
@views function car_travel_1D()
## Physical parameters
V = # speed, km/h
L = # length of segment, km
ttot = # total time, h
## Numerical parameters
dt = 0.1 # time step, h
nt = Int(cld(ttot, dt)) # number of time steps
## Array initialisation
T =
X =
## Time loop
for it = 2:nt
T[it] = T[it-1] + dt
X[it] = # move the car
if X[it] > L
## if beyond L, go back (left)
elseif X[it] < 0
## if beyond 0, go back (right)
end
end
## Visualisation
display(scatter(T, X, markersize=5,
xlabel="time, hrs", ylabel="distance, km",
framestyle=:box, legend=:none))
return
end
car_travel_1D()
md"""
Note that you can extend the first code (from step 1) to include the U-turns and use your final code to reply to the question.
### Question 1
Once the code is running, test various time step increments `0.1 < dt < 1.0` and briefly comment on your findings.
"""