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 ∫01ax3+3dx 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.
fromscipyimportintegratedefintegral(a):func=lambdax:a*x**3+3result=integrate.quad(func,0,1)print("a = %d, result = %f"%(a,result))forain[2,3,4]:integral(a)
a = 2, result = 3.500000
a = 3, result = 3.750000
a = 4, result = 4.000000
Problem 1 Rubrics:
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, θ) subtended by an object x in the sky is given by
where Rx is the radius of the object and Dx is the distance to the object. The units of Rx and Dx must be the same.
(a) Create a python function to perform this calculation.
(b) Execute this function for the moon (Dm=3.84×105 km, Rm=1.74×103 km) and sun (Ds=1.496×108 km. Rs=6.96×105 km) using the function you wrote. Store the results into a variable for each calculation, and print the result.
Dm,Rm=3.84e5,1.74e3Ds,Rs=1.496e8,6.96e5theta_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)
Angular diameter for the moon is: 0.519245
Angular diameter for the sun is: 0.533128
(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_sprint("The ratio of the moon to the sun angular diameter is: %f"%ratio)
The ratio of the moon to the sun angular diameter is: 0.973958
(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.
Problem 2 Rubrics:
(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]