Homework 1: Scripts and Notebooks

HW
Week02
Author

Your Name

Published

February 2, 2024

Download the starter qmd file here

What is the difference between a script and a notebook?

Replace this paragraph with 2-3 sentences describing your understanding of the difference between a script and a notebook. Your answer should be applicable to R or python (so if you discuss python notebooks, you should also discuss the equivalent in R). Use markdown formatting as described in this cheat-sheet. You may want to provide a table or itemized list, and you should use code formatting to indicate file extensions and programming languages.

Playing with Code in Notebooks

The code chunk below defines a logarithmic spiral. Using this reference, modify the code so that it now plots Fermat’s spiral. Use \(a = 1\).

# Define the angle of the spiral (polar coords)
# go around two full times (2*pi = one revolution)
theta <- seq(0, 4*pi, .01) 
# Define the distance from the origin of the spiral
# Needs to have the same length as theta
r <- seq(0, 5, length.out = length(theta))

# Now define x and y in cartesian coordinates
x <- r * cos(theta)
y <- r * sin(theta)

plot(x, y, type = "l")

Can you do the same thing in Python? It may help to know that in Python, to raise something to a power, you use ** instead of ^.

import numpy as np
import matplotlib.pyplot as plt

# Define the angle of the spiral (polar coords)
# go around two full times (2*pi = one revolution)
theta = np.arange(0, 4 * np.pi, 0.01)
# Define the distance from the origin of the spiral
# Needs to have the same length as theta 
# (get length of theta with theta.size, 
#  and then divide 5 by that to get the increment)
r = np.arange(0, 5, 5/theta.size)

# Now define x and y in cartesian coordinates
x = r * np.cos(theta)
y = r * np.sin(theta)

# Define the axes
fig, ax = plt.subplots()
# Plot the line
ax.plot(x, y)
plt.show()