# Define the angle of the spiral (polar coords)
# go around two full times (2*pi = one revolution)
<- seq(0, 4*pi, .01)
theta # Define the distance from the origin of the spiral
# Needs to have the same length as theta
<- seq(0, 5, length.out = length(theta))
r
# Now define x and y in cartesian coordinates
<- r * cos(theta)
x <- r * sin(theta)
y
plot(x, y, type = "l")
Homework 1: Scripts and Notebooks
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\).
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)
= np.arange(0, 4 * np.pi, 0.01)
theta # 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)
= np.arange(0, 5, 5/theta.size)
r
# Now define x and y in cartesian coordinates
= r * np.cos(theta)
x = r * np.sin(theta)
y
# Define the axes
= plt.subplots()
fig, ax # Plot the line
ax.plot(x, y) plt.show()