CoCalc Public Filesatms391geodata / Week 1 / Homework 1: Basic python notebook (answer key).ipynbOpen with one click!
Author: Steve Nesbitt
Views : 227
Compute Environment: Ubuntu 18.04 (Deprecated)

ATMS 391: Geophysical data analysis

Homework 1: Basic usage of python and iPython notebook


You will use this notebook to provide your answers to this assignment

Problem 1

Write a function to solve the definite integral 01ax3+3dx\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 aa being a=2a=2, a=3a=3, and a=4a=4?
Your function must be set up to run with only one input aa, which is passed into the function as the first argument. Print your answers for each value of aa in your notebook that you turn in.

In [1]:
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)
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:

  1. 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.

  2. As stated in the instruction, your function must be set up to run with only one input aa. Violating this will result in losing points.

  3. Calculting the integral using analytical solution other than using scipy also gets full mark.

Problem 2

The angular diameter (in degrees, θ\theta) subtended by an object xx in the sky is given by θ=2sin1RxDx\theta=2\sin^{-1}\frac{R_x}{D_x} where Rx{R_x} is the radius of the object and Dx{D_x} is the distance to the object. The units of Rx{R_x} and Dx{D_x} must be the same.

(a) Create a python function to perform this calculation.

In [2]:
import numpy as np def angular_diameter(radius, distance): return 2 * np.degrees(np.arcsin(radius / distance))

(b) Execute this function for the moon (Dm=3.84×105{D_m = 3.84 \times 10^{5} } km, Rm=1.74×103{R_m = 1.74 \times 10^3} km) and sun (Ds=1.496×108{D_s = 1.496 \times 10^{8}} km. Rs=6.96×105{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.

In [3]:
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)
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.

In [6]:
ratio = theta_m / theta_s print("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]

(d) Correct answer [1 point]