[2 points] Create a variable called x
which is a list containing all even numbers less than 100 using the list(range(...))
syntax.
[1 point] Compute the sum of x
without using a for
loop
[4 points] Compute the sum of x
with a for
loop.
[1 point] Print the elements of x
reversed using a slice
[2 point] Print the second half of x
using the len(x) // 2
syntax
[4 points] Create a new empty list y
. Using a for
loop and the append
keyword, make y
contain the square of each element of x
. So it should contain: [4, 16, ..., ]
.
[2 points] Create a variable called a
which is an array containing all even numbers less than 100 using the numpy
arange
syntax.
[1 point] Compute the sum of a
without using a for
loop
[1 point] Print the elements of a
reversed using a slice
[2 points] Print the minimum, maximum, and mean elements of a
using numpy
functions.
[1 point] Print the square of each element in a
without using a for
loop or lists. You should just have one line of numpy
code.
For each problem below, use numpy to create x
and y
arrays which are plotted. Be sure to label your x-axis, y-axis, put the problem number as the title, use at least 500 points, make your figures be 4x3 inches, and add a legend if you have more than one line being plotted.
[6 points] Plot $y = x^2$ from $-1$ to $1$
[8 points] A hanging rope, wire, or chain follows a catenary curve ($y = a\textrm{cosh}\frac{x}{a} - a$), although many including Galileo mistakenly believed hanging ropes a parabolas. Compare the catenary and parabolic ($y = x^2$) curves over $-1$ to $1$ where $a = 0.63$.
[8 points] Compare the functions $\cos x$ and $\cosh x - 1$ from $-\pi/2$ to $\pi/2$
Use this probability distribution:
$$ Q = \{\textrm{red},\textrm{ green}, \textrm{blue}\} $$$$ P(\textrm{red}) = 0.1,\, P(\textrm{green}) = 0.5,\, P(\textrm{blue}),\, = 0.4 $$[2 points] Create a dictionary called prob
where the key is the colors as a string and the values are the probability.
[4 points] Starting with this fragment, show that your probability distribution is normalized.
for key,value in prob.items():
print(key, value)
[4 points] Let's define a random variable $X$ that is 1 for red, 2 for green, and 0 for blue. Using your for
loop from 4.2, use boolean statements to set a variable $x$ to what the value of $X$ should be using the key
variable. Print out key, x
, which should look like red, 1
, green, 2
...
[4 points] Compute the expected value of x
using the for
loop from 4.3
[4 points] Compute the variance by hand, showing the steps in Markdown