Homework 1: Basic usage of python and iPython notebook
You will use this notebook to provide your answers to this assignment
Write a function to solve the definite integral $\int_0^1 ax^3 + 3 dx$ The following will be very useful: Scipy documentation.
What is the result of the integral with defined values of $a$ being $a=2$, $a=3$, and $a=4$?
Your function must be set up to run with only one input $a$, which is passed into the function as the first argument. Print your answers for each value of $a$ in your notebook that you turn in.
from scipy import integrate def integral(a): func = lambda x : a * x**3 + 3 result = integrate.quad(func, 0, 1)[0] print("a = %d, result = %f" %(a, result)) for a in [2,3,4]: integral(a)
Write the correct function, print the results, results are correct [10 points]
Don't write the correct function [-6 points] (will get partial credit if function is partial correct)
Don't print out the results [-1 point]
Incorrect answers [-3 points]
A few notes:
In the function, remember to put the return statement at the end. If you put a print statement after return statement, the function will not execute the print statement.
As stated in the instruction, your function must be set up to run with only one input $a$. Violating this will result in losing points.
Calculting the integral using analytical solution other than using scipy also gets full mark.
The angular diameter (in degrees, $\theta$) subtended by an object $x$ in the sky is given by $\theta=2\sin^{-1}\frac{R_x}{D_x}$ where ${R_x}$ is the radius of the object and ${D_x}$ is the distance to the object. The units of ${R_x}$ and ${D_x}$ must be the same.
(a) Create a python function to perform this calculation.
import numpy as np def angular_diameter(radius, distance): return 2 * np.degrees(np.arcsin(radius / distance))
(b) Execute this function for the moon (${D_m = 3.84 \times 10^{5} }$ km, ${R_m = 1.74 \times 10^3}$ km) and sun (${D_s = 1.496 \times 10^{8}}$ km. ${R_s = 6.96 \times 10^{5}}$ km) using the function you wrote. Store the results into a variable for each calculation, and print the result.
Dm, Rm = 3.84e5, 1.74e3 Ds, Rs = 1.496e8, 6.96e5 theta_m = angular_diameter(Rm, Dm) theta_s = angular_diameter(Rs, Ds) print("Angular diameter for the moon is: %f" % theta_m) print("Angular diameter for the sun is: %f" % theta_s)
(c) Calculate the ratio of the moon to the sun apparent angular diameter with the results of (b) stored in memory (i.e. not typed in!!). Print the result.
ratio = theta_m / theta_s print("The ratio of the moon to the sun angular diameter is: %f" % ratio)
(d) Does this answer explain why we have solar eclipses? Why or why not? Answer in a markdown cell.
Yes. The ratio is nearly 1, which means during solar exlipses the moon can almost cover the sun from the view on the earth.
(a) Correctly create the function [3 points]
(b) Correct answer, store the results to variables [3 points] (both degrees and radians are okay)
Incorrect answer [-1.5 points]
Not storing to variables [-1.5 points]
(c) Correct answer [3 points]
Don't print the result [-1.5 points]
Don't use the results in (b) stored in memory [-1.5 points]
(d) Correct answer [1 point]