-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw0.jl
396 lines (309 loc) · 12 KB
/
hw0.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
### A Pluto.jl notebook ###
# v0.14.8
using Markdown
using InteractiveUtils
# This Pluto notebook uses @bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of @bind gives bound variables a default value (instead of an error).
macro bind(def, element)
quote
local el = $(esc(element))
global $(esc(def)) = Core.applicable(Base.get, el) ? Base.get(el) : missing
el
end
end
# ╔═╡ 851c03a4-e7a4-11ea-1652-d59b7a6599f0
# setting up an empty package environment
begin
import Pkg
Pkg.activate(mktempdir())
Pkg.Registry.update()
end
# ╔═╡ d6ee91ea-e750-11ea-1260-31ebf3ec6a9b
# add (ie install) a package to our environment
begin
Pkg.add("Compose")
# call `using` so that we can use it in our code
using Compose
end
# ╔═╡ 5acd58e0-e856-11ea-2d3d-8329889fe16f
begin
Pkg.add("PlutoUI")
using PlutoUI
end
# ╔═╡ fafae38e-e852-11ea-1208-732b4744e4c2
md"_Homework 0, version 3 -- Spring 2021_"
# ╔═╡ a2181260-e6cd-11ea-2a69-8d9d31d1ef0e
md"""
# Homework 0: Getting up and running
HW0 release date: Monday, Feb 15, 2021.
**HW0 due date: Thursday, Feb 18, 2021, 11:59pm EST**, _but best completed before Wednesday's lecture if possible_.
First of all, **_welcome to the course!_** We are excited to teach you about real world applications of scientific computing, using the same tools that we work with ourselves.
We'd like everyone to **submit this zeroth homework assignment**. It will not affect your grade, but it will help us get everything running smoothly when the course starts. If you're stuck or don't have much time, just fill in your name and ID and submit 🙂
"""
# ╔═╡ 31a8fbf8-e6ce-11ea-2c66-4b4d02b41995
md"""## Homework Logistics
Homeworks are in the form of [Pluto notebooks](https://github.com/fonsp/Pluto.jl). Your must complete them and submit them on [Canvas](https://canvas.mit.edu/courses/5637) (if you are an MIT student.). If you are not an MIT student, we encourage you to [join Discord](https://discord.gg/Z5qnVf8) and find someone to cross-grade.
Homeworks will be released on Thursdays and due on Thursdays 11:59pm Eastern time.
HW0 is for you to get your system set up correctly and to test our grading software. You must submit it but it will not count towards your grade.
"""
# ╔═╡ f9d7250a-706f-11eb-104d-3f07c59f7174
md"## Requirements of this HW0
- Install Julia and set up Pluto
- Do the required Exercise 0.
That’s it, but if you like you can do the _OPTIONAL_ exercises that follow."
# ╔═╡ 430a260e-6cbb-11eb-34af-31366543c9dc
md"""# Installation
Before being able to run this notebook succesfully locally, you will need to [set up Julia and Pluto.](/Spring21/installation/)
One you have Julia and Pluto installed, you can click the button at the top right of this page and follow the instructions to edit this notebook locally and submit.
"""
# ╔═╡ a05d2bc8-7024-11eb-08cb-196543bbb8fd
md"## (Required) Exercise 0 - _Making a basic function_
Computing the square of a number is easy -- you just multiply it with itself.
##### Algorithm:
Given: $x$
Output: $x^2$
1. Multiply `x` by `x`"
# ╔═╡ e02f7ea6-7024-11eb-3672-fd59a6cff79b
function basic_square(x)
return x*x
end
# ╔═╡ 6acef56c-7025-11eb-2524-819c30a75d39
let
result = basic_square(5)
if !(result isa Number)
md"""
!!! warning "Not a number"
`basic_square` did not return a number. Did you forget to write `return`?
"""
elseif abs(result - 5*5) < 0.01
md"""
!!! correct
Well done!
"""
else
md"""
!!! warning "Incorrect"
Keep working on it!
"""
end
end
# ╔═╡ 348cea34-7025-11eb-3def-41bbc16c7512
md"That's all that's required for this week. Please submit the notebook. We just wanted to make sure that you're up and running.
If you want to explore further, we have included a few optional exercises below."
# ╔═╡ 339c2d5c-e6ce-11ea-32f9-714b3628909c
md"## (Optional) Exercise 1 - _Square root by Newton's method_
Computing the square of a number is easy -- you already did it.
But how does one compute the square root of a number?
##### Algorithm:
Given: $x$
Output: $\sqrt{x}$
1. Take a guess `a`
1. Divide `x` by `a`
1. Set a = the average of `x/a` and `a`. (The square root must be between these two numbers. Why?)
1. Repeat until `x/a` is roughly equal to `a`. Return `a` as the square root.
In general, you will never get to the point where `x/a` is _exactly_ equal to `a`. So if our algorithm keeps going until `x/a == a`, then it will get stuck.
So instead, the algorithm takes a parameter `error_margin`, which is used to decide when `x/a` and `a` are close enough to halt.
"
# ╔═╡ 56866718-e6ce-11ea-0804-d108af4e5653
md"### Exercise 1.1
Step 3 in the algorithm sets the new guess to be the average of `x/a` and the old guess `a`.
This is because the square root must be between the numbers `x/a` and `a`. Why?
"
# ╔═╡ bccf0e88-e754-11ea-3ab8-0170c2d44628
ex_1_1 =
md"""
x^2<x<1 if set a = 1
"""
# you might need to wait until all other cells in this notebook have completed running.
# scroll down the page to see what's up
# ╔═╡ e7abd366-e7a6-11ea-30d7-1b6194614d0a
if !(@isdefined ex_1_1)
md"""Do not change the name of the variable - write you answer as `ex_1_1 = "..."`"""
end
# ╔═╡ d62f223c-e754-11ea-2470-e72a605a9d7e
md"### Exercise 1.2
Write a function newton_sqrt(x) which implements the above algorithm."
# ╔═╡ 4896bf0c-e754-11ea-19dc-1380bb356ab6
function newton_sqrt(x, error_margin=0.01, a=x / 2) # a=x/2 is the default value of `a`
while abs(x/a-a)>=error_margin
a = (x/a + a)/2
end
return a
end
# ╔═╡ 7a01a508-e78a-11ea-11da-999d38785348
newton_sqrt(2)
# ╔═╡ 682db9f8-e7b1-11ea-3949-6b683ca8b47b
let
result = newton_sqrt(2, 0.01)
if !(result isa Number)
md"""
!!! warning "Not a number"
`newton_sqrt` did not return a number. Did you forget to write `return`?
"""
elseif abs(result - sqrt(2)) < 0.01
md"""
!!! correct
Well done!
"""
else
md"""
!!! warning "Incorrect"
Keep working on it!
"""
end
end
# ╔═╡ 088cc652-e7a8-11ea-0ca7-f744f6f3afdd
md"""
!!! hint
`abs(r - s)` is the distance between `r` and `s`
"""
# ╔═╡ c18dce7a-e7a7-11ea-0a1a-f944d46754e5
md"""
!!! hint
If you're stuck, feel free to cheat, this is homework 0 after all 🙃
Julia has a function called `sqrt`
"""
# ╔═╡ 5e24d95c-e6ce-11ea-24be-bb19e1e14657
md"## (Optional) Exercise 2 - _Sierpinksi's triangle_
Sierpinski's triangle is defined _recursively_:
- Sierpinski's triangle of complexity N is a figure in the form of a triangle which is made of 3 triangular figures which are themselves Sierpinski's triangles of complexity N-1.
- A Sierpinski's triangle of complexity 0 is a simple solid equilateral triangle
"
# ╔═╡ 6b8883f6-e7b3-11ea-155e-6f62117e123b
md"To draw Sierpinski's triangle, we are going to use an external package, [_Compose.jl_](https://giovineitalia.github.io/Compose.jl/latest/tutorial). Let's set up a package environment and add the package.
A package contains a coherent set of functionality that you can often use a black box according to its specification. There are [lots of Julia packages](https://juliahub.com/ui/Home).
"
# ╔═╡ dbc4da6a-e7b4-11ea-3b70-6f2abfcab992
md"Just like the definition above, our `sierpinksi` function is _recursive_: it calls itself."
# ╔═╡ 02b9c9d6-e752-11ea-0f32-91b7b6481684
complexity = 5
# ╔═╡ 1eb79812-e7b5-11ea-1c10-63b24803dd8a
if complexity == 3
md"""
Try changing the value of **`complexity` to `5`** in the cell above.
Hit `Shift+Enter` to affect the change.
"""
else
md"""
**Great!** As you can see, all the cells in this notebook are linked together by the variables they define and use. Just like a spreadsheet!
"""
end
# ╔═╡ d7e8202c-e7b5-11ea-30d3-adcd6867d5f5
md"### Exercise 2.1
As you can see, the total area covered by triangles is lower when the complexity is higher."
# ╔═╡ f22222b4-e7b5-11ea-0ea0-8fa368d2a014
md"""
Can you write a function that computes the _area of `sierpinski(n)`_, as a fraction of the area of `sierpinski(0)`?
So:
```
area_sierpinski(0) = 1.0
area_sierpinski(1) = 0.??
...
```
"""
# ╔═╡ ca8d2f72-e7b6-11ea-1893-f1e6d0a20dc7
function area_sierpinski(n)
if n==0
return 1.0
else
return (3/4)^n
end
end
# ╔═╡ 71c78614-e7bc-11ea-0959-c7a91a10d481
if area_sierpinski(0) == 1.0 && area_sierpinski(1) == 3 / 4
md"""
!!! correct
Well done!
"""
else
md"""
!!! warning "Incorrect"
Keep working on it!
"""
end
# ╔═╡ c21096c0-e856-11ea-3dc5-a5b0cbf29335
md"**Let's try it out below:**"
# ╔═╡ 52533e00-e856-11ea-08a7-25e556fb1127
md"Complexity = $(@bind n Slider(0:6, show_value=true))"
# ╔═╡ c1ecad86-e7bc-11ea-1201-23ee380181a1
md"""
!!! hint
Can you write `area_sierpinksi(n)` as a function of `area_sierpinski(n-1)`?
"""
# ╔═╡ a60a492a-e7bc-11ea-0f0b-75d81ce46a01
md"That's it for now, see you next week!"
# ╔═╡ dfdeab34-e751-11ea-0f90-2fa9bbdccb1e
triangle() = compose(context(), polygon([(1, 1), (0, 1), (1 / 2, 0)]))
# ╔═╡ b923d394-e750-11ea-1971-595e09ab35b5
# It does not matter which order you define the building blocks (functions) of the
# program in. The best way to organize code is the one that promotes understanding.
function place_in_3_corners(t)
# Uses the Compose library to place 3 copies of t
# in the 3 corners of a triangle.
# treat this function as a black box,
# or learn how it works from the Compose documentation here https://giovineitalia.github.io/Compose.jl/latest/tutorial/#Compose-is-declarative-1
compose(context(),
(context(1 / 4, 0, 1 / 2, 1 / 2), t),
(context(0, 1 / 2, 1 / 2, 1 / 2), t),
(context(1 / 2, 1 / 2, 1 / 2, 1 / 2), t))
end
# ╔═╡ e2848b9a-e703-11ea-24f9-b9131434a84b
function sierpinski(n)
if n == 0
triangle()
else
t = sierpinski(n - 1) # recursively construct a smaller sierpinski's triangle
place_in_3_corners(t) # place it in the 3 corners of a triangle
end
end
# ╔═╡ 9664ac52-e750-11ea-171c-e7d57741a68c
sierpinski(complexity)
# ╔═╡ df0a4068-e7b2-11ea-2475-81b237d492b3
sierpinski.(0:6)
# ╔═╡ 147ed7b0-e856-11ea-0d0e-7ff0d527e352
md"""
Sierpinski's triangle of complexity $(n)
$(sierpinski(n))
has area **$(area_sierpinski(n))**
"""
# ╔═╡ Cell order:
# ╟─fafae38e-e852-11ea-1208-732b4744e4c2
# ╟─a2181260-e6cd-11ea-2a69-8d9d31d1ef0e
# ╟─31a8fbf8-e6ce-11ea-2c66-4b4d02b41995
# ╟─f9d7250a-706f-11eb-104d-3f07c59f7174
# ╟─430a260e-6cbb-11eb-34af-31366543c9dc
# ╟─a05d2bc8-7024-11eb-08cb-196543bbb8fd
# ╠═e02f7ea6-7024-11eb-3672-fd59a6cff79b
# ╟─6acef56c-7025-11eb-2524-819c30a75d39
# ╟─348cea34-7025-11eb-3def-41bbc16c7512
# ╟─339c2d5c-e6ce-11ea-32f9-714b3628909c
# ╟─56866718-e6ce-11ea-0804-d108af4e5653
# ╠═bccf0e88-e754-11ea-3ab8-0170c2d44628
# ╟─e7abd366-e7a6-11ea-30d7-1b6194614d0a
# ╟─d62f223c-e754-11ea-2470-e72a605a9d7e
# ╠═4896bf0c-e754-11ea-19dc-1380bb356ab6
# ╠═7a01a508-e78a-11ea-11da-999d38785348
# ╠═682db9f8-e7b1-11ea-3949-6b683ca8b47b
# ╟─088cc652-e7a8-11ea-0ca7-f744f6f3afdd
# ╟─c18dce7a-e7a7-11ea-0a1a-f944d46754e5
# ╟─5e24d95c-e6ce-11ea-24be-bb19e1e14657
# ╟─6b8883f6-e7b3-11ea-155e-6f62117e123b
# ╠═851c03a4-e7a4-11ea-1652-d59b7a6599f0
# ╠═d6ee91ea-e750-11ea-1260-31ebf3ec6a9b
# ╠═5acd58e0-e856-11ea-2d3d-8329889fe16f
# ╟─dbc4da6a-e7b4-11ea-3b70-6f2abfcab992
# ╠═e2848b9a-e703-11ea-24f9-b9131434a84b
# ╠═9664ac52-e750-11ea-171c-e7d57741a68c
# ╠═02b9c9d6-e752-11ea-0f32-91b7b6481684
# ╟─1eb79812-e7b5-11ea-1c10-63b24803dd8a
# ╟─d7e8202c-e7b5-11ea-30d3-adcd6867d5f5
# ╠═df0a4068-e7b2-11ea-2475-81b237d492b3
# ╟─f22222b4-e7b5-11ea-0ea0-8fa368d2a014
# ╠═ca8d2f72-e7b6-11ea-1893-f1e6d0a20dc7
# ╟─71c78614-e7bc-11ea-0959-c7a91a10d481
# ╟─c21096c0-e856-11ea-3dc5-a5b0cbf29335
# ╟─52533e00-e856-11ea-08a7-25e556fb1127
# ╟─147ed7b0-e856-11ea-0d0e-7ff0d527e352
# ╟─c1ecad86-e7bc-11ea-1201-23ee380181a1
# ╟─a60a492a-e7bc-11ea-0f0b-75d81ce46a01
# ╟─dfdeab34-e751-11ea-0f90-2fa9bbdccb1e
# ╟─b923d394-e750-11ea-1971-595e09ab35b5